Apache Avro Maven插件生成UUID字段而不是字符串

问题描述 投票:2回答:1

给出以下Avro架构:

{
    "namespace": "ro.dspr.coreentities",
    "type": "record",
    "name": "Organization",
    "fields": [
      {
        "name": "id",
        "type": "string",
        "logicalType": "uuid"
      },
      {
        "name": "name",
        "type": "string"
      },
      {
        "name": "description",
        "type": "string"
      }
    ]
}

正在运行avro-maven-plugin 1.9.0目标模式,我得到:

@org.apache.avro.specific.AvroGenerated
public class Organization extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {

  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"Organization\",\"namespace\":\"ro.dspr.coreentities\",\"fields\":[{\"name\":\"id\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"},\"logicalType\":\"uuid\"},{\"name\":\"name\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},{\"name\":\"description\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}}]}");

  // truncated 

  @Deprecated public java.lang.String id;
  @Deprecated public java.lang.String name;
  @Deprecated public java.lang.String description;

  // truncated
}

我希望为组织生成的POJO具有id UUID,而不是String(我现在拥有的)。

我看过的链接:

我确实看到了logical type def from Avro,实际上是我要尝试触发的Conversion class,但我无法连接圆点。

其他

相关的Maven pom零件

<plugin>
  <groupId>org.apache.avro</groupId>
  <artifactId>avro-maven-plugin</artifactId>
  <version>${avro.version}</version>
  <configuration>
    <sourceDirectory>${avro-files-path}</sourceDirectory>
    <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
    <stringType>String</stringType>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>schema</goal>
      </goals>
      <configuration>
        <sourceDirectory>${avro-files-path}</sourceDirectory>
        <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

更多信息

我实际上试图使用Avro为Kafka消息提供结构。我也在使用Confluent Schema Registry和Confluent Avro Kafka Serializer。不过,我以为我只将id作为String,但是如果我尝试将消息作为非UUID的形式发送到Kafka,它将在以后失败。但是,我发现实际上没有任何限制,因此我设法将任何String发送给Kafka。因此,Avro中的“ logicalType”根本没有强制执行。

问题

  1. 如何生成Organization.class#id作为UUID?
  2. 如果没有Avro支持,那么解决方法是什么(最好重用org.apache.avro.Conversions.UUIDConversion)?
java maven code-generation avro
1个回答
0
投票

这里是一个功能示例:

模式:

{
  "name": "pk",
  "type": {"type": "string", "logicalType": "uuid"}
},

Maven配置:

            <plugin>
                <groupId>org.apache.avro</groupId>
                <artifactId>avro-maven-plugin</artifactId>
                <version>1.9.1</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>schema</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>${project.basedir}/../avro/schemas/commands</sourceDirectory>
                            <outputDirectory>${project.build.directory}/generated-sources/java</outputDirectory>
                            <!-- TODO Enable this string type once we have 1.10.0 release, or 1.9.2-->
                            <!--<stringType>String</stringType>-->
                            <fieldVisibility>PRIVATE</fieldVisibility>
                            <customConversions>org.apache.avro.Conversions$UUIDConversion</customConversions>
                            <imports>
                                <import>${project.basedir}/../avro/schemas/common</import>
                                <import>${project.basedir}/../avro/schemas/commands/account</import>
                                <import>${project.basedir}/../avro/schemas/commands/rec</import>
                            </imports>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

要注意的重要一点是,在当前版本的插件中,UUIDConversion和String之间存在冲突,因此,您需要选择哪个更重要,或者使用已合并了修复程序的1.10.0-SNAPSHOT ,但尚未发布。

© www.soinside.com 2019 - 2024. All rights reserved.