我从CSV创建了rdd lines = sc.textFile(data)现在我需要将行转换为键值rdd,其中value将是字符串(拆分后),key将是csv的列数,例如CSV
| 第1栏|列2|
| - -|- -|
| 七十三|小行星230666|
| 五十五|小行星149610|
我想得到rdd.take(1):【(1,73),(2,230666)】
I create rdd of lists
lines_of_list = lines_data.map(lambda line : line.split(','))
I create function that get list and return list of tuples (key, value)
def list_of_tuple (l):
list_tup = []
for i in range(len(l[0])):
list_tup.append((l[0][i],i))
return(list_tup)
But I can’t get the correct result when I try to map this function on RDD
2条答案
按热度按时间lpwwtiir1#
你可以使用PySpark的
create_map
函数来实现,如下所示:如果你仍然想使用RDD API,那么它是DataFrame的一个属性,所以你可以这样使用它:
tjjdgumg2#
我以下列方式修正问题: