给出以下Avro架构:
echo "pinkUnicorn" >> file
运行avro-maven-plugin 1.9.0目标 schema ,我得到:
{
"namespace": "ro.dspr.coreentities",
"type": "record",
"name": "Organization",
"fields": [
{
"name": "id",
"type": "string",
"logicalType": "uuid"
},
{
"name": "name",
"type": "string"
},
{
"name": "description",
"type": "string"
}
]
}
我希望为组织生成的POJO具有 id UUID,而不是String(我现在拥有的)。
我确实看到了logical type def from Avro,实际上是我要触发的Conversion class,但是我无法连接点。
相关的Maven pom零件
@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
}
我实际上试图使用Avro为Kafka消息提供结构。我还使用了Confluent Schema Registry和Confluent Avro Kafka Serializer。不过,我以为我只会将 id 作为字符串,但是如果我尝试将消息作为非UUID的形式发送到Kafka,它将在以后失败。但是,我发现实际上没有任何约束,并且设法将任何String发送给Kafka。因此,Avro中的“ logicalType”根本没有实施。
答案 0 :(得分:3)
这是一个实用的示例:
模式:
{
"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合并,但尚未发布。