使用引用其他 Avro 模式的 Avro 模式在 Java 中生成特定记录 class

Using Avro Schemas that refer other Avro Schema to generate specific record class in Java

我有 2 个模式,即 LogLine 和 User 模式

{
  "namespace": "com.sample.log",
  "type": "record",
  "name": "User",
  "fields": [
    {"name": "username", "type": "string"},
    {"name": "user_type", "type": "string"},
  ]
}
  
{
  "namespace": "com.sample.log",
  "type": "record",
  "name": "LogLine",
  "fields": [
    {"name": "ip", "type": "string"},
    {"name": "timestamp",  "type": "long"},
    {"name": "url",  "type": "string"},
    {"name": "referrer",  "type": "string"},
    {"name": "useragent",  "type": "string"},
    {"name": "sessionid",  "type": ["null","int"], "default": null}
    {"name": "user",  "type": ["null","user"], "default": null}
  ]
}

我已经定义了 Logline 架构,因此我需要引用另一个架构“用户”架构。我需要使用 Avro Maven 插件生成特定记录 class。

当我尝试做同样的事情时出现错误

无法执行目标 org.apache.avro:avro-maven-plugin:1.8.2:schema(默认)项目日志记录:目标的执行默认值 org.apache.avro:avro- maven-plugin:1.8.2:schema failed Undefined Name "User"

maven 插件不做交叉引用(它不能保证文件处理顺序知道其他定义存在)。此外,您希望联合类型实际上是 com.sample.log.User.

根据我的经验,如果您想嵌套模式,使用 AVDL 定义会更容易。

因为我使用的是 Avro Maven 插件,所以我需要先导入 User 模式,这是 Logline 模式的依赖项。下面我在导入部分添加了 import unser,它对我有用。

         <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/resources/typedmessages/</sourceDirectory>
                        <imports>
                            <import>${project.basedir}/src/main/resources/typedmessages/user.avsc</import>
                        </imports>
                        <includes>
                            <include>*.avsc</include>
                        </includes>
                        <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

可以在博客中找到更多详细信息: https://feitam.es/use-of-avro-maven-plugin-with-complex-schemas-defined-in-several-files-to-be-reused-in-different-typed-messages/