gpt4 book ai didi

scala - 如何在RDD中展平列表?

转载 作者:行者123 更新时间:2023-12-03 23:17:23 25 4
gpt4 key购买 nike

是否可以在 RDD 中展平列表?例如转换:

 val xxx: org.apache.spark.rdd.RDD[List[Foo]]

到:
 val yyy: org.apache.spark.rdd.RDD[Foo]

这该怎么做?

最佳答案

val rdd = sc.parallelize(Array(List(1,2,3), List(4,5,6), List(7,8,9), List(10, 11, 12)))
// org.apache.spark.rdd.RDD[List[Int]] = ParallelCollectionRDD ...

val rddi = rdd.flatMap(list => list)
// rddi: org.apache.spark.rdd.RDD[Int] = FlatMappedRDD ...

// which is same as rdd.flatMap(identity)
// identity is a method defined in Predef object.
// def identity[A](x: A): A

rddi.collect()
// res2: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

关于scala - 如何在RDD中展平列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28233405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com