Search in sources :

Example 1 with SpecificFixed

use of org.apache.avro.specific.SpecificFixed in project gora by apache.

the class SpecificFixedSerializer method fromByteBuffer.

@Override
public SpecificFixed fromByteBuffer(ByteBuffer byteBuffer) {
    if (byteBuffer == null) {
        return null;
    }
    Object value = null;
    try {
        value = clazz.newInstance();
    } catch (InstantiationException ie) {
        LOG.warn("Instantiation error for class=" + clazz, ie);
        return null;
    } catch (IllegalAccessException iae) {
        LOG.warn("Illegal access error for class=" + clazz, iae);
        return null;
    }
    if (!(value instanceof SpecificFixed)) {
        LOG.warn("Not an instance of SpecificFixed");
        return null;
    }
    SpecificFixed fixed = (SpecificFixed) value;
    byte[] bytes = fixed.bytes();
    try {
        byteBuffer.get(bytes, 0, bytes.length);
    } catch (BufferUnderflowException e) {
        // LOG.info(e.toString() + " : class=" + clazz.getName() + " length=" + bytes.length);
        throw e;
    }
    fixed.bytes(bytes);
    return fixed;
}
Also used : SpecificFixed(org.apache.avro.specific.SpecificFixed) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 2 with SpecificFixed

use of org.apache.avro.specific.SpecificFixed in project gora by apache.

the class GoraSerializerTypeInferer method getSerializer.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Serializer<T> getSerializer(Object value) {
    Serializer serializer = null;
    if (value == null) {
        serializer = ByteBufferSerializer.get();
    } else if (value instanceof CharSequence) {
        serializer = CharSequenceSerializer.get();
    } else if (value instanceof Utf8) {
        serializer = CharSequenceSerializer.get();
    } else if (value instanceof Boolean) {
        serializer = BooleanSerializer.get();
    } else if (value instanceof ByteBuffer) {
        serializer = ByteBufferSerializer.get();
    } else if (value instanceof byte[]) {
        serializer = BytesArraySerializer.get();
    } else if (value instanceof Double) {
        serializer = DoubleSerializer.get();
    } else if (value instanceof Float) {
        serializer = FloatSerializer.get();
    } else if (value instanceof Integer) {
        serializer = IntegerSerializer.get();
    } else if (value instanceof Long) {
        serializer = LongSerializer.get();
    } else if (value instanceof String) {
        serializer = StringSerializer.get();
    } else if (value instanceof SpecificFixed) {
        serializer = SpecificFixedSerializer.get(value.getClass());
    } else if (value instanceof GenericArray) {
        Schema schema = ((GenericArray) value).getSchema();
        if (schema.getType() == Type.ARRAY) {
            schema = schema.getElementType();
        }
        serializer = ListSerializer.get(schema);
    } else if (value instanceof Map) {
        Map map = (Map) value;
        if (map.size() == 0) {
            serializer = ByteBufferSerializer.get();
        } else {
            Object value0 = map.values().iterator().next();
            Schema schema = TypeUtils.getSchema(value0);
            serializer = MapSerializer.get(schema);
        }
    } else if (value instanceof Persistent) {
        serializer = ObjectSerializer.get();
    } else {
        serializer = SerializerTypeInferer.getSerializer(value);
    }
    return serializer;
}
Also used : Schema(org.apache.avro.Schema) SpecificFixed(org.apache.avro.specific.SpecificFixed) Persistent(org.apache.gora.persistency.Persistent) ByteBuffer(java.nio.ByteBuffer) Utf8(org.apache.avro.util.Utf8) GenericArray(org.apache.avro.generic.GenericArray) Map(java.util.Map) Serializer(me.prettyprint.hector.api.Serializer) DoubleSerializer(me.prettyprint.cassandra.serializers.DoubleSerializer) BytesArraySerializer(me.prettyprint.cassandra.serializers.BytesArraySerializer) ByteBufferSerializer(me.prettyprint.cassandra.serializers.ByteBufferSerializer) FloatSerializer(me.prettyprint.cassandra.serializers.FloatSerializer) ObjectSerializer(me.prettyprint.cassandra.serializers.ObjectSerializer) LongSerializer(me.prettyprint.cassandra.serializers.LongSerializer) StringSerializer(me.prettyprint.cassandra.serializers.StringSerializer) BooleanSerializer(me.prettyprint.cassandra.serializers.BooleanSerializer) IntegerSerializer(me.prettyprint.cassandra.serializers.IntegerSerializer)

Aggregations

SpecificFixed (org.apache.avro.specific.SpecificFixed)2 BufferUnderflowException (java.nio.BufferUnderflowException)1 ByteBuffer (java.nio.ByteBuffer)1 Map (java.util.Map)1 BooleanSerializer (me.prettyprint.cassandra.serializers.BooleanSerializer)1 ByteBufferSerializer (me.prettyprint.cassandra.serializers.ByteBufferSerializer)1 BytesArraySerializer (me.prettyprint.cassandra.serializers.BytesArraySerializer)1 DoubleSerializer (me.prettyprint.cassandra.serializers.DoubleSerializer)1 FloatSerializer (me.prettyprint.cassandra.serializers.FloatSerializer)1 IntegerSerializer (me.prettyprint.cassandra.serializers.IntegerSerializer)1 LongSerializer (me.prettyprint.cassandra.serializers.LongSerializer)1 ObjectSerializer (me.prettyprint.cassandra.serializers.ObjectSerializer)1 StringSerializer (me.prettyprint.cassandra.serializers.StringSerializer)1 Serializer (me.prettyprint.hector.api.Serializer)1 Schema (org.apache.avro.Schema)1 GenericArray (org.apache.avro.generic.GenericArray)1 Utf8 (org.apache.avro.util.Utf8)1 Persistent (org.apache.gora.persistency.Persistent)1