Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何展平键值元组的值元组?_Scala_Apache Spark_Scala Collections - Fatal编程技术网

Scala 如何展平键值元组的值元组?

Scala 如何展平键值元组的值元组?,scala,apache-spark,scala-collections,Scala,Apache Spark,Scala Collections,我有一个类似于[(Int,Iterable[Int])](键值格式)的RDD。我想把价值部分展平 我试过: rdd.productIterator.foreach(x => x._2.productIterator.foreach(print)) 但是,我得到以下错误: 错误:值_2不是任何 rdd.productIterator.foreach(x=>x._2.productIterator.foreach(打印)) 扁平化价值部分应通过以下方式解决: rdd.flatMap(x =

我有一个类似于
[(Int,Iterable[Int])]
(键值格式)的RDD。我想把价值部分展平

我试过:

rdd.productIterator.foreach(x =>  x._2.productIterator.foreach(print))
但是,我得到以下错误:

错误:值_2不是任何 rdd.productIterator.foreach(x=>x._2.productIterator.foreach(打印))


扁平化价值部分应通过以下方式解决:

rdd.flatMap(x => x._2.map(y => y)).foreach(println)
如果您希望使用每个iterable值将
键展平
,那么下面应该可以解决您的问题

rdd.flatMap(x => x._2.map(y => (x._1, y))).foreach(println)

看起来您需要
flatMapValues

val rdd: RDD[(Int, Iterable[Int])] = sc.parallelize(Seq((1, Seq(1, 2, 3))))
rdd.flatMapValues(idenitity)