0

I'd like to use a conditional statement in a map function. For example,

JavaPairRDD<Integer, Long> rdd1 = sc.parallelize(Arrays.asList(1, 2, 1, 0)).zipWithIndex();
rdd1.map(x -> if x._1 == 2 return x._1*x._1 else return x._1).foreach(x -> System.out.println(x));

should print [(1,0), (4, 1), (1, 2), (0, 3)]. So, what's the proper way of using the map function in such way ?

2 Answers 2

1

You can write your code like this :

JavaPairRDD<Integer, Long> rdd1 = sc.parallelize(Arrays.asList(1, 2, 1,   0)).zipWithIndex();
rdd1.mapToPair(x -> 
        {
            if (x._1 == 2) 
                return new Tuple2<Integer, Long>(x._1*x._1, x._2); 
            else 
                return new Tuple2<Integer, Long>(x._1, x._2); 
        }).foreach(x -> System.out.println(x));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Was really helpful.
0

JavaPairRDD does not define a map() function, but a mapValue() function which can only change the value on a pair and not the key, you could try using another RDD, or, if using that one, don't put values you would want to change in the Key position, but in an Object in the value.

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.