替换深层嵌套架构中的值 Spark Dataframe

sai*_*eth 6 apache-spark apache-spark-sql pyspark

我是 pyspark 的新手。我试图了解如何访问具有多层嵌套结构和数组的镶木地板文件。我需要用 null 替换数据帧(具有嵌套模式)中的某些值,我已经看到这个解决方案它可以很好地处理结构,但不确定它如何处理数组。

我的架构是这样的

|-- unitOfMeasure: struct
|    |-- raw: struct
|    |    |-- id: string
|    |    |-- codingSystemId: string
|    |    |-- display: string
|    |-- standard: struct
|    |    |-- id: string
|    |    |-- codingSystemId: string
|-- Id: string
|-- actions: array
|    |-- element: struct
|    |    |-- action: string
|    |    |-- actionDate: string
|    |    |-- actor: struct
|    |    |    |-- actorId: string
|    |    |    |-- aliases: array
|    |    |    |    |-- element: struct
|    |    |    |    |    |-- value: string
|    |    |    |    |    |-- type: string
|    |    |    |    |    |-- assigningAuthority: string
|    |    |    |-- fullName: string
Run Code Online (Sandbox Code Playgroud)

我想做的是替换unitOfMeasure.raw.id为 null 并替换actions.element.action为 null 并actions.element.actor.aliases.element.value替换为 null 保持数据框的其余部分不变。

有什么办法可以实现这个目标吗?

bla*_*hop 6

对于数组列,与结构体字段相比有点复杂。一种选择是将数组分解为新列,以便您可以访问和更新嵌套结构。更新后,您必须重建初始数组列。

transform但我更喜欢使用Spark >=2.4 引入的高阶函数这是一个示例:

输入DF:

 |-- actions: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- action: string (nullable = true)
 |    |    |-- actionDate: string (nullable = true)
 |    |    |-- actor: struct (nullable = true)
 |    |    |    |-- actorId: long (nullable = true)
 |    |    |    |-- aliases: array (nullable = true)
 |    |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |    |-- assigningAuthority: string (nullable = true)
 |    |    |    |    |    |-- type: string (nullable = true)
 |    |    |    |    |    |-- value: string (nullable = true)
 |    |    |    |-- fullName: string (nullable = true)

+--------------------------------------------------------------+
|actions                                                       |
+--------------------------------------------------------------+
|[[action_name1, 2019-12-08, [2, [[aa, t1, v1]], full_name1]]] |
|[[action_name2, 2019-12-09, [3, [[aaa, t2, v2]], full_name2]]]|
+--------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

我们传递一个 lambda 函数来transfrom选择所有结构体字段并替换actions.actionand actions.actor.aliases.valueby null

transform_expr = """transform (actions, x -> 
                               struct(null as action, 
                                      x.actionDate as actionDate, 
                                      struct(x.actor.actorId as actorId, 
                                             transform(x.actor.aliases, y -> 
                                                       struct(null as value, 
                                                              y.type as type, 
                                                              y.assigningAuthority as assigningAuthority)
                                                       ) as aliases,
                                            x.actor.fullName as fullName
                                      ) as actor
                                ))"""

df.withColumn("actions", expr(transform_expr)).show(truncate=False)
Run Code Online (Sandbox Code Playgroud)

输出DF:

+------------------------------------------------+
|actions                                         |
+------------------------------------------------+
|[[, 2019-12-08, [2, [[, t1, aa]], full_name1]]] |
|[[, 2019-12-09, [3, [[, t2, aaa]], full_name2]]]|
+------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)