Bael 1166 intro apache spark (#2875)
* Changes for BAEL-1166 * Changes for BAEL_1166 * Changes for BAEL 1166 * Changes for BAEL 1166 * Changes for BAEL_1166
This commit is contained in:
parent
814aff3031
commit
c389f56da5
|
@ -23,28 +23,15 @@ public class WordCount {
|
||||||
System.err.println("Usage: JavaWordCount <file>");
|
System.err.println("Usage: JavaWordCount <file>");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
SparkConf sparkConf = new SparkConf().setAppName("JavaWordCount").setMaster("local");
|
SparkConf sparkConf = new SparkConf().setAppName("JavaWordCount")
|
||||||
|
.setMaster("local");
|
||||||
JavaSparkContext ctx = new JavaSparkContext(sparkConf);
|
JavaSparkContext ctx = new JavaSparkContext(sparkConf);
|
||||||
JavaRDD<String> lines = ctx.textFile(args[0], 1);
|
JavaRDD<String> lines = ctx.textFile(args[0], 1);
|
||||||
|
|
||||||
JavaRDD<String> words = lines.flatMap(s -> Arrays.asList(SPACE.split(s)).iterator());
|
JavaRDD<String> words = lines.flatMap(s -> Arrays.asList(SPACE.split(s)).iterator());
|
||||||
JavaPairRDD<String, Integer> ones = words.mapToPair(
|
JavaPairRDD<String, Integer> wordAsTuple = words.mapToPair(word -> new Tuple2<>(word, 1));
|
||||||
new PairFunction<String, String, Integer>() {
|
JavaPairRDD<String, Integer> wordWithCount = wordAsTuple.reduceByKey((Integer i1, Integer i2)->i1 + i2);
|
||||||
@Override
|
List<Tuple2<String, Integer>> output = wordWithCount.collect();
|
||||||
public Tuple2<String, Integer> call(String s) {
|
|
||||||
return new Tuple2<>(s, 1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
JavaPairRDD<String, Integer> counts = ones.reduceByKey(
|
|
||||||
new Function2<Integer, Integer, Integer>() {
|
|
||||||
@Override
|
|
||||||
public Integer call(Integer i1, Integer i2) {
|
|
||||||
return i1 + i2;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
List<Tuple2<String, Integer>> output = counts.collect();
|
|
||||||
for (Tuple2<?, ?> tuple : output) {
|
for (Tuple2<?, ?> tuple : output) {
|
||||||
System.out.println(tuple._1() + ": " + tuple._2());
|
System.out.println(tuple._1() + ": " + tuple._2());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue