带记录类型的嵌套Avro记录反序列化

时间:2020-03-23 10:52:39

标签: java apache-kafka deserialization 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"
 }
 ]
}

1 个答案:

答案 0 :(得分:0)

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

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>

因此,只需在导入选项中导入所有通用模式即可。