1
1
package com .sparkTutorial .pairRdd .groupbykey ;
2
2
3
+ import com .google .common .collect .Iterables ;
3
4
import com .sparkTutorial .rdd .commons .Utils ;
5
+ import org .apache .log4j .Level ;
6
+ import org .apache .log4j .Logger ;
4
7
import org .apache .spark .SparkConf ;
5
8
import org .apache .spark .api .java .JavaPairRDD ;
6
9
import org .apache .spark .api .java .JavaRDD ;
7
10
import org .apache .spark .api .java .JavaSparkContext ;
8
11
import org .apache .spark .api .java .function .PairFunction ;
9
12
import scala .Tuple2 ;
10
13
14
+ import java .util .Arrays ;
15
+
11
16
public class AirportsByCountrySolution {
12
17
13
18
public static void main (String []args )throws Exception {
14
-
19
+ Logger . getLogger ( "org" ). setLevel ( Level . ERROR );
15
20
SparkConf conf =new SparkConf ().setAppName ("airports" ).setMaster ("local[*]" );
16
-
17
21
JavaSparkContext sc =new JavaSparkContext (conf );
18
22
19
23
JavaRDD <String >lines =sc .textFile ("in/airports.text" );
@@ -26,11 +30,11 @@ public static void main(String[] args) throws Exception {
26
30
JavaPairRDD <String ,Iterable <String >>AirportsByCountry =CountryAndAirportNameAndPair .groupByKey ();
27
31
28
32
for (Tuple2 <String ,Iterable <String >>airports :AirportsByCountry .collect ()) {
29
- System .out .print (airports ._1 () +" : [" );
30
- for (String s :airports ._2 ()) {
31
- System .out .print (s +", " );
32
- }
33
- System .out .println ("]" );
33
+ System .out .println (airports ._1 () +" : " +iterableToString (airports ._2 ()));
34
34
}
35
35
}
36
+
37
+ private static String iterableToString (Iterable <String >iterable ) {
38
+ return Arrays .toString (Iterables .toArray (iterable ,String .class ));
39
+ }
36
40
}