Apache spark Spark数据集。数组中的组结构列
我想合并两个json文件 它们看起来像这样: 要素:Apache spark Spark数据集。数组中的组结构列,apache-spark,dataframe,grouping,collapse,Apache Spark,Dataframe,Grouping,Collapse,我想合并两个json文件 它们看起来像这样: 要素: { "id" : 1, "style": { "availableColors": [1,3,5,8] "material" : "Iron" . . . } . . . } 颜色: { "colorId" : 1, "definition": { "R": 13, "G" : 23, "B" : 4
{
"id" : 1,
"style": {
"availableColors": [1,3,5,8]
"material" : "Iron"
.
.
.
}
.
.
.
}
颜色:
{
"colorId" : 1,
"definition": {
"R": 13,
"G" : 23,
"B" : 4
}
"tags": ["...", "..."]
.
.
.
}
我在DF中转换了每一个,我加入他们如下:
val joined = elemDF.join(colorDF, $"style.id".cast("string").contains($"colorId"))
但在这种情况下,每个id的条目数与colorID的条目数相同
我如何在一个数组中对颜色进行分组,这样最终就像在每个元素中添加一个颜色结构数组一样
也许使用udf而不是加入数据帧
我希望避免使用类来表示行,因为json可以有许多字段和级别,我现在不应该关心这些。终于找到了一个解决方案。我会加上它,以防有人有同样的问题
def toColumns(colNames: Array[String]) = {
colNames.map(x => col(x))
}
val joined = elemDF.join(colorDF, $"style.id".cast("string").contains($"colorId"))
.withColumn("color",
struct(toColumns(colorDF.schema.fieldNames): _*)
joined.groupBy(toColumns(elemDF.schema.fieldNames): _*)
.agg(collect_list($"color").as("colors"))