2

无论我尝试什么,Avro 都不会在代码中为字段生成 UUID 类。

版本:1.8.2 和 1.9.1

我试图用这样的模式生成 UUID:

  "type": "record",
  "name": "RecordWithUUID",
  "namespace": "avro",
  "fields": [
    {
      "name": "uuid",
      "type": {
        "type": "string",
        "logicalType": "uuid"
      }
    }
  ]
}

这是来自 Avro 项目测试类的类TestReflectLogicalTypes::RecordWithUUID 所以,简而言之,这是 Avro 用来测试其代码功能的模式示例,我希望它应该可以工作。

我这样运行以从我的方案中获取 java 代码:

java -jar avro-tools-1.8.2.jar compile schema my_scheme.avsc .

java -jar avro-tools-1.9.1.jar compile schema my_scheme.avsc .

Avro的初始类代码是这样的:

class RecordWithUUID {
  UUID uuid; // <- UUID (!!!)

  @Override
  public int hashCode() {
    return uuid.hashCode();
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (!(obj instanceof RecordWithUUID)) {
      return false;
    }
    RecordWithUUID that = (RecordWithUUID) obj;
    return this.uuid.equals(that.uuid);
  }
}

这是我生成后得到的代码:

1.8.2 来自命令行的 avro-tools

  ..

@SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated
public class RecordWithUUID extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
  private static final long serialVersionUID = 9147582668665082277L;
  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"RecordWithUUID\",\"namespace\":\"avro\",\"fields\":[{\"name\":\"uuid\",\"type\":{\"type\":\"string\",\"logicalType\":\"uuid\"}}]}");
  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }

  ..

  @Deprecated public java.lang.CharSequence uuid; //<- NO UUID

  ..

1.9.1 来自命令行的 avro-tools

 ..

@org.apache.avro.specific.AvroGenerated
public class RecordWithUUID extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
  private static final long serialVersionUID = 9147582668665082277L;
  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"RecordWithUUID\",\"namespace\":\"avro\",\"fields\":[{\"name\":\"uuid\",\"type\":{\"type\":\"string\",\"logicalType\":\"uuid\"}}]}");
  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }

  ..

   private java.lang.CharSequence uuid; //<- NO UUID
  ..

avro 1.8.2/1.9.1 的 maven 插件

  ..

@org.apache.avro.specific.AvroGenerated
public class RecordWithUUID extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
  private static final long serialVersionUID = 9147582668665082277L;
  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"RecordWithUUID\",\"namespace\":\"avro\",\"fields\":[{\"name\":\"uuid\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\",\"logicalType\":\"uuid\"}}]}");
  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }

  ..

   private java.lang.String uuid;//<- NO UUID
  ..

那么,这是一个错误还是我做错了什么?

4

1 回答 1

1

尝试 1.10.0-SNAPSHOT,我在那里工作(在遇到与其他版本相同的问题后)

于 2020-01-17T15:46:38.950 回答