【发布时间】:2019-04-04 04:19:55
【问题描述】:
例如,我有一张表,其架构如下:
id int,
name string,
score int
我想转换成
id int,
attribute struct<name string,score int>
【问题讨论】:
标签: apache-spark dictionary dataset row
例如,我有一张表,其架构如下:
id int,
name string,
score int
我想转换成
id int,
attribute struct<name string,score int>
【问题讨论】:
标签: apache-spark dictionary dataset row
你可以这样写:
你需要这样写UDF:
val toArray = udf((value1 : String, value2: String) => List(value1,value2))
你的逻辑应该是这样的:
val df1 = df.withColumn("attribute",toArray(dff.col("name"),dff.col("score")))
df1.select("id","attribute").show()
【讨论】:
toArray 带有不定参数或任何类型怎么办?
可以使用函数“struct”:
val originalDF = List((1, "any", 10)).toDF("id", "name", "score")
val transformed = originalDF.select($"id", struct($"name", $"score").alias("attribute"))
transformed.printSchema()
输出:
root
|-- id: integer (nullable = false)
|-- attribute: struct (nullable = false)
| |-- name: string (nullable = true)
| |-- score: integer (nullable = false)
【讨论】: