【发布时间】:2019-03-01 01:53:47
【问题描述】:
输入:
id1 id2 name value epid
"xxx" "yyy" "EAN" "5057723043" "1299"
"xxx" "yyy" "MPN" "EVBD" "1299"
我想要:
{ "id1": "xxx",
"id2": "yyy",
"item_specifics": [
{
"name": "EAN",
"value": "5057723043"
},
{
"name": "MPN",
"value": "EVBD"
},
{
"name": "EPID",
"value": "1299"
}
]
}
我从How to aggregate columns into json array?和how to merge rows into column of spark dataframe as vaild json to write it in mysql尝试了以下两种解决方案:
pi_df.groupBy(col("id1"), col("id2"))
//.agg(collect_list(to_json(struct(col("name"), col("value"))).alias("item_specifics"))) // => not working
.agg(collect_list(struct(col("name"),col("value"))).alias("item_specifics"))
但我得到了:
{ "name":"EAN","value":"5057723043", "EPID": "1299", "id1": "xxx", "id2": "yyy" }
如何解决这个问题?谢谢
【问题讨论】:
-
能否添加示例输入?
-
输出是无效的 json,除非你把 xxx 和 yyy 用双引号括起来。
-
你用的spark版本是什么?
-
火花 2.3.1 .....
标签: scala apache-spark apache-spark-sql