【问题标题】:DataType.fromJson() Error: java.lang.IllegalArgumentException: Failed to convert the JSON string 'int' to a data typeDataType.fromJson() 错误:java.lang.IllegalArgumentException:无法将 JSON 字符串“int”转换为数据类型
【发布时间】:2021-10-29 04:49:11
【问题描述】:

尝试从 JSON 文件创建 StructType 以用作创建数据框的架构。

JSON 结构:

    {
  "type" : "struct",
  "fields" : [
    {
    "name" : "LocationID",
    "type" : "int",
    "nullable" : false,
    "metadata" : { }
  }, {
    "name" : "Boroght",
    "type" : "string",
    "nullable" : true,
    "metadata" : { }
  }, {
    "name" : "zone",
    "type" : "string",
    "nullable" : true,
    "metadata" : { }
  }, {
    "name" : "serviceZone",
    "type" : "string",
    "nullable" : true,
    "metadata" : { }
  } ]
}

读取 JSON 文件并创建 StructType:

val schemaSource =  Source.fromFile(url.getFile).mkString
val schemaFromJson = DataType.fromJson(schemaSource).asInstanceOf[StructType]

我不断收到错误:

java.lang.IllegalArgumentException: Failed to convert the JSON string 'int' to a data type.

不确定是 JSON 结构还是要在那里更改什么。

【问题讨论】:

  • 类型应该是integer而不是int

标签: json scala apache-spark


【解决方案1】:

LocationID 的类型应该是 integer 而不是 int

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    相关资源
    最近更新 更多