1
val rdd :Array[Array[String]] = Array(Array("2345","345","fghj","dfhg")
                                ,Array("2345","3450","fghj","dfhg")
                                ,Array("23145","1345","fghj","dffghg")
                                ,Array("23045","345","feghj","adfhg"))

this is my input. I need to extract first two elements of each array in the form of key value pair.

I would like to get output

(2345,345)
(2345,3450)
(23145,1345)
(23045,345)

1 Answer 1

2

You can simply do

rdd.map(array => (array(0), array(1)))
//res0: Array[(String, String)] = Array((2345,345), (2345,3450), (23145,1345), (23045,345))

If you want the output in Map then you can add .toMap function call

rdd.map(array => (array(0), array(1))).toMap
//res0: scala.collection.immutable.Map[String,String] = Map(2345 -> 3450, 23145 -> 1345, 23045 -> 345)
Sign up to request clarification or add additional context in comments.

1 Comment

im getting as array output .i would like to get as key value pair

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.