如何在 pyspark/ 中的结构内分解结构中的内部数组
How to explode inner arrays in a struct inside a struct in pyspark/
我是 spark 的新手。我试过在 struct
内炸开 array
。 JSON 循环有点复杂,如下所示。
{
"id": 1,
"firstfield": "abc",
"secondfield": "zxc",
"firststruct": {
"secondstruct": {
"firstarray": [{
"firstarrayfirstfield": "asd",
"firstarraysecondfield": "dasd",
"secondarray": [{
"score": " 7 "
}]
}]
}
}
}
我正在尝试访问 secondarray
字段下的 score
字段,以便能够计算一些指标并得出每个 id
.[=16 的平均分数=]
如果您使用的是 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")
我是 spark 的新手。我试过在 struct
内炸开 array
。 JSON 循环有点复杂,如下所示。
{
"id": 1,
"firstfield": "abc",
"secondfield": "zxc",
"firststruct": {
"secondstruct": {
"firstarray": [{
"firstarrayfirstfield": "asd",
"firstarraysecondfield": "dasd",
"secondarray": [{
"score": " 7 "
}]
}]
}
}
}
我正在尝试访问 secondarray
字段下的 score
字段,以便能够计算一些指标并得出每个 id
.[=16 的平均分数=]
如果您使用的是 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")