【发布时间】:2018-04-10 12:13:40
【问题描述】:
我遇到了 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)
我想将我的 dict 列表聚合成一个 dict 列表。
如果我在 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)])]
列表存储在错误的级别。 是什么导致了这个问题?
【问题讨论】:
-
你想要的输出是什么?显示您的 HIVE 查询及其产生的输出。
标签: python pyspark pyspark-sql