How do you update an existing avro schema using apache avro SchemaBuilder?
Asked Answered
F

2

11

I am testing a new schema registry which loads and retrieves different kinds of avro schemas. In the process of testing, I need to create a bunch of different types of avro schemas. As it involves a lot of permutations, I decided to create the schema programmatically. I am using the apache avro SchemaBuilder to do so.

I created the avro using :

Schema oldSchema = SchemaBuilder
      .record("abc")
      .aliases("records")
      .fields()
      .name("field_null")
      .type("null")
      .noDefault()
      .endRecord();

This worked. The avro created looks like :

{
 "type" : "record",
 "name" : "abc",
 "fields" : [ {
   "name" : "field_null",
   "type" : "null"
  } ],
  "aliases" : [ "records" ]
}

Now I want to create a new version of the schema using the apache avro libraries like :

{
 "type" : "record",
 "name" : "abc",
 "fields" : [ {
   "name" : "field_null",
   "type" : "null"
  },
  {
   "name" : "new_field",
   "type" : "int",
   "default" : 10
  }
 ],
 "aliases" : [ "records" ]
}

For this, I tried :

Schema.Field field = new Schema.Field("new_field", SchemaBuilder.builder().intType(),
    "NewField", 10);

List<Schema.Field> fields = new ArrayList<>();
fields.add(field);
fields.addAll(oldSchema.getFields());

Schema record = Schema.createRecord(oldSchema.getName(),
    "Changes",
    oldSchema.getNamespace(),
    false,
    fields);

I get :

org.apache.avro.AvroRuntimeException: Field already used: field_null type:NULL pos:0

at org.apache.avro.Schema$RecordSchema.setFields(Schema.java:647)
at org.apache.avro.Schema$RecordSchema.<init>(Schema.java:618)
at org.apache.avro.Schema.createRecord(Schema.java:167)

My problem is :

  1. How do I add new versions of the schema using existing libraries?
  2. Should I use avro schemaBuilder to create the schema or rather create my own POJOs to build the schema/save the avsc files in a data directory.
Flasher answered 22/8, 2017 at 12:40 Comment(0)
B
3

You can try this to create fields, maybe it's clumsy :

Schema.Field field = new Schema.Field("new_field",SchemaBuilder.builder().intType(),
    "NewField", 10);

List<Schema.Field> fields = new ArrayList<>();

for (Schema.Field f : oldSchema.getFields()) {

   Schema.Field _field = new Schema.Field(f.name(), f.schema(), f.doc(), f.defaultValue());
  fields.add(_field);

}
Backlash answered 14/12, 2017 at 8:36 Comment(0)
F
1

To copy the fields from the old schema to the new one you have to do a deep copy of each field as @xiping xing suggested.

This is because the Schema class checks that the field is only added once to a schema, and in your case those fields were already added to the old schema.

You can see how they use a flag in this snippet from Avro 1.7.7:

@Override
public void setFields(List<Field> fields) {
  if (this.fields != null) {
    throw new AvroRuntimeException("Fields are already set");
  }
  int i = 0;
  fieldMap = new HashMap<String, Field>();
  LockableArrayList ff = new LockableArrayList();
  for (Field f : fields) {
    if (f.position != -1)
      throw new AvroRuntimeException("Field already used: " + f);
    f.position = i++;
    final Field existingField = fieldMap.put(f.name(), f);
    if (existingField != null) {
      throw new AvroRuntimeException(String.format(
          "Duplicate field %s in record %s: %s and %s.",
          f.name(), name, f, existingField));
    }
    ff.add(f);
  }
  this.fields = ff.lock();
  this.hashCode = NO_HASHCODE;
}
Foraminifer answered 30/1, 2018 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.