[英]Pyspark collect_list weird behavior
我遇到了collect_list函数的异常行为。
这是我的代码:
sqlContext = HiveContext(sc)
test = sc.parallelize([("uid1", [{"A":"a_string","B":1,"C":10},{"A":"another_string", "B":2,"C":20}]),
("uid1", [{"A":"another_string","B":0,"C":5}, {"A":"last_string","B":3,"C":3}])])
schema = StructType([StructField("uid", StringType(), False),
StructField("an_array", ArrayType(StructType([StructField("A", StringType(), False),
StructField("B", IntegerType(), False),
StructField("C", IntegerType(), False)]), False), False)
])
df = sqlContext.createDataFrame(test, schema= schema)
df.registerTempTable("test_df")
print(sqlContext.sql("SELECT uid, collect_list(an_array) from test_df group by uid").head(1))
df.groupBy("uid").agg(collect_list(col("an_array"))).head(1)
我想将我的字典列表聚合到一个字典列表列表中。
如果我在Hive中运行查询,我会得到我想要的。 但是使用pyspark我使用的两种方法都让我感到有些奇怪:
[Row(uid='uid1', _c1=[Row(a='[a_string, 1, 10]', b=['another_string', 2, 20], c=None), Row(a='[another_string, 0, 5]', b=['last_string', 3, 3], c=None)])]
列表存储在错误的级别。 是什么引起了这个问题?
我在Spark 2.2和Spark 2.3上测试您的代码。 结果与您的结果不同,看起来还可以。
[Row(uid=u'uid1', collect_list(an_array)=[[Row(A=u'a_string', B=1, C=10), Row(A=u'another_string', B=2, C=20)], [Row(A=u'another_string', B=0, C=5), Row(A=u'last_string', B=3, C=3)]])]
与你的相比
[Row(uid='uid1', _c1=[Row(a='[a_string, 1, 10]', b=['another_string', 2, 20], c=None), Row(a='[another_string, 0, 5]', b=['last_string', 3, 3], c=None)])]
也许新版本的Spark已解决了该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.