问题
This question concerns Avro version 1.8.1.
We have the following field in our AVRO schema:
"name" : "sale_price",
"type" : ["bytes", "null"],
"logicalType": "decimal",
"precision": 18,
"scale": 17,
As you can see, the logicalType of the field has been defined as decimal
.
But when we use the avro-maven-plugin
, it does not generate the correct data type within the generated Java source files.
Instead it generates, java.nio.ByteBuffer
.
How would you have the correct data type generated within the Java files?
This is our plugin configuration:
<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>
回答1:
If you want BigDecimal you need to use version 1.8.2 and add enableDecimalLogicalType
parameter with true
value to your pom file:
<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>
For more info, check this issue: https://issues.apache.org/jira/browse/AVRO-1847
回答2:
As the Avro docs explain,
Language implementations must ignore unknown logical types when reading, and should use the underlying Avro type.
A decimal logical type annotates Avro bytes or fixed types.
Hence in the generated Java files, the decimal logical type is represented in the underlying Avro type bytes, i.e., java.nio.ByteBuffer
来源:https://stackoverflow.com/questions/43068048/decimal-datatype-support-in-avro-schema-and-generated-files