Spark: Expansion of RDD(Key, List) to RDD(Key, Value)

梦想与她 提交于 2019-12-18 06:48:09

问题


So I have an RDD of something like this

RDD[(Int, List)]]

Where a single element in the RDD looks like

(1, List(1, 2, 3))

My question is how can I expand the key value pair to something like this

(1,1)
(1,2)
(1,3)

Thank you


回答1:


rdd.flatMap { case (key, values) => values.map((key, _)) }




回答2:


And in Python (based on @seanowen's answer):

rdd.flatMap(lambda x: map(lambda e: (x[0], e), x[1]))


来源:https://stackoverflow.com/questions/36392938/spark-expansion-of-rddkey-list-to-rddkey-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!