scalaapache-sparkrddsparkcore

Scala/RDD : How to compare a value of tuple with a list of values in the same tuple


I have data like below

val t=((1,List(1,2,3,4)),(2,List(1,2,3,4)),(3,List(1,2,3,4)),(4,List(1,2,3,4)))

and I want output like :

1--1
2--2
3--3
4--4

Can some body please help me here by using scala or spark core.


Solution

  • Another way using RDD:

    Input:

    (1,List(1, 2, 3, 4))
    (2,List(1, 2, 3, 4))
    (3,List(1, 2, 3, 4))
    (4,List(1, 2, 3, 4))
    
    t.map(x => s"""${x._1} -- ${if(x._2.contains(x._1)) x._1}""").collect.foreach(println)  
    

    Output:

    1 -- 1
    2 -- 2
    3 -- 3
    4 -- 4