1

I am new to spark and i am trying to perform a mapping of words to counts and errors for the implementation of Space Saving Algorithm. For every word i need to keeps its counts and its errors. My code the following. The problem is that i cannot access the elements in array of integers within 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
  • What error are you getting? Commented Dec 29, 2014 at 13:28

1 Answer 1

1

change this:

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]);
}

to

for (Tuple2<String, Integer[]> tuple : output) {
    Integer temp = tuple._2()[0];
    System.out.println(tuple._1() + ": " + tuple._2() + " " + temp[0]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.