Java Code Examples for org.apache.avro.generic.GenericData#getConversionFor()
The following examples show how to use
org.apache.avro.generic.GenericData#getConversionFor() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AvroIndexedRecordConverter.java From parquet-mr with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") private static <T> Class<T> getDatumClass(GenericData model, Schema schema) { if (model.getConversionFor(schema.getLogicalType()) != null) { // use generic classes to pass data to conversions return null; } if (model instanceof SpecificData) { return (Class<T>) ((SpecificData) model).getClass(schema); } return null; }
Example 2
Source File: AvroRecordConverter.java From parquet-mr with Apache License 2.0 | 5 votes |
public AvroRecordConverter(MessageType parquetSchema, Schema avroSchema, GenericData baseModel) { this(null, parquetSchema, avroSchema, baseModel); LogicalType logicalType = avroSchema.getLogicalType(); Conversion<?> conversion = baseModel.getConversionFor(logicalType); this.rootContainer = ParentValueContainer.getConversionContainer(new ParentValueContainer() { @Override @SuppressWarnings("unchecked") public void add(Object value) { AvroRecordConverter.this.currentRecord = (T) value; } }, conversion, avroSchema); }
Example 3
Source File: AvroIndexedRecordConverter.java From parquet-mr with Apache License 2.0 | 4 votes |
private static Converter newConverter(Schema schema, Type type, GenericData model, ParentValueContainer setter) { LogicalType logicalType = schema.getLogicalType(); // the expected type is always null because it is determined by the parent // datum class, which never helps for generic. when logical types are added // to specific, this should pass the expected type here. Conversion<?> conversion = model.getConversionFor(logicalType); ParentValueContainer parent = ParentValueContainer .getConversionContainer(setter, conversion, schema); switch (schema.getType()) { case ARRAY: return new AvroArrayConverter(parent, type.asGroupType(), schema, model); case BOOLEAN: return new AvroConverters.FieldBooleanConverter(parent); case BYTES: return new AvroConverters.FieldByteBufferConverter(parent); case DOUBLE: return new AvroConverters.FieldDoubleConverter(parent); case ENUM: return new FieldEnumConverter(parent, schema, model); case FIXED: return new FieldFixedConverter(parent, schema, model); case FLOAT: return new AvroConverters.FieldFloatConverter(parent); case INT: return new AvroConverters.FieldIntegerConverter(parent); case LONG: return new AvroConverters.FieldLongConverter(parent); case MAP: return new MapConverter(parent, type.asGroupType(), schema, model); case RECORD: return new AvroIndexedRecordConverter(parent, type.asGroupType(), schema, model); case STRING: return new AvroConverters.FieldStringConverter(parent); case UNION: return new AvroUnionConverter(parent, type, schema, model); case NULL: // fall through default: throw new UnsupportedOperationException(String.format("Cannot convert Avro type: %s" + " (Parquet type: %s) ", schema, type)); } }
Example 4
Source File: AvroRecordConverter.java From parquet-mr with Apache License 2.0 | 4 votes |
private static Converter newConverter(Schema schema, Type type, GenericData model, Class<?> knownClass, ParentValueContainer setter) { LogicalType logicalType = schema.getLogicalType(); Conversion<?> conversion; if (knownClass != null) { conversion = model.getConversionByClass(knownClass, logicalType); } else { conversion = model.getConversionFor(logicalType); } ParentValueContainer parent = ParentValueContainer .getConversionContainer(setter, conversion, schema); switch (schema.getType()) { case BOOLEAN: return new AvroConverters.FieldBooleanConverter(parent); case INT: Class<?> intDatumClass = getDatumClass(conversion, knownClass, schema, model); if (intDatumClass == null) { return new AvroConverters.FieldIntegerConverter(parent); } if (intDatumClass == byte.class || intDatumClass == Byte.class) { return new AvroConverters.FieldByteConverter(parent); } if (intDatumClass == char.class || intDatumClass == Character.class) { return new AvroConverters.FieldCharConverter(parent); } if (intDatumClass == short.class || intDatumClass == Short.class) { return new AvroConverters.FieldShortConverter(parent); } return new AvroConverters.FieldIntegerConverter(parent); case LONG: return new AvroConverters.FieldLongConverter(parent); case FLOAT: return new AvroConverters.FieldFloatConverter(parent); case DOUBLE: return new AvroConverters.FieldDoubleConverter(parent); case BYTES: Class<?> byteDatumClass = getDatumClass(conversion, knownClass, schema, model); if (byteDatumClass == null) { return new AvroConverters.FieldByteBufferConverter(parent); } if (byteDatumClass.isArray() && byteDatumClass.getComponentType() == byte.class) { return new AvroConverters.FieldByteArrayConverter(parent); } return new AvroConverters.FieldByteBufferConverter(parent); case STRING: if (logicalType != null && logicalType.getName().equals(LogicalTypes.uuid().getName())) { return new AvroConverters.FieldUUIDConverter(parent, type.asPrimitiveType()); } return newStringConverter(schema, model, parent); case RECORD: return new AvroRecordConverter(parent, type.asGroupType(), schema, model); case ENUM: return new AvroConverters.FieldEnumConverter(parent, schema, model); case ARRAY: Class<?> arrayDatumClass = getDatumClass(conversion, knownClass, schema, model); if (arrayDatumClass != null && arrayDatumClass.isArray()) { return new AvroArrayConverter(parent, type.asGroupType(), schema, model, arrayDatumClass); } return new AvroCollectionConverter(parent, type.asGroupType(), schema, model, arrayDatumClass); case MAP: return new MapConverter(parent, type.asGroupType(), schema, model); case UNION: return new AvroUnionConverter(parent, type, schema, model); case FIXED: return new AvroConverters.FieldFixedConverter(parent, schema, model); default: throw new UnsupportedOperationException(String.format( "Cannot convert Avro type: %s to Parquet type: %s", schema, type)); } }