json - 将 Spark 的数据框的 Json 列转换为对象数组
问题描述
我有一个带有 JSON 列的数据框。JSON 基本上包含键和值的数组,如下例所示。
Col1
=====================================================================
|{“Name”:”Ram”,”Place”:”RamGarh”} |
|{“Name”:”Lakshman”,”Place”:”LakshManPur”.”DepartMent”:”Operations”} |
|{“Name”:”Sita”,”Place”:”SitaPur”,”Experience”,”14”} |
我需要解析这个 JSON 数据。什么应该是最有效的方法?
我需要呈现它的形式
case class dfCol(col:String, valu:String)
所以基本上我需要解析该数据帧每一行的json并转换为形式
| Col
| ==========================================================
| Array(dfCol(Name,Ram),dfCOl(Place,Ramgarh))
| Array(dfCol(Name,Lakshman),dfCOl(Place,LakshManPur),dfCOl(DepartMent,Operations))
| Array(dfCol(Name,Sita),dfCOl(Place,SitaPur),dfCOl(Experience,14))
解决方案
用这个 -
case class dfCol(col:String, valu:String)
加载提供的测试数据
val data =
"""
|{"Name":"Ram","Place":"RamGarh"}
|{"Name":"Lakshman","Place":"LakshManPur","DepartMent":"Operations"}
|{"Name":"Sita","Place":"SitaPur","Experience":14.0}
""".stripMargin
val df = spark.read.json(data.split(System.lineSeparator()).toSeq.toDS())
df.show(false)
df.printSchema()
/**
* +----------+----------+--------+-----------+
* |DepartMent|Experience|Name |Place |
* +----------+----------+--------+-----------+
* |null |null |Ram |RamGarh |
* |Operations|null |Lakshman|LakshManPur|
* |null |14.0 |Sita |SitaPur |
* +----------+----------+--------+-----------+
*
* root
* |-- DepartMent: string (nullable = true)
* |-- Experience: double (nullable = true)
* |-- Name: string (nullable = true)
* |-- Place: string (nullable = true)
*/
转变Row -> Array[dfCol]
val ds: Dataset[Array[dfCol]] = df.map(row => {
row.getValuesMap[String](row.schema.map(_.name))
.filter(_._2 != null)
.map{f => dfCol(f._1, String.valueOf(f._2))}
.toArray
})
ds.show(false)
ds.printSchema()
// +------------------------------------------------------------------+
//|value |
//+------------------------------------------------------------------+
//|[[Name, Ram], [Place, RamGarh]] |
//|[[DepartMent, Operations], [Name, Lakshman], [Place, LakshManPur]]|
//|[[Experience, 14.0], [Name, Sita], [Place, SitaPur]] |
//+------------------------------------------------------------------+
//
//root
// |-- value: array (nullable = true)
// | |-- element: struct (containsNull = true)
// | | |-- col: string (nullable = true)
// | | |-- valu: string (nullable = true)
推荐阅读
- php - 如何在 apache 服务器上增加 max_upload_size
- java - 使用 Map 反序列化 Avro 中的对象
字段返回错误类的值 - python-3.x - 遍历列表并查询值
- c# - 不要在 TagHelper ASP.NET Core 中使用引导样式
- cloudify - 如何在 Cloudify 中正确地自动扩展虚拟机组?
- java - 是否可以使用 Spring Session 和 Spring Boot 将会话存储在两个不同的商店中?
- php - Wordpress async_upload.php 500 错误上传较大的文件时互联网连接速度慢和延迟
- django - 创建具有深度选项支持的相同类型的嵌套序列化程序
- go - 无法使用 CGO AMD=> ARM 与 Xgo 交叉编译 Go 项目
- blockchain - 如何获取存储在区块链中的数据,然后显示在网页上?