@Experimental(value=SCHEMAS) public class AvroUtils extends java.lang.Object
Avro Beam Field Type INT <-----> INT32 LONG <-----> INT64 FLOAT <-----> FLOAT DOUBLE <-----> DOUBLE BOOLEAN <-----> BOOLEAN STRING <-----> STRING BYTES <-----> BYTES <------ LogicalType(urn="beam:logical_type:var_bytes:v1") FIXED <-----> LogicalType(urn="beam:logical_type:fixed_bytes:v1") ARRAY <-----> ARRAY ENUM <-----> LogicalType(EnumerationType) MAP <-----> MAP RECORD <-----> ROW UNION <-----> LogicalType(OneOfType) LogicalTypes.Date <-----> LogicalType(DATE) LogicalTypes.TimestampMillis <-----> DATETIME LogicalTypes.Decimal <-----> DECIMALFor SQL CHAR/VARCHAR types, an Avro schema
LogicalType({"type":"string","logicalType":"char","maxLength":MAX_LENGTH}) or LogicalType({"type":"string","logicalType":"varchar","maxLength":MAX_LENGTH})is used.
Modifier and Type | Class and Description |
---|---|
static class |
AvroUtils.AvroConvertType |
static class |
AvroUtils.AvroConvertValueForGetter |
static class |
AvroUtils.AvroConvertValueForSetter |
static class |
AvroUtils.FixedBytesField
Wrapper for fixed byte fields.
|
Modifier and Type | Method and Description |
---|---|
static @Nullable java.lang.Object |
convertAvroFieldStrict(@Nullable java.lang.Object value,
Schema avroSchema,
Schema.FieldType fieldType)
Strict conversion from AVRO to Beam, strict because it doesn't do widening or narrowing during
conversion.
|
static SimpleFunction<byte[],Row> |
getAvroBytesToRowFunction(Schema beamSchema)
Returns a function mapping encoded AVRO
GenericRecord s to Beam Row s. |
static <T> SchemaUserTypeCreator |
getCreator(java.lang.Class<T> clazz,
Schema schema)
Get an object creator for an AVRO-generated SpecificRecord.
|
static <T> java.util.List<FieldValueTypeInformation> |
getFieldTypes(java.lang.Class<T> clazz,
Schema schema)
Get field types for an AVRO-generated SpecificRecord or a POJO.
|
static <T> SerializableFunction<Row,T> |
getFromRowFunction(java.lang.Class<T> clazz) |
static SerializableFunction<GenericRecord,Row> |
getGenericRecordToRowFunction(@Nullable Schema schema)
Returns a function mapping AVRO
GenericRecord s to Beam Row s for use in PCollection.setSchema(org.apache.beam.sdk.schemas.Schema, org.apache.beam.sdk.values.TypeDescriptor<T>, org.apache.beam.sdk.transforms.SerializableFunction<T, org.apache.beam.sdk.values.Row>, org.apache.beam.sdk.transforms.SerializableFunction<org.apache.beam.sdk.values.Row, T>) . |
static <T> java.util.List<FieldValueGetter> |
getGetters(java.lang.Class<T> clazz,
Schema schema)
Get generated getters for an AVRO-generated SpecificRecord or a POJO.
|
static SimpleFunction<Row,byte[]> |
getRowToAvroBytesFunction(Schema beamSchema)
Returns a function mapping Beam
Row s to encoded AVRO GenericRecord s. |
static SerializableFunction<Row,GenericRecord> |
getRowToGenericRecordFunction(Schema avroSchema)
Returns a function mapping Beam
Row s to AVRO GenericRecord s for use in PCollection.setSchema(org.apache.beam.sdk.schemas.Schema, org.apache.beam.sdk.values.TypeDescriptor<T>, org.apache.beam.sdk.transforms.SerializableFunction<T, org.apache.beam.sdk.values.Row>, org.apache.beam.sdk.transforms.SerializableFunction<org.apache.beam.sdk.values.Row, T>) . |
static <T> @Nullable Schema |
getSchema(java.lang.Class<T> clazz,
Schema schema) |
static <T> SerializableFunction<T,Row> |
getToRowFunction(java.lang.Class<T> clazz,
Schema schema) |
static <T> SchemaCoder<T> |
schemaCoder(AvroCoder<T> avroCoder)
Returns an
SchemaCoder instance based on the provided AvroCoder for the element type. |
static <T> SchemaCoder<T> |
schemaCoder(java.lang.Class<T> clazz)
Returns an
SchemaCoder instance for the provided element class. |
static <T> SchemaCoder<T> |
schemaCoder(java.lang.Class<T> clazz,
Schema schema)
Returns an
SchemaCoder instance for the provided element type using the provided Avro
schema. |
static SchemaCoder<GenericRecord> |
schemaCoder(Schema schema)
Returns an
SchemaCoder instance for the Avro schema. |
static <T> SchemaCoder<T> |
schemaCoder(TypeDescriptor<T> type)
Returns an
SchemaCoder instance for the provided element type. |
static Schema.Field |
toAvroField(Schema.Field field,
java.lang.String namespace)
Get Avro Field from Beam Field.
|
static Schema |
toAvroSchema(Schema beamSchema) |
static Schema |
toAvroSchema(Schema beamSchema,
@Nullable java.lang.String name,
@Nullable java.lang.String namespace)
Converts a Beam Schema into an AVRO schema.
|
static Schema.Field |
toBeamField(Schema.Field field)
Get Beam Field from avro Field.
|
static Row |
toBeamRowStrict(GenericRecord record,
@Nullable Schema schema)
Strict conversion from AVRO to Beam, strict because it doesn't do widening or narrowing during
conversion.
|
static Schema |
toBeamSchema(Schema schema)
Converts AVRO schema to Beam row schema.
|
static GenericRecord |
toGenericRecord(Row row)
Convert from a Beam Row to an AVRO GenericRecord.
|
static GenericRecord |
toGenericRecord(Row row,
Schema avroSchema)
Convert from a Beam Row to an AVRO GenericRecord.
|
public static Schema.Field toBeamField(Schema.Field field)
public static Schema.Field toAvroField(Schema.Field field, java.lang.String namespace)
public static Schema toBeamSchema(Schema schema)
schema
- schema of type RECORDpublic static Schema toAvroSchema(Schema beamSchema, @Nullable java.lang.String name, @Nullable java.lang.String namespace)
public static Row toBeamRowStrict(GenericRecord record, @Nullable Schema schema)
public static GenericRecord toGenericRecord(Row row)
public static GenericRecord toGenericRecord(Row row, Schema avroSchema)
public static <T> SerializableFunction<T,Row> getToRowFunction(java.lang.Class<T> clazz, Schema schema)
public static <T> SerializableFunction<Row,T> getFromRowFunction(java.lang.Class<T> clazz)
public static SimpleFunction<byte[],Row> getAvroBytesToRowFunction(Schema beamSchema)
GenericRecord
s to Beam Row
s.public static SimpleFunction<Row,byte[]> getRowToAvroBytesFunction(Schema beamSchema)
Row
s to encoded AVRO GenericRecord
s.public static SerializableFunction<GenericRecord,Row> getGenericRecordToRowFunction(@Nullable Schema schema)
GenericRecord
s to Beam Row
s for use in PCollection.setSchema(org.apache.beam.sdk.schemas.Schema, org.apache.beam.sdk.values.TypeDescriptor<T>, org.apache.beam.sdk.transforms.SerializableFunction<T, org.apache.beam.sdk.values.Row>, org.apache.beam.sdk.transforms.SerializableFunction<org.apache.beam.sdk.values.Row, T>)
.public static SerializableFunction<Row,GenericRecord> getRowToGenericRecordFunction(Schema avroSchema)
Row
s to AVRO GenericRecord
s for use in PCollection.setSchema(org.apache.beam.sdk.schemas.Schema, org.apache.beam.sdk.values.TypeDescriptor<T>, org.apache.beam.sdk.transforms.SerializableFunction<T, org.apache.beam.sdk.values.Row>, org.apache.beam.sdk.transforms.SerializableFunction<org.apache.beam.sdk.values.Row, T>)
.public static <T> SchemaCoder<T> schemaCoder(TypeDescriptor<T> type)
SchemaCoder
instance for the provided element type.T
- the element typepublic static <T> SchemaCoder<T> schemaCoder(java.lang.Class<T> clazz)
SchemaCoder
instance for the provided element class.T
- the element typepublic static SchemaCoder<GenericRecord> schemaCoder(Schema schema)
SchemaCoder
instance for the Avro schema. The implicit type is
GenericRecord.public static <T> SchemaCoder<T> schemaCoder(java.lang.Class<T> clazz, Schema schema)
SchemaCoder
instance for the provided element type using the provided Avro
schema.
If the type argument is GenericRecord, the schema may be arbitrary. Otherwise, the schema must correspond to the type provided.
T
- the element typepublic static <T> SchemaCoder<T> schemaCoder(AvroCoder<T> avroCoder)
SchemaCoder
instance based on the provided AvroCoder for the element type.T
- the element typepublic static <T> java.util.List<FieldValueTypeInformation> getFieldTypes(java.lang.Class<T> clazz, Schema schema)
public static <T> java.util.List<FieldValueGetter> getGetters(java.lang.Class<T> clazz, Schema schema)
public static <T> SchemaUserTypeCreator getCreator(java.lang.Class<T> clazz, Schema schema)
public static @Nullable java.lang.Object convertAvroFieldStrict(@Nullable java.lang.Object value, @Nonnull Schema avroSchema, @Nonnull Schema.FieldType fieldType)
value
- GenericRecord
or any nested valueavroSchema
- schema for valuefieldType
- target beam field typeRow