PySpark:获取特定RDD分区的元素

时间:2016-12-12 18:23:09

标签: python apache-spark pyspark apache-spark-sql spark-dataframe

我正在尝试打印/获取特定分区的元素。在this question上,我发现了一种使用此代码在Scala中执行此操作的优雅方法:

distData.mapPartitionsWithIndex( (index: Int, it: Iterator[Int]) =>it.toList.map(x => if (index ==5) {println(x)}).iterator).collect

我正在努力将其转换为Python,你能不能帮助我。

P.S:另外,与上述解决方案不同,我只想采取分区的前5个元素,而不是全部打印。

1 个答案:

答案 0 :(得分:1)

你可以:

from itertools import islice

rdd.mapPartitions(lambda it: islice(it, 0, 5))

rdd.mapPartitionsWithIndex(lambda i, it: islice(it, 0, 5) if i == x else [])