python - 如何在pyspark /中的结构内爆炸结构中的内部数组
问题描述
我是新来的火花。我试过在一个array
内部爆炸struct
。JSON 循环有点复杂,如下所示。
{
"id": 1,
"firstfield": "abc",
"secondfield": "zxc",
"firststruct": {
"secondstruct": {
"firstarray": [{
"firstarrayfirstfield": "asd",
"firstarraysecondfield": "dasd",
"secondarray": [{
"score": " 7 "
}]
}]
}
}
}
我正在尝试访问score
字段下的secondarray
字段,以便能够计算一些指标并得出每个指标的平均分数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")
推荐阅读
- python - 如何在烧瓶中闪烁用户会话已过期的消息
- laravel - 在 Laravel 中使用两个数据库时密码验证失败
- apache-flink - Flink 水印没有在 Python 中推进,卡在 -9223372036854775808
- android - 当用户切换到 Android Studio Kotlin 中的其他页面时,如何停止播放音乐?
- reactjs - 无法在反应 js 应用程序中应用 Bootstrap5 粘性页脚模板
- reactjs - 未处理的拒绝(TypeError):这是未定义的
- android - 从包含另一个图像的小部件渲染图像
- go - 用于双向流的 GRPC 刷新令牌
- reactjs - 使用 Three.js 模拟通过管道的水流
- php - 根据 shapfile 数据创建热图