Skip to content

Instantly share code, notes, and snippets.

@iht
Created June 15, 2020 14:59
Show Gist options
  • Save iht/e848365e8f3a4c06fefba8c2967100cd to your computer and use it in GitHub Desktop.
Save iht/e848365e8f3a4c06fefba8c2967100cd to your computer and use it in GitHub Desktop.
Reading a NUMERIC from an Avro GENERIC RECORD
GenericRecord record = input.getRecord()
Schema fieldSchema = record.getSchema().getField(fieldName).schema();
Schema.Type type = fieldSchema.getType();
// LogicalType will be Decimal for NUMERIC, and null for most of other types
Optional<LogicalType> logicalType = Optional.ofNullable(fieldSchema.getLogicalType);
switch (type) {
case BYTES:
if (!logicalType.isPresent()) {
// Type is not NUMERIC, Bytes can also be used for the BigQuery BYTES type)
throw new RuntimeException("We are implementing only NUMERIC parsing")
}
// Type is NUMERIC
// Get scale property
Decimal decimalType = (Decimal) logicalType.get();
int scale = decimalType.getScale();
// Transform value into BigDecimal
ByteBuffer value = (ByteBuffer) rec.get(fieldName);
byte[] bytes = new byte[value.remaining()];
value.get(bytes);
BigDecimal bigDecimalValue = new BigDecimal(new BigInteger(bytes), scale);
return bigDecimalValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment