【问题标题】:Avro 1.8.2 Java code generation for BigDecimal (Logical Type)Avro 1.8.2 Java 代码生成 BigDecimal(逻辑类型)
【发布时间】:2017-07-25 06:31:52
【问题描述】:

如何在我的Apple 类中生成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 sn -p:

           <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>

这两个东西都返回了这个显然几乎无法使用的丑陋方法......

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

我怎样才能让这个方法请求BigDecimal? 这是使用 Avro 1.8.2

【问题讨论】:

    标签: java maven avro


    【解决方案1】:

    为使生成的类用 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>
    

    【讨论】:

    • 谢谢!为什么这不是默认行为?
    猜你喜欢
    • 2017-08-22
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    相关资源
    最近更新 更多