java - 使用数组类型的记录进行嵌套 Avro 记录反序列化

标签 java apache-kafka deserialization avro

我正在编写代码来反序列化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"
 }
 ]
}

最佳答案

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

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 - 使用数组类型的记录进行嵌套 Avro 记录反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60812141/

相关文章:

java - JPA持久化 CrudRepository.save() 语法错误

Java 垃圾收集器和字符串操作

java - Spring Kafka-ConfigException : Invalid value TopicNameStrategy for configuration key. subject.name.strategy:找不到类 TopicNameStrategy

json - 在 Golang 中反序列化模式字段

c# - Unity Json反/序列化嵌套数据

java - 从 Sybase 插入/检索文件

java - for 循环中的构造函数

apache-kafka - 当生产者在 Kafka 中引发 NotEnoughReplicas 时

apache-kafka - Kafka Connect - 通过 JMX 的指标

java - 可序列化对象内的静态方法调用