首页 > 解决方案 > 使用 avro-maven-plugin 1.9.0 生成具有十进制数据类型的类

问题描述

我有一个 Avro 架构,其中一些字段定义为十进制逻辑类型。从该架构中,我使用 avro-maven-plugin(使用 1.9.0 版)生成类。我想避免生成 ByteBuffer 类型并改用 BigDecimal。

我在较早的问题(如这里)中发现使用 1.8.2 版。我试过了,然后升级到 1.9.0 仍然无法获得正确的数据类型。

架构:

{
            "name": "TUR_ID",
            "type": [
              "null",
              "bytes"
            ],
            "default": null,
            "logicalType": "decimal",
            "precision": 16,
            "scale": 0
}

pom.xml 中的插件配置:

            <plugin>
                <groupId>org.apache.avro</groupId>
                <artifactId>avro-maven-plugin</artifactId>
                <version>${avro.version}</version>
                <executions>
                    <execution>
                        <id>schemas</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>schema</goal>
                            <goal>protocol</goal>
                            <goal>idl-protocol</goal>
                        </goals>
                        <configuration>
                            <stringType>String</stringType>
                            <enableDecimalLogicalType>true</enableDecimalLogicalType>
                            <sourceDirectory>${project.basedir}/src/main/resources/</sourceDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

实际输出是@Deprecated public java.nio.ByteBuffer TUR_ID;,我想得到@Deprecated public java.math.BigDecimal TUR_ID;.

标签: javamavenavroauto-generate

解决方案


<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>
            <goal>idl-protocol</goal>
        </goals>
        <configuration>
         <enableDecimalLogicalType>true</enableDecimalLogicalType>
        </configuration>
    </execution>
</executions>


推荐阅读