【发布时间】:2021-07-04 07:49:26
【问题描述】:
我正在尝试使用 pyspark 访问以下 json 的内部属性
[
{
"432": [
{
"atttr1": null,
"atttr2": "7DG6",
"id":432,
"score": 100
}
]
},
{
"238": [
{
"atttr1": null,
"atttr2": "7SS8",
"id":432,
"score": 100
}
]
}
]
在输出中,我正在寻找类似下面的 csv 格式的内容 atttr1, atttr2,id,分数 空,"7DG6",432,100 null,"7SS8",238,100
我知道我可以像下面这样获得这些详细信息,但我不想在 lambda 表达式中传递 432 或 238,因为在更大的 json 中这(斜体)会有所不同。我想遍历所有可用的值。
print(inputDF.rdd.map(lambda x:(x['*432*'])).first())
print(inputDF.rdd.map(lambda x:(x['*238*'])).first())
我也尝试注册一个名为“test”的临时表,但它给出了消息元素错误。_id 不存在。
inputDF.registerTempTable("test")
srdd2 = spark.sql("select element._id from test limit 1")
我们将不胜感激任何帮助。我正在使用火花 2.4
【问题讨论】:
-
什么是
peopleDF?你能显示peopleDF.show()的输出吗? -
那是输入 df。将其重命名。输出 .show() 也是 +--------------------+-------- + | 238| 432| +--------------------+--------+ |空|[[, 7DG6, 432, 100]]| |[[, 7SS8, 432, 100]]|空| +--------------------+--------+
标签: python json apache-spark pyspark