I have the following WordCount example and I would like to get the result to JSONObject:
JavaPairRDD<String, Integer> counts = words.mapToPair(new PairFunction<String, String, Integer>() {
public Tuple2<String, Integer> call(String x) {
return new Tuple2<String, Integer>(x, 1);
}
}).reduceByKey(new Function2<Integer, Integer, Integer>() {
public Integer call(Integer x, Integer y) {
return x + y;
}
});
JSONObject json = new JSONObject();
counts.foreach(line -> {
json.put(line._1(), line._2());
});
It seems that json
remains empty after tracing counts
, as if it were an internal variable within the iterator. How can I get it out?