我有一个嵌套json的api数据集。但它没有模式。以下是示例:
{
"meta": {
"hits": 20600
},
"data": {
"2020-05-01": {
"value": 20448,
"data": {
"neutral": {
"value": 11019
},
"positive": {
"value": 5866
},
"negative": {
"value": 3423
},
"unassigned": {
"value": 140
}
}
},
"2020-05-02": {
"value": 152,
"data": {
"positive": {
"value": 73
},
"negative": {
"value": 42
},
"neutral": {
"value": 37
}
}
}
}
}
我正在使用pyspark,我能够读取数据并创建Dataframe。但我正在努力定义模式和派生列。
理想情况下,我希望有3列的表:
事件日期
事件类型
事件编号
我将输出保存为json文件,然后创建dataframe
data_json = spark.read.json(json_file,multiLine=True)
display(data_json )
print(data_json .show())
它打印2列 data
以及 meta
. 我想 explode
列 data
具有
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
explodeArrarDF = data_json.withColumn('Exp_RESULTS',F.explode(F.col('data'))).drop('data')
但它不起作用。错误:
cannot resolve 'explode(`data`)' due to data type mismatch: input to function explode should be array or map type, not struct<2020-05-01:struct<data:struct<negative:struct<value:bigint>,neutral:struct<value:bigint>,positive:struct<value:bigint>,unassigned:struct<value:bigint>>,value:bigint>,
我看到了一个用 schema
选择,但运气不好。
暂无答案!
目前还没有任何答案,快来回答吧!