khr*_*tal 1 scala apache-spark
是否可以将 StringType 列转换为 spark 数据框中的 ArrayType 列?
df.printSchema() 给这个
架构 ->
a: string(nullable= true)
现在我想将其转换为
a:数组(可为空=真)
正如Elisiah评论的那样,您必须拆分字符串。您可以使用 UDF:
df.printSchema
import org.apache.spark.sql.functions._
val toArray = udf[Array[String], String]( _.split(" "))
val featureDf = df
.withColumn("a", toArray(df("a")))
featureDF.printSchema
Run Code Online (Sandbox Code Playgroud)
给出输出:
root
|-- a: string (nullable = true)
root
|-- a: array (nullable = true)
| |-- element: string (containsNull = true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5044 次 |
| 最近记录: |