Ich habe folgenden Datenrahmen in Funken:Elegante Json in Spark-abflachen
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)
...
Was würde ich tun möchte, ist diesen Datenrahmen glättet, so dass die prop_1 ... prop_n
auf der obersten Ebene existiert. I.e.
test.printSchema
root
|-- prop_1: string (nullable = true)
|-- prop_2: string (nullable = true)
|-- prop_3: boolean (nullable = true)
|-- prop_4: long (nullable = true)
...
Es gibt mehrere Lösungen für ähnliche Probleme. Das Beste, was ich finden kann, ist here gestellt. Die Lösung funktioniert jedoch nur, wenn properties
vom Typ Array
ist. In meinem Fall sind die Eigenschaften vom Typ StructType
.
Ein alternativer Ansatz wäre so etwas wie:
test.registerTempTable("test")
val test2 = sqlContext.sql("""SELECT properties.prop_1, ... FROM test""")
Aber in diesem Fall habe ich jede Zeile explizit angeben, und das ist unelegant.
Was ist der beste Weg, um dieses Problem zu lösen?