-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransformationsExample.java
42 lines (32 loc) · 1.91 KB
/
TransformationsExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package sparkpipeline.example;
import static org.apache.spark.sql.functions.col;
import static org.apache.spark.sql.functions.sum;
import sparkpipeline.core.pipeline.Pipeline;
import sparkpipeline.core.reader.ReaderCSV;
class TransformationsExample {
static final String DATASET_1 = "DATASET_1_NAME";
static final String DATASET_2 = "DATASET_2_NAME";
static final String DATASET_3 = "DATASET_3_NAME";
static final String DATASET_1_PATH_INPUT = "example/src-resource/fileA.csv";
static final String DATASET_1_TRANSF_PATH_OUTPUT = "example/build/temp-outputs/TransformationsExample";
public static void main(String[] args) {
Pipeline.init()
.read(DATASET_1, ReaderCSV.init(DATASET_1_PATH_INPUT).hasHeader(true))
.anyRunning(context -> context.datasetByKey(DATASET_1).show())
// transform dataset with methods
.transform(DATASET_1, context -> context.datasetByKey(DATASET_1).filter(col("category").notEqual("A")))
// transform dataset with sqlContext
.transformSql(DATASET_1, context -> String.format("select * from %s where category <> 'B'", DATASET_1))
.anyRunning(context -> context.datasetByKey(DATASET_1).show())
// transform dataset with methods into new dataset
.transform(DATASET_2, context -> context.datasetByKey(DATASET_1).groupBy("category").agg(sum("value")))
// transform dataset with sqlContext into new dataset
.transformSql(DATASET_3, context -> String.format("select * from %s where category is not null", DATASET_1))
.anyRunning(context -> {
context.datasetByKey(DATASET_1).show();
context.datasetByKey(DATASET_2).show();
context.datasetByKey(DATASET_3).show();
})
.execute();
}
}