【问题标题】:Error in reading json file with schema in Scala在 Scala 中读取带有模式的 json 文件时出错
【发布时间】:2021-06-06 11:58:44
【问题描述】:

读取 ArrayType 值(phoneNumbers)时出错,没有 ArrayType 值,我可以读取其余值。

{
    "firstName": "Rack",
    "lastName": "Jackon",
    "gender": "man",
    "age": 24,
    "address": {
        "streetAddress": 126,
        "city": "San Jone",
        "state": "CA",
        "postalCode": 394221
    },
    "phoneNumbers": [
        { "type": "home", "number": 7383627627}
    ]
}

My schema ->
val schema=StructType(List(
      StructField("firstName",StringType),
      StructField("lastName",StringType),
      StructField("gender",StringType),
      StructField("age",IntegerType),
      StructField("address",StructType(List(
        StructField("streetAddress",StringType),
        StructField("city",StringType),
        StructField("state",StringType),
        StructField("postalCode",IntegerType)))),
      StructField("phoneNumbers",ArrayType(StructType(List(
      StructField("type",StringType),
      StructField("number",IntegerType))))),
    ))

json_df.selectExpr("firstName","lastName",
      "gender","age","address.streetAddress","address.city",
      "address.state","address.postalCode",
      "explode(phoneNumbers) as phone","phone.type","phone.number").drop("phone").show()

当我执行.show 时,它只显示列名而没有值,但是当我不使用“phoneNumbers”数组时,它可以正常工作。

【问题讨论】:

    标签: json scala apache-spark apache-spark-sql


    【解决方案1】:

    IntegerType代表4字节有符号整数,最大2147483647,不能保存电话号码。使用LongTypeStringType 作为电话号码。

    select 查询没有结果,因为您正在分解一个空的电话号码数组,该数组返回 0 行。该数组为空,因为电话号码无法保存在 IntegerType 列中。

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 2023-03-26
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多