我有如下数据框,如果有人可以帮助我以不同的格式获得输出,请感谢。
输入:
|customerId|transHeader|transLine|
|1001 |1001aa |1001aa1 |
|1001 |1001aa |1001aa2 |
|1001 |1001aa |1001aa3 |
|1001 |1001aa |1001aa4 |
|1002 |1002bb |1002bb1 |
|1002 |1002bb |1002bb2 |
|1002 |1002bb |1002bb3 |
|1002 |1002bb |1002bb4 |
|1003 |1003cc |1003cc1 |
|1003 |1003cc |1003cc2 |
|1003 |1003cc |1003cc3 |
+----------+-----------+---------+
预期的输出集1:
customerId headerLineMapGroup
1001 Map(1001aa -> (1001aa1, 1001aa2, 1001aa3, 1001aa4))
1002 Map(1002bb -> (1002bb1, 1002bb2, 1002bb3, 1002bb4))
1003 Map(1003cc -> (1003cc1, 1003cc2, 1003cc3))
预期的输出集2:
customerId headerLineListOfMapGroup
1001 List[ Map(1001aa -> 1001aa1), Map(1001aa ->1001aa2), Map(1001aa ->1001aa3), Map(1001aa ->1001aa4) ]
1002 List[ Map(1002bb -> 1002bb1), Map(1002bb -> 1002bb2), Map(1002bb -> 1002bb3), Map(1002bb -> 1002bb4)]
1003 List[ Map(1003cc -> 1003cc1), Map(1003cc ->1003cc2), Map(1003cc ->1003cc3) ]
答案 0 :(得分:0)
以下是使用udf的解决方案。
val spark = SparkSession
.builder()
.master("local")
.appName("ParquetAppendMode")
.getOrCreate()
import spark.implicits._
val data = spark.sparkContext.parallelize(Seq(
(1001, "1001aa","1001aa1"),
(1001, "1001aa","1001aa2"),
(1001, "1001aa","1001aa3")
)).toDF("customerId", "transHeader", "transLine")
val toMap = udf((header: String, line: Seq[String]) => {
Map(header -> line)
})
val toMapList = udf((header: String, line: Seq[String]) => {
line.map(l => Map(header -> l)).toList
})
val grouped = data.groupBy("customerId", "transHeader").agg(collect_list("transLine").alias("transLine"))
grouped.withColumn("headerLineMapGroup", toMap($"transHeader", $"transLine"))
.drop("transHeader", "transLine")
.show(false)
grouped.withColumn("headerLineMapGroupList", toMapList($"transHeader", $"transLine"))
.drop("transHeader", "transLine")
.show(false)
希望这有帮助!