lookup rdd where keys are from different rdd

I am having difficulty in looking up value from a RDD where keys come from a different RDD. I need to convert RDD of topsuperhero array[Int] to plain string then I can pass to namesmap.lookup rdd.

How to convert array of [Int] to plain string ? do I need to write a function ?

val names = sc.textFile("C:\\Users\\kalit_000\\Desktop\\udemy_spark\\marvelnames.txt")
val namemap = names.map(x => x.split(" ")).map(x => (x(0),x(1)))

val graph = sc.textFile("C:\\Users\\kalit_000\\Desktop\\udemy_spark\\marvelgraph.txt")
val graphmap = graph.map(x => x.split(" ")).map(x => (x(0).toInt,(x.length -1).toInt))

val totalfirendsBycharacter = graphmap.mapValues(x => (x)).reduceByKey((x,y) => x+y)
val topsuperhero = totalfirendsBycharacter.sortBy(_._2,false).take(1)

Error: topsuperhero is array[Int] which I need to convert to plain string how can I achieve this ?

namemap.lookup(topsuperhero.map(x => x._1)).foreach(println)

Upvotes: 0

Views: 141

Answers (1)

eliasah
eliasah

Reputation: 40370

To convert your Array[Int] into a String you can do the following :

val str = topsuperhero.mkString(" ") 

That will convert topsuperhero into a String

Upvotes: 1

Related Questions