【问题标题】:Explode struct column which isn't array in Pyspark在 Pyspark 中爆炸不是数组的结构列
【发布时间】:2022-01-24 01:14:21
【问题描述】:

我得到了一个结构列(来自 json)与这个架构:

struct_col_name
 |    |-- 2053771759: struct (nullable = true)
 |    |    |-- col1: long (nullable = true)
 |    |    |-- col2: string (nullable = true)
 |    |    |-- col3: long (nullable = true)
 |    |    |-- col4: string (nullable = true)
 |    |-- 2053771760: struct (nullable = true)
 |    |    |-- col1: long (nullable = true)
 |    |    |-- col2: string (nullable = true)
 |    |    |-- col3: long (nullable = true)
 |    |    |-- col4: string (nullable = true)
 |    |-- 2053771761: struct (nullable = true)
 |    |    |-- col1: long (nullable = true)
 |    |    |-- col2: string (nullable = true)
 |    |    |-- col3: long (nullable = true)
 |    |    |-- col4: string (nullable = true)

由于所有内部 strcuts 都具有相同的字段,我想将架构转换为类似的内容,并将id(例如2053771759)添加为每个元素的字段。

通过这样做,我将能够将列分解为行。

struct_col_name
 |    |-- element: struct: struct (nullable = true)
 |    |    |    |-- col1: long (nullable = true)
 |    |    |    |-- col2: string (nullable = true)
 |    |    |    |-- col3: long (nullable = true)
 |    |    |    |-- col4: string (nullable = true)
 |    |    |    |-- id: long (nullable = true)

知道我该怎么做吗?还是用其他方式炸开柱子?

【问题讨论】:

    标签: python apache-spark pyspark apache-spark-sql


    【解决方案1】:

    首先,您可以使用df.select("struct_col_name.*") 数据框的架构来获取IDs 的列表。然后,您可以遍历该列表以更新每个 通过将id 字段添加到现有字段并创建结构列数组来构建结构。最后,分解数组列,逐行得到一个结构体。

    类似这样的:

    from pyspark.sql import functions as F
    
    inner_fields = ["col1", "col2", "col3", "col4"]
    ids = df.select("struct_col_name.*").columns
    
    df = df.select(
        F.explode(F.array(*[
            F.struct(*[
                F.col(f"struct_col_name.{i}.{c}") for c in inner_fields
            ], F.lit(i).alias(i))
            for i in ids
        ])).alias("struct_col_name")
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 2021-11-09
      相关资源
      最近更新 更多