SQLTransformer

SQLTransformer

implements the transformations which are defined by SQL statement. Currently, support SQL syntax like
1
SELECT a, a + b AS ab FROM __THIS__
2
SELECT a, SQRT(b) AS bsqrt FROM __THIS__ where a > 5
3
SELECT a, b, SUM(c) AS csum FROM __THIS__ GROUP BY a, b
Copied!
SQLTransformer implements the transformations which are defined by SQL statement. Currently, we only support SQL syntax like "SELECT ... FROM __THIS__ ..." where "__THIS__" represents the underlying table of the input dataset. The select clause specifies the fields, constants, and expressions to display in the output, and can be any select clause that Spark SQL supports. Users can also use Spark SQL built-in function and UDFs to operate on these selected columns. For example, SQLTransformer supports statements like:
1
SELECT a, a + b AS ab FROM __THIS__
2
SELECT a, SQRT(b) AS bsqrt FROM __THIS__ where a > 5
3
SELECT a, b, SUM(c) AS csum FROM __THIS__ GROUP BY a, b
Copied!
Examples
Assume that we have the following DataFrame with columns id, v1 and v2:
1
id | v1 | v2
2
----|-----|-----
3
0 | 1.0 | 3.0
4
2 | 2.0 | 5.0
Copied!
1
import org.apache.spark.ml.feature.SQLTransformer
2
​
3
val df = spark.createDataFrame(
4
Seq((0, 1.0, 3.0), (2, 2.0, 5.0))).toDF("id", "v1", "v2")
5
​
6
val sqlTrans = new SQLTransformer().setStatement(
7
"SELECT *, (v1 + v2) AS v3, (v1 * v2) AS v4 FROM __THIS__")
8
​
9
sqlTrans.transform(df).show()
10
/*
11
+---+---+---+---+----+
12
| id| v1| v2| v3| v4|
13
+---+---+---+---+----+
14
| 0|1.0|3.0|4.0| 3.0|
15
| 2|2.0|5.0|7.0|10.0|
16
+---+---+---+---+----+
17
​
18
*/
Copied!
Last modified 1yr ago
Copy link