我有一个具有以下模式的Dataframe:
root
|-- docnumber: string (nullable = true)
|-- event: struct (nullable = false)
| |-- data: struct (nullable = true)
|-- codevent: int (nullable = true)
我需要在里面加一列 event.data
模式如下:
root
|-- docnumber: string (nullable = true)
|-- event: struct (nullable = false)
| |-- data: struct (nullable = true)
|-- codevent: int (nullable = true)
|-- needtoaddit: int (nullable = true)
我尝试运行以下代码: dataframe.withColumn("event.data.needtoaddit", lit("added"))
但它不起作用,因为您正在添加一个名为的列 event.data.needtoaddit
,我已尝试运行此:
dataframe.withColumn(
"event",
struct(
$"event.*",
struct(
lit("added")
.as("needtoaddit")
)
.as("data")
)
但它会创建一个名为 event.data
我又遇到了一个问题,有人知道我该怎么做?
1条答案
按热度按时间x4shl7ld1#
你有点接近了。请尝试以下代码: