NIFI-4030 Populating default values on GenericRecord from Avro schema if not present in RecordSchema

This closes #1896.
This commit is contained in:
Bryan Bende 2017-06-07 13:00:13 -04:00 committed by Mark Payne
parent 45e035686f
commit b0c9428776
1 changed files with 9 additions and 0 deletions

View File

@ -273,6 +273,15 @@ public class AvroTypeUtil {
rec.put(fieldName, converted);
}
// see if the Avro schema has any fields that aren't in the RecordSchema, and if those fields have a default
// value then we want to populate it in the GenericRecord being produced
for (final Field field : avroSchema.getFields()) {
final Optional<RecordField> recordField = recordSchema.getField(field.name());
if (!recordField.isPresent() && rec.get(field.name()) == null && field.defaultVal() != null) {
rec.put(field.name(), field.defaultVal());
}
}
return rec;
}