Search in sources :

Example 11 with Type

use of org.apache.avro.Schema.Type in project gora by apache.

the class CassandraStore method getUnionSchema.

/**
   * Given an object and the object schema this function obtains,
   * from within the UNION schema, the position of the type used.
   * If no data type can be inferred then we return a default value
   * of position 0.
   * @param pValue
   * @param pUnionSchema
   * @return the unionSchemaPosition.
   */
private int getUnionSchema(Object pValue, Schema pUnionSchema) {
    int unionSchemaPos = 0;
    //    String valueType = pValue.getClass().getSimpleName();
    for (Schema currentSchema : pUnionSchema.getTypes()) {
        Type schemaType = currentSchema.getType();
        if (pValue instanceof CharSequence && schemaType.equals(Type.STRING))
            return unionSchemaPos;
        else if (pValue instanceof ByteBuffer && schemaType.equals(Type.BYTES))
            return unionSchemaPos;
        else if (pValue instanceof Integer && schemaType.equals(Type.INT))
            return unionSchemaPos;
        else if (pValue instanceof Long && schemaType.equals(Type.LONG))
            return unionSchemaPos;
        else if (pValue instanceof Double && schemaType.equals(Type.DOUBLE))
            return unionSchemaPos;
        else if (pValue instanceof Float && schemaType.equals(Type.FLOAT))
            return unionSchemaPos;
        else if (pValue instanceof Boolean && schemaType.equals(Type.BOOLEAN))
            return unionSchemaPos;
        else if (pValue instanceof Map && schemaType.equals(Type.MAP))
            return unionSchemaPos;
        else if (pValue instanceof List && schemaType.equals(Type.ARRAY))
            return unionSchemaPos;
        else if (pValue instanceof Persistent && schemaType.equals(Type.RECORD))
            return unionSchemaPos;
        unionSchemaPos++;
    }
    // if we weren't able to determine which data type it is, then we return the default
    return DEFAULT_UNION_SCHEMA;
}
Also used : Schema(org.apache.avro.Schema) Persistent(org.apache.gora.persistency.Persistent) ByteBuffer(java.nio.ByteBuffer) Type(org.apache.avro.Schema.Type) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 12 with Type

use of org.apache.avro.Schema.Type in project gora by apache.

the class CassandraSuperColumn method getSuperValue.

private Object getSuperValue(Field field, Schema fieldSchema, Type type) {
    Object value = null;
    switch(type) {
        case ARRAY:
            List<Object> array = new ArrayList<>();
            for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
                Object memberValue = fromByteBuffer(fieldSchema.getElementType(), hColumn.getValue());
                // int i = IntegerSerializer().get().fromByteBuffer(hColumn.getName());
                array.add(memberValue);
            }
            value = array;
            break;
        case MAP:
            Map<CharSequence, Object> map = new HashMap<>();
            for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
                CharSequence mapKey = CharSequenceSerializer.get().fromByteBuffer(hColumn.getName());
                if (!mapKey.toString().contains(CassandraStore.UNION_COL_SUFIX)) {
                    Object memberValue = null;
                    // We need detect real type for UNION Fields
                    if (fieldSchema.getValueType().getType().equals(Type.UNION)) {
                        HColumn<ByteBuffer, ByteBuffer> cc = getUnionTypeColumn(mapKey + CassandraStore.UNION_COL_SUFIX, this.hSuperColumn.getColumns());
                        Integer unionIndex = getUnionIndex(cc);
                        Schema realSchema = fieldSchema.getValueType().getTypes().get(unionIndex);
                        memberValue = fromByteBuffer(realSchema, hColumn.getValue());
                    } else {
                        memberValue = fromByteBuffer(fieldSchema.getValueType(), hColumn.getValue());
                    }
                    map.put(mapKey, memberValue);
                }
            }
            value = map;
            break;
        case RECORD:
            String fullName = fieldSchema.getFullName();
            Class<?> claz = null;
            try {
                claz = Class.forName(fullName);
            } catch (ClassNotFoundException cnfe) {
                LOG.warn("Unable to load class " + fullName, cnfe);
                break;
            }
            try {
                value = claz.newInstance();
            } catch (InstantiationException ie) {
                LOG.warn("Instantiation error", ie);
                break;
            } catch (IllegalAccessException iae) {
                LOG.warn("Illegal access error", iae);
                break;
            }
            // we updated the value instance, now update its members
            if (value instanceof PersistentBase) {
                PersistentBase record = (PersistentBase) value;
                for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
                    String memberName = StringSerializer.get().fromByteBuffer(hColumn.getName());
                    if (memberName == null || memberName.length() == 0) {
                        LOG.warn("member name is null or empty.");
                        continue;
                    }
                    if (!memberName.contains(CassandraStore.UNION_COL_SUFIX)) {
                        Field memberField = fieldSchema.getField(memberName);
                        Schema memberSchema = memberField.schema();
                        Type memberType = memberSchema.getType();
                        CassandraSubColumn cassandraColumn = new CassandraSubColumn();
                        cassandraColumn.setField(memberField);
                        cassandraColumn.setValue(hColumn);
                        if (memberType.equals(Type.UNION)) {
                            HColumn<ByteBuffer, ByteBuffer> hc = getUnionTypeColumn(memberField.name() + CassandraStore.UNION_COL_SUFIX, this.hSuperColumn.getColumns().toArray());
                            Integer unionIndex = getUnionIndex(hc);
                            cassandraColumn.setUnionType(unionIndex);
                        }
                        record.put(record.getSchema().getField(memberName).pos(), cassandraColumn.getValue());
                    }
                }
            }
            break;
        case UNION:
            int schemaPos = this.getUnionType();
            Schema unioSchema = fieldSchema.getTypes().get(schemaPos);
            Type unionType = unioSchema.getType();
            value = getSuperValue(field, unioSchema, unionType);
            break;
        default:
            Object memberValue = null;
            // Using for UnionIndex of Union type field get value. UnionIndex always Integer.  
            for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
                memberValue = fromByteBuffer(fieldSchema, hColumn.getValue());
            }
            value = memberValue;
            LOG.warn("Type: " + type.name() + " not supported for field: " + field.name());
    }
    return value;
}
Also used : PersistentBase(org.apache.gora.persistency.impl.PersistentBase) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Field(org.apache.avro.Schema.Field) Type(org.apache.avro.Schema.Type)

