在不同文件中引用 avro 模式不起作用 - avro 抛出错误
Referencing an avro schema in a different file does not work - avro throws an error
我有两个模式,
a.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 文件非常相似。
我有两个模式,
a.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 文件非常相似。