Json 在火花中变平

Json 在火花中变平,json,scala,apache-spark,apache-spark-sql,Json,Scala,Apache Spark,Apache Spark Sql,我在spark中有以下数据帧: val test = sqlContext.read.json(path = "/path/to/jsonfiles/*") test.printSchema root |-- properties: struct (nullable = true) | |-- prop_1: string (nullable = true) | |-- prop_2: string (nullable = true) | |-- prop_3: b

我在spark中有以下数据帧:

val test = sqlContext.read.json(path = "/path/to/jsonfiles/*")  
test.printSchema
root
 |-- properties: struct (nullable = true)
 |    |-- prop_1: string (nullable = true)
 |    |-- prop_2: string (nullable = true)
 |    |-- prop_3: boolean (nullable = true)
 |    |-- prop_4: long (nullable = true)
...
我想做的是展平此数据帧,以便
属性1。。。属性存在于顶层。即

test.printSchema
root
|-- prop_1: string (nullable = true)
|-- prop_2: string (nullable = true)
|-- prop_3: boolean (nullable = true)
|-- prop_4: long (nullable = true)
...
类似的问题有几种解决方案。我能找到的最好的就是摆姿势。但是,仅当
属性
的类型为
数组
时,解决方案才有效。在我的例子中,属性的类型是
StructType

另一种方法类似于:

test.registerTempTable("test")
val test2 = sqlContext.sql("""SELECT properties.prop_1, ... FROM test""")
但在这种情况下,我必须明确指定每一行,这是不雅观的


解决这个问题的最好办法是什么

如果您不是在寻找递归解决方案,那么在1.6+点语法中使用star应该可以很好地工作:

val df = sqlContext.read.json(sc.parallelize(Seq(
  """{"properties": {
       "prop1": "foo", "prop2": "bar", "prop3": true, "prop4": 1}}"""
)))

df.select($"properties.*").printSchema
// root
//  |-- prop1: string (nullable = true)
//  |-- prop2: string (nullable = true)
//  |-- prop3: boolean (nullable = true)
//  |-- prop4: long (nullable = true)
不幸的是,这在1.5及之前的版本中不起作用


在这种情况下,您可以直接从模式中提取所需的信息。您将看到一个示例,其中应易于调整以适应此场景,另一个示例(Python中的递归模式扁平化)

这回答了你的问题吗?