小编Dra*_*ick的帖子

Spark from_json - StructType和ArrayType

我有一个以XML形式出现的数据集,其中一个节点包含JSON.Spark正在将其作为StringType读取,因此我尝试使用from_json()将JSON转换为DataFrame.

我能够转换一串JSON,但是如何编写模式以使用数组呢?

没有数组的字符串 - 工作得很好

import org.apache.spark.sql.functions._

val schemaExample = new StructType()
          .add("FirstName", StringType)
          .add("Surname", StringType)

val dfExample = spark.sql("""select "{ \"FirstName\":\"Johnny\", \"Surname\":\"Boy\" }" as theJson""")

val dfICanWorkWith = dfExample.select(from_json($"theJson", schemaExample))

dfICanWorkWith.collect()

// Results \\
res19: Array[org.apache.spark.sql.Row] = Array([[Johnny,Boy]])
Run Code Online (Sandbox Code Playgroud)

带有数组的字符串 - 无法解决这个问题

import org.apache.spark.sql.functions._

val schemaExample2 = new StructType()
                              .add("", ArrayType(new StructType()
                                                          .add("FirstName", StringType)
                                                          .add("Surname", StringType)
                                                )
                                  )

val dfExample2= spark.sql("""select "[{ \"FirstName\":\"Johnny\", \"Surname\":\"Boy\" }, { \"FirstName\":\"Franky\", \"Surname\":\"Man\" }" as theJson""")

val dfICanWorkWith = dfExample2.select(from_json($"theJson", schemaExample2))

dfICanWorkWith.collect()

// Result \\ …
Run Code Online (Sandbox Code Playgroud)

json scala apache-spark apache-spark-sql

2
推荐指数
1
解决办法
6804
查看次数

标签 统计

apache-spark ×1

apache-spark-sql ×1

json ×1

scala ×1