pyspark RDD zip、zipWithUniqueId、zipWithIndex操作详解

一、zip(other)
Zips this RDD with another one, returning key-value pairs with the first element in each RDD second element in each RDD, etc. Assumes that the two RDDs have the same number of partitions and the same number of elements in each partition (e.g. one was made through a map on the other).
两个RDDzip,返回k-v
前提:
两个RDD具有相同个数的分区,并且每个分区内的个数相等
例如:

例子:

x=sc.parallelize(range(5),2)
y=sc.parallelize(range(1000,1005),2)
a=x.zip(y).glom().collect()
print(a)
a=x.zip(y).collect()
print(a)
运行结果:
[[(0, 1000), (1, 1001)], [(2, 1002), (3, 1003), (4, 1004)]]
[(0, 1000), (1, 1001), (2, 1002), (3, 1003), (4, 1004)]

二、zipWithIndex()
Zips this RDD with its element indices.
The ordering is first based on the partition index and then the ordering of items within each partition. So the first item in the first partition gets index 0, and the last item in the last partition receives the largest index.
返回:k-v
与分区没有关系,v的值对应list下标值

例子:
a=sc.parallelize(list('abczyx'),1).zipWithIndex().glom().collect()
print(a)

a=sc.parallelize(list('abczyx'),2).zipWithIndex().glom().collect()
print(a)

a=sc.parallelize(list('abczyx'),6).zipWithIndex().glom().collect()
print(a)

运行结果

运行结果:

[[('a', 0), ('b', 1), ('c', 2), ('z', 3), ('y', 4), ('x', 5)]]
[[('a', 0), ('b', 1), ('c', 2)], [('z', 3), ('y', 4), ('x', 5)]]
[[('a', 0)], [('b', 1)], [('c', 2)], [('z', 3)], [('y', 4)], [('x', 5)]]

三、zipWithUniqueId

Zips this RDD with generated unique Long ids.

Items in the kth partition will get ids k, n+k, 2n+k, …, where n is the number of partitions. So there may exist gaps, but this method won’t trigger a spark job, which is different from zipWithIndex
返回k-v,与分区有关系
k, n+k, 2
n+k,
n为第几个分区
k为第n个分区的第k个值
从0开始计数

程序实例:一个分区

a=sc.parallelize(list('abczyx'),1).zipWithUniqueId().glom().collect()
print(a)

结果同zipWithIndex()

结果
[[('a', 0), ('b', 1), ('c', 2), ('z', 3), ('y', 4), ('x', 5)]]

二个分区

代码:
rdd=sc.parallelize(list('abczyx'),2)
print(rdd.glom().collect())
a=rdd.zipWithUniqueId().glom().collect()
print(a)

结果:

[['a', 'b', 'c'], ['z', 'y', 'x']]
[[('a', 0), ('b', 2), ('c', 4)], [('z', 1), ('y', 3), ('x', 5)]]

在这里插入图片描述
在这里插入图片描述

4个分区

rdd=sc.parallelize(list('45abczyx'),4)
print(rdd.glom().collect())
a=rdd.zipWithUniqueId().glom().collect()
print(a)
[['4', '5'], ['a', 'b'], ['c', 'z'], ['y', 'x']]
[[('4', 0), ('5', 4)], [('a', 1), ('b', 5)], [('c', 2), ('z', 6)], [('y', 3), ('x', 7)]]

分析:

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PySpark RDD是一种分布式的数据集,它是PySpark的核心抽象之一。RDD代表弹性分布式数据集(Resilient Distributed Dataset),它是由一系列分区组成的可并行处理的集合。RDD可以包含任何类型的对象,并且可以在集群上进行并行操作PySpark RDD可以通过不同的方式创建,其中一种常见的方式是使用`sc.parallelize`方法,该方法可以将Python列表、NumPy数组或Pandas Series/Pandas DataFrame转换为Spark RDD。例如,通过以下代码可以使用列表创建一个RDD: ```python rdd = sc.parallelize([1, 2, 3, 4, 5]) ``` 这将创建一个名为`rdd`的RDD对象,其中包含了列表中的元素。RDD支持各种转换和操作,例如映射、过滤、排序和聚合等。你可以使用这些操作来对RDD进行变换和计算,最终得到你想要的结果。 PySpark提供了丰富的文档来帮助你了解RDD的更多细节和使用方法。你可以参考Spark官方网站的RDD编程指南和PySpark官方文档,它们提供了详细的介绍和示例代码,帮助你更好地理解和使用PySpark RDD。 总结起来,PySpark RDD是一种分布式的可并行处理的数据集,它可以通过不同的方式创建,例如使用`sc.parallelize`方法。RDD支持各种转换和操作,它是PySpark中非常重要的概念之一。 参考文献: Spark官方网站 - RDD编程指南:http://spark.apache.org/docs/latest/rdd-programming-guide.html PySpark官方文档:https://spark.apache.org/docs/latest/api/python/index.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值