我有来自任何restapi的多个json,但我不知道其架构。我无法使用数据框的爆炸功能,因为我不知道由spark api创建的列名。
1.Can we store the keys of the nested arrays elements keys by decoding values from dataframe.schema.fields
, As spark only provides the value part in the rows of the dataframe and take the top level key as column name.
数据框-
+--------------------+
| stackoverflow|
+--------------------+
|[[[Martin Odersky...|
+--------------------+
是否有任何最佳方法可以通过在运行时确定架构来使用数据框方法来展平json。
样本Json-:
{
"stackoverflow": [{
"tag": {
"id": 1,
"name": "scala",
"author": "Martin Odersky",
"frameworks": [
{
"id": 1,
"name": "Play Framework"
},
{
"id": 2,
"name": "Akka Framework"
}
]
}
},
{
"tag": {
"id": 2,
"name": "java",
"author": "James Gosling",
"frameworks": [
{
"id": 1,
"name": "Apache Tomcat"
},
{
"id": 2,
"name": "Spring Boot"
}
]
}
}
]
}
注意-我们需要在dataframe中进行所有操作,因为有大量数据即将到来,因此我们无法解析每个json。
Created helper function & You can directly call
df.explodeColumns
on DataFrame.下面的代码将展平多级数组和结构类型列。