簡體   English   中英

Avro - 如何為特定編譯器注冊自定義 LogicalType

[英]Avro - how to register custom LogicalType for SpecificCompiler

嘿 avro 用戶和專家,

我想使用 avro logicalTypes,意味着自己創建一些 - 不僅使用內置的。

問題是如何讓編譯器從模式生成代碼以使用我自己創建的代碼

我創建了我的架構(相關部分):

{
  "name": "street",
  "type": {
    "type": "string",
    "logicalType": "custom-street"
  },
  "doc": "Street format ending with house number"
}

(當然還有創建的類型和轉換,請參閱https://github.com/markush81/avro-examples

我現在不知道如何配置編譯器來使用它

我通過 gradle 插件使用編譯器(但我想這首先沒有任何區別)

plugins {
    id 'com.commercehub.gradle.plugin.avro' version '0.14.2'
}

avro {
    enableDecimalLogicalType = true //enable built-in decimal type
}

感謝您提供任何提示(或解決方法)。

PS:當然我知道如何操作生成的類來支持我的邏輯類型(參見: https : //github.com/markush81/avro-examples/tree/master/src/main/manual ),但這意味着我永遠無法從我的架構定義中重新編譯。

avro 代碼1.8.2 中搜索了很多之后,我得出的結論是,目前編譯器工具不支持用於代碼生成的自定義邏輯類型。

如果我們看一下record.vm速度模板

#if ($this.hasLogicalTypeField($schema))
    protected static final org.apache.avro.data.TimeConversions.DateConversion DATE_CONVERSION = new org.apache.avro.data.TimeConversions.DateConversion();
    protected static final org.apache.avro.data.TimeConversions.TimeConversion TIME_CONVERSION = new org.apache.avro.data.TimeConversions.TimeConversion();
    protected static final org.apache.avro.data.TimeConversions.TimestampConversion TIMESTAMP_CONVERSION = new org.apache.avro.data.TimeConversions.TimestampConversion();
    protected static final org.apache.avro.Conversions.DecimalConversion DECIMAL_CONVERSION = new org.apache.avro.Conversions.DecimalConversion();

    private static final org.apache.avro.Conversion<?>[] conversions = new org.apache.avro.Conversion<?>[] {
    #foreach ($field in $schema.getFields())
        ${this.conversionInstance($field.schema())},
    #end
       null
    };

    @Override
    public org.apache.avro.Conversion<?> getConversion(int field) {
        return conversions[field];
    }
#end

只添加了四個轉換,這些轉換由 avro 本身提供。

另一塊要查看的是org.apache.avro.compiler.specific.SpecificCompiler

  public String conversionInstance(Schema schema) {
      if (schema == null || schema.getLogicalType() == null) {
           return "null";
      }

      if (LogicalTypes.date().equals(schema.getLogicalType())) {
           return "DATE_CONVERSION";
      } else if (LogicalTypes.timeMillis().equals(schema.getLogicalType())) {
           return "TIME_CONVERSION";
      } else if (LogicalTypes.timestampMillis().equals(schema.getLogicalType())) {
           return "TIMESTAMP_CONVERSION";
      } else if (LogicalTypes.Decimal.class.equals(schema.getLogicalType().getClass())) {
           return enableDecimalLogicalType ? "DECIMAL_CONVERSION" : "null";
      }

      return "null";
}

也沒有可以添加自定義邏輯類型的部分。

從 Avro 1.9.x 開始,現在可以注冊自定義logicalTypeconversion

重點是寫

  • 邏輯類型
  • 邏輯類型工廠
  • 轉換

使用 maven 和 gradle,還支持使用來自 avro 模式的自定義類型生成代碼。 請參閱https://github.com/markush81/avro-examples 中的示例。

Gradle 插件配置

plugins {
    id 'com.commercehub.gradle.plugin.avro' version '0.18.0'
}

avro {
    enableDecimalLogicalType = true
    dateTimeLogicalType = "JSR310"
    stringType = "CharSequence"
    outputCharacterEncoding = "UTF-8"
    logicalTypeFactory("street", de.mh.examples.avro.StreetLogicalTypeFactory)
    customConversion(de.mh.examples.avro.StreetConversion)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM