Apache Spark concatenate multiple rows into list i

2019-09-22 03:49发布

This question already has an answer here:

I need to create a table(hive table/spark dataframe) from a source table that stores data of users in multiple rows into list in single row.

User table:
Schema:  userid: string | transactiondate:string | charges: string |events:array<struct<name:string,value:string>> 
----|------------|-------| ---------------------------------------
123 | 2017-09-01 | 20.00 | [{"name":"chargeperiod","value":"this"}]
123 | 2017-09-01 | 30.00 | [{"name":"chargeperiod","value":"last"}]
123 | 2017-09-01 | 20.00 | [{"name":"chargeperiod","value":"recent"}]
123 | 2017-09-01 | 30.00 | [{"name":"chargeperiod","value":"0"}]
456 | 2017-09-01 | 20.00 | [{"name":"chargeperiod","value":"this"}]
456 | 2017-09-01 | 30.00 | [{"name":"chargeperiod","value":"last"}]
456 | 2017-09-01 | 20.00 | [{"name":"chargeperiod","value":"recent"}]
456 | 2017-09-01 | 30.00 | [{"name":"chargeperiod","value":"0"}]

Output table should be

userid:String | concatenatedlist :List[Row]
-------|-----------------
123    | [[2017-09-01,20.00,[{"name":"chargeperiod","value":"this"}]],[2017-09-01,30.00,[{"name":"chargeperiod","value":"last"}]],[2017-09-01,20.00,[{"name":"chargeperiod","value":"recent"}]], [2017-09-01,30.00, [{"name":"chargeperiod","value":"0"}]]]
456    | [[2017-09-01,20.00,[{"name":"chargeperiod","value":"this"}]],[2017-09-01,30.00,[{"name":"chargeperiod","value":"last"}]],[2017-09-01,20.00,[{"name":"chargeperiod","value":"recent"}]], [2017-09-01,30.00, [{"name":"chargeperiod","value":"0"}]]]

Spark version: 1.6.2

1条回答
孤傲高冷的网名
2楼-- · 2019-09-22 04:15
val rdd = sc.parallelize(Seq(("1","2017-02-01","20.00","abc"),("1","2017-02-01","30.00","abc2"),("2","2017-02-01","20.00","abc"),("2","2017-02-01","30.00","abc")))
val df = rdd.toDF("id","date","amt","array")
df.withColumn("new",concat_ws(",",$"date",$"amt",$"array")).select("id","new").groupBy("id").agg(concat_ws(",",collect_list("new")))
查看更多
登录 后发表回答