这个问题涉及 Avro 1.8.1 版。
我们的 AVRO 架构中有以下字段:
"name" : "sale_price",
"type" : ["bytes", "null"],
"logicalType": "decimal",
"precision": 18,
"scale": 17,
Run Code Online (Sandbox Code Playgroud)
如您所见,该字段的 logicalType 已定义为decimal。
但是当我们使用 时avro-maven-plugin,它不会在生成的 Java 源文件中生成正确的数据类型。相反,它生成,java.nio.ByteBuffer。您将如何在 Java 文件中生成正确的数据类型?
这是我们的插件配置:
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.8.1</version>
<configuration>
<stringType>String</stringType>
<enableDecimalLogicalType>true</enableDecimalLogicalType>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
如果你想要 BigDecimal,你需要使用 1.8.2 版本并将enableDecimalLogicalType带true值的参数添加到你的 pom 文件中:
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-avro-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${basedir}/src/main/avro/</sourceDirectory>
<outputDirectory>${basedir}/generated-sources/main/java/</outputDirectory>
<enableDecimalLogicalType>true</enableDecimalLogicalType>
</configuration>
</execution>
</executions>
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看此问题:https : //issues.apache.org/jira/browse/AVRO-1847