以编程方式从Avro模式创建SpecificRecord

2020-02-22 avro

我正在使用以下代码,该代码采用org.apache.avro.Schema并返回org.apache.avro.generic.GenericRecord

如何更改它以返回org.apache.avro.specific.SpecificRecord代替?

  private GenericRecord generateRecord(Schema schema) {
    GenericRecordBuilder builder = new GenericRecordBuilder(schema);
    for (Schema.Field field : schema.getFields()) {
      builder.set(field, generateObject(field.schema()));
    }
    return builder.build();
  }

例如

if (schema.getName().equals("MyCustomRecord")) {
    // code to create SpecificRecord here from Schema
}

Answers

Related