【问题标题】:not able to get nested json value as column无法获取嵌套的 json 值作为列
【发布时间】:2023-01-11 01:20:54
【问题描述】:

我正在尝试为 json 创建模式,并将其视为数据框中的列

输入json

{"place":{"place_name":"NYC","lon":0,"lat":0,"place_id":1009}, "region":{"region_issues":[{"key":"health","issue_name":"Cancer"},{"key":"sports","issue_name":"swimming"}}}

代码

  val schemaRsvp =  new StructType()
      .add("place",  StructType(Array(
      StructField("place_name", DataTypes.StringType),
      StructField("lon", DataTypes.IntegerType),
      StructField("lat", DataTypes.IntegerType),
      StructField("place_id", DataTypes.IntegerType))))

 val ip =  spark.read.schema(schemaRsvp).json("D:\\Data\\rsvp\\inputrsvp.json")
 ip.show()

它在单列place 中显示所有字段,希望按列显示值

place_name,lon,lat,place_id
NYC,0,0,1009

任何建议,如何解决这个问题?

【问题讨论】:

    标签: scala apache-spark


    【解决方案1】:

    您可以使用“.*”将结构类型转换为列

    ip.select("place.*").show()
    
    +----------+---+---+--------+
    |place_name|lon|lat|place_id|
    +----------+---+---+--------+
    |       NYC|  0|  0|    1009|
    +----------+---+---+--------+
    

    更新:

    使用新的列数组,您可以展开日期,然后执行相同的“.*”将结构类型转换为列:

    ip.select(col("place"), explode(col("region.region_issues")).as("region_issues"))
      .select("place.*", "region_issues.*").show(false)
    
    +---+---+--------+----------+----------+------+
    |lat|lon|place_id|place_name|issue_name|key   |
    +---+---+--------+----------+----------+------+
    |0  |0  |1009    |NYC       |Cancer    |health|
    |0  |0  |1009    |NYC       |swimming  |sports|
    +---+---+--------+----------+----------+------+
    

    【讨论】:

    • 谢谢,稍微更新了我的 json,它也有 region_issues 元素的值数组,我怎样才能得到这些值呢?
    • @OxanaGrey 我更新了我的答案
    猜你喜欢
    • 2020-02-18
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    相关资源
    最近更新 更多