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

2024-09-30 14:18:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新手。我试过在struct内部爆炸array。JSON循环有点复杂,如下所示。在

{
"id": 1,
"firstfield": "abc",
"secondfield": "zxc",
"firststruct": {
    "secondstruct": {
        "firstarray": [{
            "firstarrayfirstfield": "asd",
            "firstarraysecondfield": "dasd",
            "secondarray": [{
                "score": " 7 "
            }]
        }]
    }
}

}

我试图访问secondarray字段下的score字段,以便能够计算很少的指标并得出每个id的平均分数。在


Tags: idjsonarraystructscoreabc新手zxc
1条回答
网友
1楼 · 发布于 2024-09-30 14:18:57

如果使用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")

相关问题 更多 >