Example 13 with Type

use of org.apache.avro.Schema.Type in project gora by apache.

the class MongoStore method fromMongoUnion.

private Object fromMongoUnion(final Schema fieldSchema, final DocumentFieldType storeType, final Field field, final String docf, final BSONDecorator easybson) {
    // schema [type0, type1]
    Object result;
    Type type0 = fieldSchema.getTypes().get(0).getType();
    Type type1 = fieldSchema.getTypes().get(1).getType();
    // or ["type","null"]
    if (!type0.equals(type1) && (type0.equals(Type.NULL) || type1.equals(Type.NULL))) {
        Schema innerSchema = fieldSchema.getTypes().get(1);
        LOG.debug("Load from DBObject (UNION), schemaType:{}, docField:{}, storeType:{}", new Object[] { innerSchema.getType(), docf, storeType });
        // Deserialize as if schema was ["type"]
        result = fromDBObject(innerSchema, storeType, field, docf, easybson);
    } else {
        throw new IllegalStateException("MongoStore doesn't support 3 types union field yet. Please update your mapping");
    }
    return result;
}
Also used : DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType) Type(org.apache.avro.Schema.Type) Schema(org.apache.avro.Schema)

Example 14 with Type

use of org.apache.avro.Schema.Type in project gora by apache.

the class HBaseByteInterface method toBytes.

/**
   * Serializes an object following the given schema.
   * Does not handle <code>array/map</code> if it is not inside a <code>record</code>
   * @param o Utf8|ByteBuffer|Integer|Long|Float|Double|Boolean|Enum|Persistent
   * @param schema The schema describing the object (or a compatible description)
   * @return array of bytes of the serialized object
   * @throws IOException
   */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static byte[] toBytes(Object o, Schema schema) throws IOException {
    Type type = schema.getType();
    switch(type) {
        // TODO: maybe ((Utf8)o).getBytes(); ?
        case STRING:
            return Bytes.toBytes(((CharSequence) o).toString());
        case BYTES:
            return ((ByteBuffer) o).array();
        case INT:
            return Bytes.toBytes((Integer) o);
        case LONG:
            return Bytes.toBytes((Long) o);
        case FLOAT:
            return Bytes.toBytes((Float) o);
        case DOUBLE:
            return Bytes.toBytes((Double) o);
        case BOOLEAN:
            return (Boolean) o ? new byte[] { 1 } : new byte[] { 0 };
        case ENUM:
            return new byte[] { (byte) ((Enum<?>) o).ordinal() };
        case UNION:
        case RECORD:
            SpecificDatumWriter writer = writerMap.get(schema.getFullName());
            if (writer == null) {
                // ignore dirty bits
                writer = new SpecificDatumWriter(schema);
                writerMap.put(schema.getFullName(), writer);
            }
            BinaryEncoder encoderFromCache = encoders.get();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            outputStream.set(bos);
            BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(bos, null);
            if (encoderFromCache == null) {
                encoders.set(encoder);
            }
            //reset the buffers
            ByteArrayOutputStream os = outputStream.get();
            os.reset();
            writer.write(o, encoder);
            encoder.flush();
            return os.toByteArray();
        default:
            throw new RuntimeException("Unknown type: " + type);
    }
}
Also used : Type(org.apache.avro.Schema.Type) BinaryEncoder(org.apache.avro.io.BinaryEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 15 with Type

use of org.apache.avro.Schema.Type in project gora by apache.

the class HBaseStore method getResolvedUnionIndex.

//TODO temporary solution, has to be changed after implementation of saving the index of union type
private int getResolvedUnionIndex(Schema unionScema) {
    if (unionScema.getTypes().size() == 2) {
        // schema [type0, type1]
        Type type0 = unionScema.getTypes().get(0).getType();
        Type type1 = unionScema.getTypes().get(1).getType();
        // or ["type","null"]
        if (!type0.equals(type1) && (type0.equals(Schema.Type.NULL) || type1.equals(Schema.Type.NULL))) {
            if (type0.equals(Schema.Type.NULL))
                return 1;
            else
                return 0;
        }
    }
    return 2;
}
Also used : Type(org.apache.avro.Schema.Type)

Aggregations

Type (org.apache.avro.Schema.Type)40 Schema (org.apache.avro.Schema)28 Field (org.apache.avro.Schema.Field)13 DataType (com.linkedin.pinot.common.data.FieldSpec.DataType)6 ByteBuffer (java.nio.ByteBuffer)6 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 SQLException (java.sql.SQLException)4 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)4 EventCreationException (com.linkedin.databus2.producers.EventCreationException)3 SourceType (com.linkedin.databus2.relay.config.ReplicationBitSetterStaticConfig.SourceType)3 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 GenericArray (org.apache.avro.generic.GenericArray)3 Utf8 (org.apache.avro.util.Utf8)3 DocumentFieldType (org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)3 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)2