Python 为Spark RDD中的元素生成/插入连续数字
假设我有一个RDD,由Python 为Spark RDD中的元素生成/插入连续数字,python,apache-spark,pyspark,rdd,Python,Apache Spark,Pyspark,Rdd,假设我有一个RDD,由 lines=sc.textFile('/test.txt') RDD就像['apple','orange','banana']。然后我想生成RDD[(0,'apple'),(1,'orange'),(2,'banana')] 我知道这可以通过indexed_lines=lines.zipWithIndex().map(lambda(x,y):','.join([str(y),x]).collect()来实现。 但是现在我有了另一个RDDnew_line=['菠萝','bl
lines=sc.textFile('/test.txt')
RDD就像['apple','orange','banana']
。然后我想生成RDD[(0,'apple'),(1,'orange'),(2,'banana')]
我知道这可以通过indexed_lines=lines.zipWithIndex().map(lambda(x,y):','.join([str(y),x]).collect()来实现。
但是现在我有了另一个RDDnew_line=['菠萝','blueberry']
,我想union
这两个RDD(索引_line和new_line)来构建[(0,'apple'),(1,'orange'),(2,'banana'),(3,'菠萝'),(4,'blueberry')]
请注意,索引行已经存在,我不想更改其中的数据
我正在尝试zip和union
RDDs
索引=sc.parallelize(范围(3,5))
新建索引行=新建行。zip(索引)
但它打破了这个zip
转换
知道它为什么会坏吗?有没有更聪明的方法
谢谢。像这样的怎么样
offset = lines.count()
new_indexed_lines = (new_lines
.zipWithIndex()
.map(lambda xi: (xi[1] + offset, xi[0])))