Hello,

I'm trying to understand working with Avro records and schemas,
programmatically. Then I was first trying to create a new schema and
records based on existing records, but with a different name /
namespace. It seems then I don't understand getFields() or
createRecord(...). Why can't I use the fields obtained from
getFields() in createRecord()?  How would I go about this properly?

// for an existing record already present
GenericRecord someRecord

// get a list of existing fields
List<Schema.Field> existingFields = someRecord.getSchema().getFields();

// schema for new record with existing fields
Schema updatedSchema = createRecord("UpdatedName",
"","avro.com.example.namespace" , false, existingFields);

^^ throws an exception ^^

/* Caused by: org.apache.avro.AvroRuntimeException: Field already
used: eventMetadata type:UNION pos:0
at org.apache.avro.Schema$RecordSchema.setFields(Schema.java:888)
at org.apache.avro.Schema$RecordSchema.<init>(Schema.java:856)
at org.apache.avro.Schema.createRecord(Schema.java:217)
*/

final int length = fields.size();

GenericRecord clonedRecord = new GenericData.Record(updatedSchema);
for (int i = 0; i < length; i++) {
    final Schema.Field field = existingFields.get(i);
    clonedRecord.put(i, someRecord.get(i));
}


Best Regards,

Colin Williams

Reply via email to