【问题标题】:How to explode inner arrays in a struct inside a struct in pyspark/如何在pyspark /中的结构内爆炸结构中的内部数组
【发布时间】:2018-09-26 23:57:57
【问题描述】:

我是新来的火花。我曾尝试在struct 内爆炸array。 JSON 循环有点复杂,如下所示。

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

}

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

【问题讨论】:

    标签: python pyspark aws-glue


    【解决方案1】:

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

    【讨论】:

    • 18/09/27 13:43:20 信息 MemoryStore:MemoryStore 已清除 18/09/27 13:43:20 信息 BlockManager:BlockManager 已停止 18/09/27 13:43:20 信息 ShutdownHookManager : 称为 End of LogType:stderr 的关闭挂钩
    • 这是脚本在日志中返回的内容并打印模式
    • 我提供的代码不打印模式。如果您愿意,可以添加scoresDf.printSchema()。日志看起来不错
    • 很抱歉给您带来了困惑。我已经完成了这个 scoresDf.printSchema() ,其中包含打印模式
    • 如果删除.select("secondExplode.score"),它会打印什么吗?
    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多