如何在 pyspark/ 中的结构内分解结构中的内部数组



我是火花的新手。我试过在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,然后使用爆炸函数:

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")

最新更新