convert JavaPairRDDString, Integer to Json

0

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?

    
asked by invitado 13.02.2018 в 17:47
source

1 answer

0
    List<Tuple2<String, Integer>> finalCounts = counts.toArray();

    JSONObject json = new JSONObject();

    for (Tuple2<String, Integer> count : finalCounts)
        json.put(count._1(), count._2());
    
answered by 15.02.2018 / 16:37
source