Apache Spark:如何在Java中的Tuple2对象中访问嵌套的整数数组?

时间:2014-12-29 13:24:12

标签: java apache-spark

我是新来的火花,我正在尝试将字数映射到计数和错误,以实现节省空间算法。对于我需要保留其计数和错误的每个单词。我的代码如下。问题是我无法访问tuple2中的整数数组中的元素。

JavaPairRDD<String, Integer[]> ones = words.mapToPair(new PairFunction<String, String, Integer[]>() {
        @Override
        public Tuple2<String, Integer[]> call(String s) {
            Integer[] defaults = {1,0};
            return new Tuple2<String, Integer[]>(s, defaults);
        }
    });

    List<Tuple2<String,Integer[]>> output = ones.collect();

    for (Tuple2<?, ?> tuple : output) {
        Integer temp = (Integer) tuple._2()[0];  ///ERROR, HOW CAN I ACCESS ARRAY ELEMENT???
        System.out.println(tuple._1() + ": " + tuple._2()+" "+temp[0]);
    }

1 个答案:

答案 0 :(得分:1)

改变这个:

for (Tuple2<?, ?> tuple : output) {
    Integer temp = (Integer) tuple._2()[0];  ///ERROR, HOW CAN I ACCESS ARRAY ELEMENT???
    System.out.println(tuple._1() + ": " + tuple._2()+" "+temp[0]);
}

for (Tuple2<String, Integer[]> tuple : output) {
    Integer temp = tuple._2()[0];
    System.out.println(tuple._1() + ": " + tuple._2() + " " + temp[0]);
}