假设我有以下 xml 数据:
<students>
<studentId>110</studentId>
<info>
<rollNo>2</rollNo>
<address>
<permanent>abc</permanent>
<temporary>def</temporary>
</address>
</info>
<subjects>
<subject>
<name>maths</name>
<credit>3</credit>
</subject>
<subject>
<name>science</name>
<credit>2</credit>
</subject>
</subjects>
</students>
它的架构是:
root
|-- info: struct (nullable = true)
| |-- address: struct (nullable = true)
| | |-- permanent: string (nullable = true)
| | |-- temporary: string (nullable = true)
| |-- rollNo: long (nullable = true)
|-- studentId: long (nullable = true)
|-- subjects: struct (nullable = true)
| |-- subject: array (nullable = true)
| | |-- element: struct (containsNull = true)
| | | |-- credit: long (nullable = true)
| | | |-- name: string (nullable = true)
作为根标签"students"
。
在这里,我想更新某些列的值。
我想使用更新"studentId"
列的值UDF
。我找到了一种方法:
df = df.withColumn("studentId", updateValue(col("studentId")))
然后,我想更新一个嵌套列,即"info.rollNo"
. 应用上述过程给了我另一个新列“ <info.rollNo>updated_value</info.rollNo>
”。找了一阵子,找到了一个办法:
val colStruct = df.select(col("info" + ".*")).columns
.filter(_ != "rollNo")
.map(f => col("info" + "." + f))
df = df.withColumn("info",
struct(
(colStruct :+ updateValue(col("info.rollNo")
).as("rollNo")): _*)
)
对于第三个嵌套列,我尝试了上述方式。但我无法弄清楚这个过程。在这里,问题是,有人可以解释一下更新嵌套列值的算法,其嵌套级别可能是 3、4、5 等等。例如:我想更新以下字段。
"info.address.permanent"
哪个是结构,
"subjects.subject.credit"
哪个是数组的元素"subject"
PS:如果您知道任何其他更新某些列的方法,请提及。