Avro 1.8.2 Java BigDecimal(逻辑类型)的代码生成

Avro 1.8.2 Java code generation for BigDecimal (Logical Type)

如何在我的 Apple class 中生成 BigDecimal?现在我只有一个 ByteBuffer.....

使用 Avro 架构 (avsc):

{"namespace": "com.example",
  "type": "record",
  "name": "Apple",
  "fields": [
    {"name": "price",  "type": {
                                          "type": "bytes",
                                          "logicalType": "decimal",
                                          "precision": 9,
                                          "scale": 2
                                        }}
  ]
}

使用 IDL:

@namespace("com.example")
protocol AppleProtocol {
    record Apple {
        @java-class("java.math.BigDecimal") decimal(9,2) price;
    }
}

使用 maven 生成方法 mvn clean compile,以及以下 maven 片段:

           <plugin>
                <groupId>org.apache.avro</groupId>
                <artifactId>avro-maven-plugin</artifactId>
                <version>1.8.2</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>schema</goal>
                            <goal>idl-protocol</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>${project.basedir}/src/main/avro</sourceDirectory>
                            <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

这两个东西return这个明显勉强可用的丑陋方法...

public void setPrice(java.nio.ByteBuffer value) {
    this.price = value;
  }

如何使用此方法请求 BigDecimal? 这是使用 Avro 1.8.2

对于生成的类用BigDecimal而不是ByteBuffer表示十进制逻辑类型,设置 Avro Maven 插件配置参数 enableDecimalLogicalType 为 true.

<plugin>
    <groupId>org.apache.avro</groupId>
    <artifactId>avro-maven-plugin</artifactId>
    <version>1.8.2</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>schema</goal>
                <goal>idl-protocol</goal>
            </goals>
            <configuration>
                <enableDecimalLogicalType>true</enableDecimalLogicalType>
            </configuration>
        </execution>
    </executions>
</plugin>