Search in sources :

Example 1 with Serializer

use of me.prettyprint.hector.api.Serializer 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)

Example 2 with Serializer

use of me.prettyprint.hector.api.Serializer in project gora by apache.

the class GoraSerializerTypeInferer method getSerializer.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Serializer<T> getSerializer(Schema schema) {
    Serializer serializer = null;
    Type type = schema.getType();
    if (type.equals(Type.STRING)) {
        serializer = CharSequenceSerializer.get();
    } else if (type.equals(Type.BOOLEAN)) {
        serializer = BooleanSerializer.get();
    } else if (type.equals(Type.BYTES)) {
        serializer = ByteBufferSerializer.get();
    } else if (type.equals(Type.DOUBLE)) {
        serializer = DoubleSerializer.get();
    } else if (type.equals(Type.FLOAT)) {
        serializer = FloatSerializer.get();
    } else if (type.equals(Type.INT)) {
        serializer = IntegerSerializer.get();
    } else if (type.equals(Type.LONG)) {
        serializer = LongSerializer.get();
    } else if (type.equals(Type.FIXED)) {
        Class clazz = TypeUtils.getClass(schema);
        serializer = SpecificFixedSerializer.get(clazz);
    // serializer = SpecificFixedSerializer.get(schema);
    } else if (type.equals(Type.ARRAY)) {
        serializer = ListSerializer.get(schema.getElementType());
    } else if (type.equals(Type.MAP)) {
        serializer = MapSerializer.get(schema.getValueType());
    } else if (type.equals(Type.UNION)) {
        serializer = ByteBufferSerializer.get();
    } else if (type.equals(Type.RECORD)) {
        serializer = BytesArraySerializer.get();
    } else {
        serializer = null;
    }
    return serializer;
}
Also used : Type(org.apache.avro.Schema.Type) 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

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