【问题标题】:Avro java serialization as json not working correctlyAvro java序列化为json无法正常工作
【发布时间】:2016-05-24 05:33:33
【问题描述】:

我有一个简单的 avro 架构,我使用 avro-maven-plugin 从中生成了一个 java 类。

avro架构如下:

{
    "type": "record",
    "name": "addressGeo",
    "namespace": "com.mycompany",
    "doc": "Best record address and list of geos",
    "fields": [

    {
        "name": "version",
        "type": "int",
        "default": 1,
        "doc": "version the class"
    },
    {
        "name": "eventType",
        "type": "string",
        "default": "addressGeo",
        "doc": "event type"
    },
    {
        "name": "parcelId",
        "type": "long",
        "doc": "ParcelID of the parcel. Join parcelid and sequence with ParcelInfo"
    },
    {
        "name": "geoCodes",
        "type": {"type": "array", "items": "com.mycompany.geoCode"},
        "doc": "Multiple Geocodes, with restrictions information"
    },
    {
        "name": "brfAddress",
        "type": ["null", "com.mycompany.address"],
        "doc": "Address cleansed version of BRF"
    }
    ]
}

如果我使用构建器构造一个简单的对象,并使用 json 对其进行序列化,我会得到以下输出:

{
  "version": 1,
  "eventType": {
    "bytes": [
      97,
      100,
      100,
      114,
      101,
      115,
      115,
      71,
      101,
      111
    ],
    "length": 10,
    "string": null
  },
  "parcelId": 1,
  "geoCodes": [
    {
      "version": 1,
      "latitude": 1,
      "longitude": 1,
      "geoQualityCode": "g",
      "geoSourceTypeID": 1,
      "restrictions": "NONE"
    }
  ],
  "brfAddress": {
    "version": 1,
    "houseNumber": "1",
    "houseNumberFraction": null,
    "streetDirectionPrefix": null,
    "streetName": "main",
    "streetSuffix": "street",
    "streetDirectionSuffix": null,
    "fullStreetAddress": "1 main street, seattle, wa, 98101",
    "unitPrefix": null,
    "unitNumber": null,
    "city": "seattle",
    "state": "wa",
    "zipCode": "98101",
    "zipPlusFour": null,
    "addressDPV": "Y",
    "addressQualityCode": "good",
    "buildingNumber": "1",
    "carrierRoute": "t",
    "censusTract": "c",
    "censusTractAndBlock": "b",
    "dataCleanerTypeID": 1,
    "restrictions": "NONE"
  }
}

注意 eventType 字段的输出。它以字节数组的形式出现,而字段的类型是 CharSequence。

知道为什么序列化会这样做吗?它适用于其他类型的字符串。

我正在使用 google-gson 将对象序列化为 json。

【问题讨论】:

    标签: avro


    【解决方案1】:

    您可能正在使用旧版本的 avro,它使用 CharSequence。理想情况下,字符串类型应该是 java String 类型。我建议更新 avro 版本或看看这个 - Apache Avro: map uses CharSequence as key

    【讨论】:

    • 我想我使用的是最新的,但我会仔细检查。谢谢!
    • 不,不是版本问题。我正在使用 avro v 1.8.0,这是根据此链接的最新版本:search.maven.org/…
    • 另外,这个问题只发生在默认字符串字段上。所有其他字段都正确序列化为字符串,即使它们在 java 类中被声明为字符序列。
    • 我拿走了你的 avro(删除了 geoCodes 和 brfAddress)并通过 maven 生成了 java 类,得到了它作为字符串。
    • 没关系...您附加的链接有一个解决方法。我需要在 maven 插件配置中设置 String 让它使用 java.lang.String 而不是 java.lang.CharSequence 作为字符串类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2011-06-01
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 2020-12-14
    相关资源
    最近更新 更多