gpt4 book ai didi

scala - Apache Spark - Scala - 如何将 FlatMap (k, {v1,v2,v3,...}) 到 ((k,v1),(k,v2),(k,v3),...)

转载 作者:行者123 更新时间:2023-12-01 10:13:45 27 4
gpt4 key购买 nike

我明白了:

val vector: RDD[(String, Array[String])] = [("a", {v1,v2,..}),("b", {u1,u2,..})]

想转换成:

RDD[(String, String)] = [("a",v1), ("a",v2), ..., ("b",u1), ("b",u2), ...]

知道如何使用 flatMap 做到这一点。

最佳答案

这个:

vector.flatMap { case (x, arr) => arr.map((x, _)) }

会给你:

scala> val vector = sc.parallelize(Vector(("a", Array("b", "c")), ("b", Array("d", "f"))))
vector: org.apache.spark.rdd.RDD[(String, Array[String])] =
ParallelCollectionRDD[3] at parallelize at <console>:27


scala> vector.flatMap { case (x, arr) => arr.map((x, _)) }.collect
res4: Array[(String, String)] = Array((a,b), (a,c), (b,d), (b,f))

关于scala - Apache Spark - Scala - 如何将 FlatMap (k, {v1,v2,v3,...}) 到 ((k,v1),(k,v2),(k,v3),...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38462402/

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