我正在学习 Spark,我有下面的 xml,我想从中读取 2 个值并创建两个不同的列
<appRoot>
<applist>
<app type="test">
<code>8.52544</code>
</app>
<app type="dev">
<code>8.52537</code>
</app>
</applist>
</appRoot>
我想
如果 type="test" 那么它应该在新列“app_test”中设置值(即 8.52544)和
如果 type="dev" 那么它应该在新列“app_dev”中设置值(即 8.52537)
我在下面试过
df.select(
functions.when($"applist.app._type" === "test", $"applist.app.code").as("app_test"),
functions.when($"applist.app._type" === "dev", $"applist.app.code").as("app_dev"))
但它返回
app_test with value [8.52544, 8.52537]
app_dev with value [8.52544, 8.52537]
在将值设置为列时如何区分?
更新:
val df = spark.read
.format("com.databricks.spark.xml")
.option("rootTag", "appRoot")
.option("rowTag", "applist")
.load("test.xml")
df.printSchema()
root
|-- app: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- _type: string (nullable = true)
| | |-- code: double (nullable = true)