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

时间:2017-07-25 06:31:52

标签: java maven avro

如何在@SpringBootApplication(scanBasePackages = {"fr.example.spring.clipperton.clippertondao"}) @EntityScan(basePackages = "fr.example.spring.clipperton.clippertondao.domain") @Import({DataSourceConfiguration.class, SessionFactoryConfiguration.class}) public class ClippertonStandaloneApplication { ... } 课程中生成BigDecimal?现在我所拥有的只是Apple ....

使用Avro Schema(avsc):

ByteBuffer

使用IDL:

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

使用maven生成方法@namespace("com.example") protocol AppleProtocol { record Apple { @java-class("java.math.BigDecimal") decimal(9,2) price; } } 和以下maven片段:

mvn clean compile

这两件事都回归了这个丑陋的方法,显然几乎无法使用......

           <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; } ? 这是使用Avro BigDecimal

1 个答案:

答案 0 :(得分:4)

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