在Scala程序中找到缺少的代码,以下列格式显示输出。
输出:
Array[(Int, String)] = Array((4,anar), (5,applelichi), (6,bananagrapes), (7,oranges))
程序
val a = sc.parallelize(List("apple","banana","oranges","grapes","lichi","anar"))
val b = a.map(x =>(x.length,x))
答案 0 :(得分:0)
我相信这会给你想要的结果:
a.groupBy(_.length).mapValues(_.sorted.mkString).toArray.sorted
答案 1 :(得分:0)
您需要的只是reduceByKey
val a = sc.parallelize(List("apple","banana","oranges","grapes","lichi","anar"))
val b = a.map(x =>(x.length,x)).reduceByKey(_ + _)
应该给你
b: org.apache.spark.rdd.RDD[(Int, String)] = ....
//(4,anar)
//(6,bananagrapes)
//(7,oranges)
//(5,applelichi)