Decimal Datatype Support in Avro Schema and Generated Files

与世无争的帅哥 提交于 2019-12-11 07:28:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!