[英]Referencing an avro schema in a different file does not work - avro throws an error
我有兩個模式,
一個.avsc
{
"namespace": "my.ns",
"type": "record",
"name": "A",
"fields": [
{
"name": "b",
"type": "my.ns.B"
}
]
}
b.avsc
{
"namespace": "my.ns",
"type": "record",
"name": "B",
"fields": [
]
}
這會引發異常:
[ERROR] Failed to execute goal org.apache.avro:avro-maven-plugin:1.10.2:schema (default) on project schema:
Execution default of goal org.apache.avro:avro-maven-plugin:1.10.2:schema failed:
"my.ns.B" is not a defined name. The type of the "b" field must be a defined name or a {"type": ...} expression. -> [Help 1]
如果我將它們都放在同一個文件中,它可以工作。 但必須先聲明B
[
{
"namespace": "my.ns",
"type": "record",
"name": "B",
"fields": [
]
},
{
"namespace": "my.ns",
"type": "record",
"name": "A",
"fields": [
{
"name": "b",
"type": "my.ns.B"
}
]
}
]
我的 maven 插件設置如下:
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>${avro.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/avro/
</sourceDirectory>
<outputDirectory>${project.basedir}/target/generated-sources/main/java/
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
這是預期的行為; 您不能創建交叉引用 AVSC 文件。
作為替代方案,我建議您使用 IDL 文件
更新 POM
<goals>
<goal>schema</goal>
<goal>idl-protocol</goal>
</goals>
創建一些records.idl
文件
@namespace("my.ns")
protocol MyProtocol {
record B {
}
record A {
B b;
}
}
生成的架構文件將與您定義的組合 AVSC 文件非常相似。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.