【问题标题】:Nested Avro record deserialization with array type of records具有数组类型记录的嵌套 Avro 记录反序列化
【发布时间】:2020-07-03 19:58:47
【问题描述】:

我正在编写代码来反序列化具有数组类型记录的 avro 嵌套记录,方法是将 POJO 用于数组类型记录并在主 POJO 类中作为列表调用以进行反序列化。但是我不明白如何使用多个模式来反序列化记录。

架构结构:

{
"type": "record",
"name": "MainSchemaName",
"version": "2",
"namespace": "com.cmain",
"doc": "AExample",
"fields": [
 {
  "name": "MainABC",
  "type": {
    "type": "array",
    "items": {
      "name": "ABCarr",
      "type": "record",
      "fields": [
        {
          "name": "prod1",
          "type": "double"
        },
        {
          "name": "prod2",
          "type": "string"
        }
      ]
    }
  }
 },
 {
  "name": "comnsu1",
  "type": "int"
 }
 ]
}

【问题讨论】:

  • 问题上的架构结构无效
  • 更新了我正在使用的架构

标签: java apache-kafka deserialization avro


【解决方案1】:

您需要为每条记录指定一个文件,如下所示:

ABCarr.avsc

{
      "name": "ABCarr",
      "namespace": "com.cmain",
      "type": "record",
      "fields": [
        {
          "name": "prod1",
          "type": "double"
        },
        {
          "name": "prod2",
          "type": "string"
        }
      ]
}

MainSchemaName.avsc

{
"type": "record",
"name": "MainSchemaName",
"version": "2",
"namespace": "com.cmain",
"doc": "AExample",
"fields": [
 {
  "name": "MainABC",
  "type": {
       "type": "array",
       "items": "com.cmain.ABCarr",
       "java-class": "java.util.List"
     }
 },
 {
  "name": "comnsu1",
  "type": "int"
 }
 ]
}

然后您应该配置 avro-maven-plugin 以构建其他人需要的架构(在您的情况下为 ABCarr)。假设您的 avro 架构文件位于 src/main/resources/schema/avro 路径上,您应该指定以下内容来构建 ABCarr,然后在 MainSchemaName 上使用它作为类型:

<plugin>
    <groupId>org.apache.avro</groupId>
    <artifactId>avro-maven-plugin</artifactId>
    <version>1.9.2</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>schema</goal>
            </goals>
            <configuration>
                <sourceDirectory>${project.basedir}/src/main/resources/schema/avro</sourceDirectory>
                <stringType>String</stringType>
                <createSetters>true</createSetters>
                <fieldVisibility>private</fieldVisibility>
                <imports>
                    <import>${project.basedir}/src/main/resources/schema/avro/ABCarr.avsc</import>
                </imports>
            </configuration>
        </execution>
    </executions>
</plugin>

所以只需在导入选项中导入所有常用架构

【讨论】:

  • 我做了同样的方法,并使用生成的java类进行反序列化。它正在抛出空指针异常
  • 你能分享更多细节和堆栈跟踪吗?
  • 如上所述,我首先构建了架构 ABCarr.avsc 观察到的实际结果:未为架构 ABCarr 创建 Java pojo 类 后来我使用 pom.xml 中提到的输出目录构建架构 ABCarr.avsc,然后使用 com.cmain.ABCarr 类型构建 MainSchemaName.avsc。构建失败并出现错误:架构失败:不支持类型 com.cmain.ABCarr。我的第二个问题是如何在 datumReader 中使用这个多重模式来反序列化 bytearray 记录?
  • 我更新了 MainSchemaName 上的数组定义。尝试使用此代码。关于反序列化,您应该使用包含其他作为参考的主类,在您的情况下是 MainSchemaName
  • 如果我们可以使用带有嵌套记录的单个 avro 模式进行反序列化,有什么办法吗??
猜你喜欢
  • 1970-01-01
  • 2021-05-31
  • 2021-12-24
  • 2014-01-28
  • 2020-07-07
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 2015-01-26
相关资源
最近更新 更多