Python 3.x 如何使用pyspark.resultiterable.resultiterable对象
我有一对rdd结构的1TB记录,我想按键对所有记录进行分组,然后只对值应用函数 我的代码如下:Python 3.x 如何使用pyspark.resultiterable.resultiterable对象,python-3.x,pyspark,apache-spark-2.0,Python 3.x,Pyspark,Apache Spark 2.0,我有一对rdd结构的1TB记录,我想按键对所有记录进行分组,然后只对值应用函数 我的代码如下: rdd = sc.textFile("path").map(lambdal:l.split(";")) rdd_pair=rdd.map(lambda a: (a[0], a)) rdd_pair.take(3) #output: [('id_client', ('id_client','time','city')] #[('1', [('1', '2013/03/12 23:59:59', 'Lon
rdd = sc.textFile("path").map(lambdal:l.split(";"))
rdd_pair=rdd.map(lambda a: (a[0], a))
rdd_pair.take(3)
#output: [('id_client', ('id_client','time','city')]
#[('1', [('1', '2013/03/12 23:59:59', 'London')]
#[('1', [('1', '2013/12/03 10:43:12', 'Rome')]
#[('1', [('1', '2013/05/01 00:09:59', 'Madrid')]
我想按id_客户端对所有记录进行分组,然后将函数矩阵仅应用于值。对于每个键,函数按“时间”对元组列表进行排序,然后提取从一个城市到另一个城市的转换
grouped=rdd_pair.groupByKey(200)
grouped.take(1)
#output [("1",<pyspark.resultiterable.ResultIterable object at 0x7fc659e0a210)]
def matrix(input):
output=[]
input_bag= sorted(input, key=lambda x: x[1], reverse=False)
loc0 = input_bag[0]
for loc in input_bag[1:]:
output.append((loc0[2],loc[2]))
loc0 = loc
return output
transition=grouped.mapValues(lambda k: matrix(k)).filter(lambda l: l[1]!=[])
我遇到一个Python错误:列表索引超出范围错误
有人能帮我吗?谢谢我这样决定:
def matrix(input):
output=[]
input2=[i[0] for i in input]
input_bag= sorted(input2, key=lambda x: x[1], reverse=False)
loc0 = input_bag[0]
for loc in input_bag[1:]:
output.append((loc0[2],loc[2]))
loc0 = loc
return output
在使用Python-in-bulit函数“sorted”之前,我在input2(元组列表)中转换输入(一个iterable对象)您可以添加一些示例输入、所需的输出,并更清楚地描述您尝试执行的操作吗?函数
matrix()
应该返回什么?请阅读这篇文章,并尝试提供一个。我认为数据格式不正确,一些属性丢失。唯一可能导致此错误的行是loc0=input\u bag[0]
所有其他(loc0,loc,x)都是元组。
def matrix(input):
output=[]
input2=[i[0] for i in input]
input_bag= sorted(input2, key=lambda x: x[1], reverse=False)
loc0 = input_bag[0]
for loc in input_bag[1:]:
output.append((loc0[2],loc[2]))
loc0 = loc
return output