Search in sources :

Example 26 with Utf8

use of org.apache.avro.util.Utf8 in project gora by apache.

the class MapFieldValueFilter method readFields.

@Override
public void readFields(DataInput in) throws IOException {
    fieldName = Text.readString(in);
    mapKey = new Utf8(Text.readString(in));
    filterOp = WritableUtils.readEnum(in, FilterOp.class);
    operands.clear();
    int operandsSize = WritableUtils.readVInt(in);
    for (int i = 0; i < operandsSize; i++) {
        Object operand = ObjectWritable.readObject(in, conf);
        if (operand instanceof String) {
            operand = new Utf8((String) operand);
        }
        operands.add(operand);
    }
    filterIfMissing = in.readBoolean();
}
Also used : Utf8(org.apache.avro.util.Utf8)

Example 27 with Utf8

use of org.apache.avro.util.Utf8 in project gora by apache.

the class SingleFieldValueFilter method readFields.

@Override
public void readFields(DataInput in) throws IOException {
    fieldName = Text.readString(in);
    filterOp = WritableUtils.readEnum(in, FilterOp.class);
    operands.clear();
    int operandsSize = WritableUtils.readVInt(in);
    for (int i = 0; i < operandsSize; i++) {
        Object operand = ObjectWritable.readObject(in, conf);
        if (operand instanceof String) {
            operand = new Utf8((String) operand);
        }
        operands.add(operand);
    }
    filterIfMissing = in.readBoolean();
}
Also used : Utf8(org.apache.avro.util.Utf8)

Example 28 with Utf8

use of org.apache.avro.util.Utf8 in project gora by apache.

the class MongoStore method fromMongoString.

private Object fromMongoString(final DocumentFieldType storeType, final String docf, final BSONDecorator easybson) {
    Object result;
    if (storeType == DocumentFieldType.OBJECTID) {
        // Try auto-conversion of BSON data to ObjectId
        // It will work if data is stored as String or as ObjectId
        Object bin = easybson.get(docf);
        if (bin instanceof String) {
            ObjectId id = new ObjectId((String) bin);
            result = new Utf8(id.toString());
        } else {
            result = new Utf8(bin.toString());
        }
    } else if (storeType == DocumentFieldType.DATE) {
        Object bin = easybson.get(docf);
        if (bin instanceof Date) {
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.getDefault());
            calendar.setTime((Date) bin);
            result = new Utf8(DatatypeConverter.printDateTime(calendar));
        } else {
            result = new Utf8(bin.toString());
        }
    } else {
        result = easybson.getUtf8String(docf);
    }
    return result;
}
Also used : ObjectId(org.bson.types.ObjectId) Utf8(org.apache.avro.util.Utf8)

Example 29 with Utf8

use of org.apache.avro.util.Utf8 in project gora by apache.

the class MongoStore method fromMongoMap.

/* pp */
Object fromMongoMap(final String docf, final Schema fieldSchema, final BSONDecorator easybson, final Field f) {
    BasicDBObject map = easybson.getDBObject(docf);
    Map<Utf8, Object> rmap = new HashMap<>();
    if (map == null) {
        return new DirtyMapWrapper(rmap);
    }
    for (Entry<String, Object> e : map.entrySet()) {
        String mapKey = e.getKey();
        String decodedMapKey = decodeFieldKey(mapKey);
        DocumentFieldType storeType = mapping.getDocumentFieldType(docf);
        Object o = fromDBObject(fieldSchema.getValueType(), storeType, f, mapKey, new BSONDecorator(map));
        rmap.put(new Utf8(decodedMapKey), o);
    }
    return new DirtyMapWrapper<>(rmap);
}
Also used : DirtyMapWrapper(org.apache.gora.persistency.impl.DirtyMapWrapper) DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType) BSONDecorator(org.apache.gora.mongodb.utils.BSONDecorator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Utf8(org.apache.avro.util.Utf8)

Example 30 with Utf8

use of org.apache.avro.util.Utf8 in project gora by apache.

the class HBaseStore method setField.

private void setField(Result result, T persistent, HBaseColumn col, Field field, Schema fieldSchema) throws IOException {
    switch(fieldSchema.getType()) {
        case UNION:
            int index = getResolvedUnionIndex(fieldSchema);
            if (index > 1) {
                //if more than 2 type in union, deserialize directly for now
                byte[] val = result.getValue(col.getFamily(), col.getQualifier());
                if (val == null) {
                    return;
                }
                setField(persistent, field, val);
            } else {
                Schema resolvedSchema = fieldSchema.getTypes().get(index);
                setField(result, persistent, col, field, resolvedSchema);
            }
            break;
        case MAP:
            NavigableMap<byte[], byte[]> qualMap = result.getNoVersionMap().get(col.getFamily());
            if (qualMap == null) {
                return;
            }
            Schema valueSchema = fieldSchema.getValueType();
            Map<Utf8, Object> map = new HashMap<>();
            for (Entry<byte[], byte[]> e : qualMap.entrySet()) {
                map.put(new Utf8(Bytes.toString(e.getKey())), fromBytes(valueSchema, e.getValue()));
            }
            setField(persistent, field, map);
            break;
        case ARRAY:
            qualMap = result.getFamilyMap(col.getFamily());
            if (qualMap == null) {
                return;
            }
            valueSchema = fieldSchema.getElementType();
            ArrayList<Object> arrayList = new ArrayList<>();
            DirtyListWrapper<Object> dirtyListWrapper = new DirtyListWrapper<>(arrayList);
            for (Entry<byte[], byte[]> e : qualMap.entrySet()) {
                dirtyListWrapper.add(fromBytes(valueSchema, e.getValue()));
            }
            setField(persistent, field, arrayList);
            break;
        default:
            byte[] val = result.getValue(col.getFamily(), col.getQualifier());
            if (val == null) {
                return;
            }
            setField(persistent, field, val);
            break;
    }
}
Also used : HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) DirtyListWrapper(org.apache.gora.persistency.impl.DirtyListWrapper) Utf8(org.apache.avro.util.Utf8)

Aggregations

Utf8 (org.apache.avro.util.Utf8)123 Test (org.junit.Test)34 WebPage (org.apache.gora.examples.generated.WebPage)32 GenericRecord (org.apache.avro.generic.GenericRecord)17 Schema (org.apache.avro.Schema)14 GenericData (org.apache.avro.generic.GenericData)13 ByteBuffer (java.nio.ByteBuffer)12 HashMap (java.util.HashMap)12 Map (java.util.Map)12 Employee (org.apache.gora.examples.generated.Employee)11 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 Field (org.apache.avro.Schema.Field)6 Record (org.apache.avro.generic.GenericData.Record)5 File (java.io.File)4 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)4 Metadata (org.apache.gora.examples.generated.Metadata)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Iterator (java.util.Iterator)3 List (java.util.List)3