【发布时间】:2019-07-05 23:28:20
【问题描述】:
我有以下几种情况:
case class attribute(key:String,value:String)
case class entity(id:String,attr:List[attribute])
val entities = List(entity("1",List(attribute("name","sasha"),attribute("home","del"))),
entity("2",List(attribute("home","hyd"))))
val df = entities.toDF()
// df.show
+---+--------------------+
| id| attr|
+---+--------------------+
| 1|[[name,sasha], [d...|
| 2| [[home,hyd]]|
+---+--------------------+
//df.printSchema
root
|-- id: string (nullable = true)
|-- attr: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- key: string (nullable = true)
| | |-- value: string (nullable = true)
我想要制作的是
+---+--------------------+-------+
| id| name | home |
+---+--------------------+-------+
| 1| sasha |del |
| 2| null |hyd |
+---+--------------------+-------+
我该怎么做。我在堆栈上查看了很多类似的问题,但找不到任何有用的东西。
我的主要动机是对不同的属性进行 groupBy,因此希望将其带入上述格式。
我研究了爆炸功能。它将列表分解为单独的行,我不希望这样。我想从attribute 的数组中创建更多列。
我发现了类似的东西:
Spark - convert Map to a single-row DataFrame
【问题讨论】:
标签: scala apache-spark dataframe