spark从json JDBC列中获取值



在mysql jdbc数据源中,用于将数据加载到Spark中,有一个列包含JSON字符串。

// JDBC Connection and load table in Dataframe
val verDf = spark.read.format("jdbc").option("driver", driver).option("url", url).option("dbtable", verticesTable).option("user", user).option("password", pass).load()
verDf.printSchema
root
|-- id: integer (nullable = true)
|-- url: string (nullable = true)
|-- al: string (nullable = true) -->> this is JSON string
|-- batch_id: integer (nullable = true)
|-- x: double (nullable = true)
|-- y: double (nullable = true)
|-- z: double (nullable = true)
|-- size: double (nullable = true)

JSON在所有列中,只需要单个值。我如何提取它?我见过from_json/get_json_schema方法,它看起来既昂贵又笨重——schema应该先创建,然后将JSON解包裹成Map等。

val schema = schema_of_json(lit(verDf.select($"al").as[String].first))

所以当我在上面运行这一行时,它会超时或运行大约几分钟(4-8分钟)。

  • 我不明白为什么-它应该只是第一行和解析Json来生成它的模式。为什么它这么长(json值有大约1-2千字节,真的很小的对象)?
  • 是否有机会使用一些类似于mysql的功能json_extract()提取相对较快的值从JSON字符串?

函数get_json_object完全按照要求工作-它接受2个参数-列名和路径:

val newsh=spark.sql("select get_json_object(verDf.al,'$.key1.paramName') from table where table.key=value")

最新更新