我是 spark 的新手。我试过在 struct 中分解一个 array。 JSON 循环有点复杂,如下所示。
{
"id": 1,
"firstfield": "abc",
"secondfield": "zxc",
"firststruct": {
"secondstruct": {
"firstarray": [{
"firstarrayfirstfield": "asd",
"firstarraysecondfield": "dasd",
"secondarray": [{
"score": " 7 "
}]
}]
}
}
我正在尝试访问 secondarray 字段下的 score 字段,以便能够计算一些指标并得出每个 id< 的平均分数.
请您参考如下方法:
如果您使用的是 Glue,那么您应该将 DynamicFrame 转换为 Spark 的 DataFrame,然后使用 explode功能:
from pyspark.sql.functions import col, explode
scoresDf = dynamicFrame.toDF
.withColumn("firstExplode", explode(col("firststruct.secondstruct.firstarray")))
.withColumn("secondExplode", explode(col("firstExplode.secondarray")))
.select("secondExplode.score")
scoresDyf = DynamicFrame.fromDF(scoresDf, glueContext, "scoresDyf")
