Home > Software engineering >  Getting the first item for a tuple for each row in a list in Scala
Getting the first item for a tuple for each row in a list in Scala

Time:10-16

I am looking to do this in Scala, but nothing works. In pyspark it works obviously.

from operator import itemgetter
rdd = sc.parallelize([(0, [(0,'a'), (1,'b'), (2,'c')]), (1, [(3,'x'), (5,'y'), (6,'z')])])
mapped = rdd.mapValues(lambda v: map(itemgetter(0), v))

Output

mapped.collect()
[(0, [0, 1, 2]), (1, [3, 5, 6])]

CodePudding user response:

val rdd = sparkContext.parallelize(List(
  (0, Array((0, "a"), (1, "b"), (2, "c"))),
  (1, Array((3, "x"), (5, "y"), (6, "z")))
))

rdd
  .mapValues(v => v.map(_._1))
  .foreach(v=>println(v._1 "; " v._2.toSeq.mkString(",") ))

Output:

0; 0,1,2
1; 3,5,6
  • Related