2017-07-25 108 views
1

如何在我的Apple類中生成BigDecimal?現在我只有一個ByteBuffer ....Avro 1.8.2 BigDecimal(邏輯類型)的Java代碼生成

使用的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 snippet:

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

這兩樣東西都會返回這個醜陋的m ethod這顯然是勉強可用...

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

我怎樣才能得到這種方法要求一個BigDecimal? 這是使用阿夫羅1.8.2

回答

3

對於所生成的類來表示十進制邏輯類型與BigDecimal的代替的ByteBuffer,設置 的阿夫羅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> 
+0

謝謝!爲什麼這不是默認行爲? – Stephane