[prev in list] [next in list] [prev in thread] [next in thread]
List: avro-user
Subject: Fwd: working with Avro records and schemas, programmatically
From: Colin Williams <colin.williams.seattle () gmail ! com>
Date: 2020-09-18 6:27:34
Message-ID: CAPXXXSBnMW_1rsFXc+FBzAKrWPWBjS5BgWeYo6XSCheJbRGNFg () mail ! gmail ! com
[Download RAW message or body]
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
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic