Search in sources :

Example 1 with Schema

use of org.apache.avro.Schema in project camel by apache.

the class AvroDataFormat method loadSchema.

protected Schema loadSchema(String className) throws CamelException, ClassNotFoundException {
    // must use same class loading procedure to ensure working in OSGi
    Class<?> instanceClass = camelContext.getClassResolver().resolveMandatoryClass(className);
    Class<?> genericContainer = camelContext.getClassResolver().resolveMandatoryClass(GENERIC_CONTAINER_CLASSNAME);
    if (genericContainer.isAssignableFrom(instanceClass)) {
        try {
            Method method = instanceClass.getMethod("getSchema");
            return (Schema) method.invoke(camelContext.getInjector().newInstance(instanceClass));
        } catch (Exception ex) {
            throw new CamelException("Error calling getSchema on " + instanceClass, ex);
        }
    } else {
        throw new CamelException("Class " + instanceClass + " must be instanceof " + GENERIC_CONTAINER_CLASSNAME);
    }
}
Also used : CamelException(org.apache.camel.CamelException) Schema(org.apache.avro.Schema) Method(java.lang.reflect.Method) CamelException(org.apache.camel.CamelException)

Example 2 with Schema

use of org.apache.avro.Schema in project storm by apache.

the class CachedSchemas method getSchema.

public Schema getSchema(String schemaString) {
    Schema schema = cache.get(schemaString);
    if (schema == null) {
        schema = new Schema.Parser().parse(schemaString);
        cache.put(schemaString, schema);
    }
    return schema;
}
Also used : Schema(org.apache.avro.Schema)

Example 3 with Schema

use of org.apache.avro.Schema in project storm by apache.

the class AbstractAvroSerializer method read.

@Override
public GenericContainer read(Kryo kryo, Input input, Class<GenericContainer> aClass) {
    Schema theSchema = this.getSchema(input.readString());
    GenericDatumReader<GenericContainer> reader = new GenericDatumReader<>(theSchema);
    Decoder decoder = DecoderFactory.get().directBinaryDecoder(input, null);
    GenericContainer foo;
    try {
        foo = reader.read(null, decoder);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return foo;
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) IOException(java.io.IOException) GenericContainer(org.apache.avro.generic.GenericContainer) Decoder(org.apache.avro.io.Decoder)

Example 4 with Schema

use of org.apache.avro.Schema in project hive by apache.

the class AvroDeserializer method deserializeSingleItemNullableUnion.

private Object deserializeSingleItemNullableUnion(Object datum, Schema fileSchema, Schema recordSchema) throws AvroSerdeException {
    // Determine index of value
    int tag = GenericData.get().resolveUnion(recordSchema, datum);
    Schema schema = recordSchema.getTypes().get(tag);
    if (schema.getType().equals(Type.NULL)) {
        return null;
    }
    Schema currentFileSchema = null;
    if (fileSchema != null) {
        if (fileSchema.getType() == Type.UNION) {
            // we need to get the correct tag
            try {
                tag = GenericData.get().resolveUnion(fileSchema, datum);
                currentFileSchema = fileSchema.getTypes().get(tag);
            } catch (UnresolvedUnionException e) {
                if (LOG.isDebugEnabled()) {
                    String datumClazz = null;
                    if (datum != null) {
                        datumClazz = datum.getClass().getName();
                    }
                    String msg = "File schema union could not resolve union. fileSchema = " + fileSchema + ", recordSchema = " + recordSchema + ", datum class = " + datumClazz + ": " + e;
                    LOG.debug(msg, e);
                }
                // This occurs when the datum type is different between
                // the file and record schema. For example if datum is long
                // and the field in the file schema is int. See HIVE-9462.
                // in this case we will re-use the record schema as the file
                // schema, Ultimately we need to clean this code up and will
                // do as a follow-on to HIVE-9462.
                currentFileSchema = schema;
            }
        } else {
            currentFileSchema = fileSchema;
        }
    }
    return worker(datum, currentFileSchema, schema, SchemaToTypeInfo.generateTypeInfo(schema, null));
}
Also used : Schema(org.apache.avro.Schema) UnresolvedUnionException(org.apache.avro.UnresolvedUnionException)

Example 5 with Schema

use of org.apache.avro.Schema in project hive by apache.

the class AvroDeserializer method workerBase.

// The actual deserialization may involve nested records, which require recursion.
private List<Object> workerBase(List<Object> objectRow, Schema fileSchema, List<String> columnNames, List<TypeInfo> columnTypes, GenericRecord record) throws AvroSerdeException {
    for (int i = 0; i < columnNames.size(); i++) {
        TypeInfo columnType = columnTypes.get(i);
        String columnName = columnNames.get(i);
        Object datum = record.get(columnName);
        Schema datumSchema = record.getSchema().getField(columnName).schema();
        Schema.Field field = AvroSerdeUtils.isNullableType(fileSchema) ? AvroSerdeUtils.getOtherTypeFromNullableType(fileSchema).getField(columnName) : fileSchema.getField(columnName);
        objectRow.add(worker(datum, field == null ? null : field.schema(), datumSchema, columnType));
    }
    return objectRow;
}
Also used : Schema(org.apache.avro.Schema) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo)

Aggregations

Schema (org.apache.avro.Schema)458 Test (org.junit.Test)154 GenericRecord (org.apache.avro.generic.GenericRecord)102 ArrayList (java.util.ArrayList)64 GenericData (org.apache.avro.generic.GenericData)56 IOException (java.io.IOException)45 HashMap (java.util.HashMap)38 Field (org.apache.avro.Schema.Field)37 File (java.io.File)36 Map (java.util.Map)27 Type (org.apache.avro.Schema.Type)27 Configuration (org.apache.hadoop.conf.Configuration)25 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)24 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)23 Path (org.apache.hadoop.fs.Path)23 ByteBuffer (java.nio.ByteBuffer)22 List (java.util.List)21 DataFileWriter (org.apache.avro.file.DataFileWriter)21 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 GenericArray (org.apache.avro.generic.GenericArray)17