use*_*400 3 arrays json apache-spark
如何在 Spark 2.2.0 中将字符串数组展平为数据帧的多行?
输入行["foo", "bar"]
val inputDS = Seq("""["foo", "bar"]""").toDF
Run Code Online (Sandbox Code Playgroud)
inputDS.printSchema()
root
|-- value: string (nullable = true)
Run Code Online (Sandbox Code Playgroud)
输入数据集inputDS
inputDS.show(false)
value
-----
["foo", "bar"]
Run Code Online (Sandbox Code Playgroud)
预期输出数据集outputDS
value
-------
"foo" |
"bar" |
Run Code Online (Sandbox Code Playgroud)
我尝试了explode如下所示的功能,但效果不佳
inputDS.select(explode(from_json(col("value"), ArrayType(StringType))))
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
org.apache.spark.sql.AnalysisException: cannot resolve 'jsontostructs(`value`)' due to data type mismatch: Input schema string must be a struct or an array of structs
Run Code Online (Sandbox Code Playgroud)
还尝试了以下方法
inputDS.select(explode(col("value")))
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
org.apache.spark.sql.AnalysisException: cannot resolve 'explode(`value`)' due to data type mismatch: input to function explode should be array or map type, not StringType
Run Code Online (Sandbox Code Playgroud)
抛出异常的是:
from_json(col("value"), ArrayType(StringType))
Run Code Online (Sandbox Code Playgroud)
不explode,具体来说:
输入架构数组必须是结构体或结构体数组。
你可以:
inputDS.selectExpr(
"split(substring(value, 2, length(value) - 2), ',\\s+') as value")
Run Code Online (Sandbox Code Playgroud)
和explode输出。
| 归档时间: |
|
| 查看次数: |
7971 次 |
| 最近记录: |