【问题标题】:Pyspark collect_list weird behaviorPyspark collect_list 奇怪的行为
【发布时间】: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


【解决方案1】:

我在 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 已经解决了这个问题。

【讨论】:

    猜你喜欢
    • 2015-07-06
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多