gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:50:08 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com