41
41
/**
42
42
* Suite for testing the aggregate functionality of Datasets in Java.
43
43
*/
44
- public class JavaDatasetAggregatorSuite implements Serializable {
45
- private transient JavaSparkContext jsc ;
46
- private transient TestSQLContext context ;
47
-
48
- @ Before
49
- public void setUp () {
50
- // Trigger static initializer of TestData
51
- SparkContext sc = new SparkContext ("local[*]" , "testing" );
52
- jsc = new JavaSparkContext (sc );
53
- context = new TestSQLContext (sc );
54
- context .loadTestData ();
55
- }
56
-
57
- @ After
58
- public void tearDown () {
59
- context .sparkContext ().stop ();
60
- context = null ;
61
- jsc = null ;
62
- }
63
-
64
- private <T1 , T2 > Tuple2 <T1 , T2 > tuple2 (T1 t1 , T2 t2 ) {
65
- return new Tuple2 <>(t1 , t2 );
66
- }
67
-
68
- private KeyValueGroupedDataset <String , Tuple2 <String , Integer >> generateGroupedDataset () {
69
- Encoder <Tuple2 <String , Integer >> encoder = Encoders .tuple (Encoders .STRING (), Encoders .INT ());
70
- List <Tuple2 <String , Integer >> data =
71
- Arrays .asList (tuple2 ("a" , 1 ), tuple2 ("a" , 2 ), tuple2 ("b" , 3 ));
72
- Dataset <Tuple2 <String , Integer >> ds = context .createDataset (data , encoder );
73
-
74
- return ds .groupByKey (
75
- new MapFunction <Tuple2 <String , Integer >, String >() {
76
- @ Override
77
- public String call (Tuple2 <String , Integer > value ) throws Exception {
78
- return value ._1 ();
79
- }
80
- },
81
- Encoders .STRING ());
82
- }
83
-
44
+ public class JavaDatasetAggregatorSuite extends JavaDatasetAggregatorSuiteBase {
84
45
@ Test
85
46
public void testTypedAggregationAnonClass () {
86
47
KeyValueGroupedDataset <String , Tuple2 <String , Integer >> grouped = generateGroupedDataset ();
@@ -100,7 +61,6 @@ public void testTypedAggregationAnonClass() {
100
61
}
101
62
102
63
static class IntSumOf extends Aggregator <Tuple2 <String , Integer >, Integer , Integer > {
103
-
104
64
@ Override
105
65
public Integer zero () {
106
66
return 0 ;
@@ -170,3 +130,47 @@ public Long call(Tuple2<String, Integer> value) throws Exception {
170
130
Assert .assertEquals (Arrays .asList (tuple2 ("a" , 3 ), tuple2 ("b" , 3 )), agged .collectAsList ());
171
131
}
172
132
}
133
+
134
+ /**
135
+ * Common test base shared across this and Java8DatasetAggregatorSuite.
136
+ */
137
+ class JavaDatasetAggregatorSuiteBase implements Serializable {
138
+ protected transient JavaSparkContext jsc ;
139
+ protected transient TestSQLContext context ;
140
+
141
+ @ Before
142
+ public void setUp () {
143
+ // Trigger static initializer of TestData
144
+ SparkContext sc = new SparkContext ("local[*]" , "testing" );
145
+ jsc = new JavaSparkContext (sc );
146
+ context = new TestSQLContext (sc );
147
+ context .loadTestData ();
148
+ }
149
+
150
+ @ After
151
+ public void tearDown () {
152
+ context .sparkContext ().stop ();
153
+ context = null ;
154
+ jsc = null ;
155
+ }
156
+
157
+ protected <T1 , T2 > Tuple2 <T1 , T2 > tuple2 (T1 t1 , T2 t2 ) {
158
+ return new Tuple2 <>(t1 , t2 );
159
+ }
160
+
161
+ protected KeyValueGroupedDataset <String , Tuple2 <String , Integer >> generateGroupedDataset () {
162
+ Encoder <Tuple2 <String , Integer >> encoder = Encoders .tuple (Encoders .STRING (), Encoders .INT ());
163
+ List <Tuple2 <String , Integer >> data =
164
+ Arrays .asList (tuple2 ("a" , 1 ), tuple2 ("a" , 2 ), tuple2 ("b" , 3 ));
165
+ Dataset <Tuple2 <String , Integer >> ds = context .createDataset (data , encoder );
166
+
167
+ return ds .groupByKey (
168
+ new MapFunction <Tuple2 <String , Integer >, String >() {
169
+ @ Override
170
+ public String call (Tuple2 <String , Integer > value ) throws Exception {
171
+ return value ._1 ();
172
+ }
173
+ },
174
+ Encoders .STRING ());
175
+ }
176
+ }
0 commit comments