Search in sources :

Example 21 with BSONObject

use of org.bson.BSONObject in project mongo-hadoop by mongodb.

the class BSONLoader method getNext.

@Override
public Tuple getNext() throws IOException {
    BSONObject val;
    try {
        if (!in.nextKeyValue()) {
            return null;
        }
        val = (BSONObject) in.getCurrentValue();
        Tuple t;
        if (this.fields == null) {
            // dynamic schema mode - just output a tuple with a single element,
            // which is a map storing the keys/vals in the document
            t = tupleFactory.newTuple(1);
            t.set(0, BSONLoader.convertBSONtoPigType(val));
        } else {
            t = tupleFactory.newTuple(fields.length);
            for (int i = 0; i < fields.length; i++) {
                String fieldTemp = fields[i].getName();
                if (this.idAlias != null && this.idAlias.equals(fieldTemp)) {
                    fieldTemp = "_id";
                }
                t.set(i, BSONLoader.readField(val.get(fieldTemp), fields[i]));
            }
        }
        return t;
    } catch (InterruptedException e) {
        throw new ExecException("Error while reading input", 6018);
    }
}
Also used : ExecException(org.apache.pig.backend.executionengine.ExecException) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) Tuple(org.apache.pig.data.Tuple)

Example 22 with BSONObject

use of org.bson.BSONObject in project mongo-hadoop by mongodb.

the class BSONLoader method readField.

/**
     * Convert an object from a MongoDB document into a type that Pig can
     * understand, based on the expectations of the given schema.
     * @param obj object from a MongoDB document
     * @param field the schema describing this field
     * @return an object appropriate for Pig
     * @throws IOException
     */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected static Object readField(final Object obj, final ResourceFieldSchema field) throws IOException {
    if (obj == null) {
        return null;
    }
    try {
        if (field == null) {
            return obj;
        }
        switch(field.getType()) {
            case DataType.INTEGER:
                return Integer.parseInt(obj.toString());
            case DataType.LONG:
                return Long.parseLong(obj.toString());
            case DataType.FLOAT:
                return Float.parseFloat(obj.toString());
            case DataType.DOUBLE:
                return Double.parseDouble(obj.toString());
            case DataType.BYTEARRAY:
                return BSONLoader.convertBSONtoPigType(obj);
            case DataType.CHARARRAY:
                return obj.toString();
            case DataType.DATETIME:
                return new DateTime(obj);
            case DataType.TUPLE:
                ResourceSchema s = field.getSchema();
                ResourceFieldSchema[] fs = s.getFields();
                Tuple t = tupleFactory.newTuple(fs.length);
                BasicDBObject val = (BasicDBObject) obj;
                for (int j = 0; j < fs.length; j++) {
                    t.set(j, readField(val.get(fs[j].getName()), fs[j]));
                }
                return t;
            case DataType.BAG:
                s = field.getSchema();
                fs = s.getFields();
                s = fs[0].getSchema();
                fs = s.getFields();
                DataBag bag = bagFactory.newDefaultBag();
                BasicDBList vals = (BasicDBList) obj;
                for (Object val1 : vals) {
                    t = tupleFactory.newTuple(fs.length);
                    for (int k = 0; k < fs.length; k++) {
                        t.set(k, readField(((BasicDBObject) val1).get(fs[k].getName()), fs[k]));
                    }
                    bag.add(t);
                }
                return bag;
            case DataType.MAP:
                s = field.getSchema();
                fs = s != null ? s.getFields() : null;
                Map outputMap = new HashMap();
                if (obj instanceof BSONObject) {
                    BasicBSONObject inputMap = (BasicBSONObject) obj;
                    for (String key : inputMap.keySet()) {
                        if (fs != null) {
                            outputMap.put(key, readField(inputMap.get(key), fs[0]));
                        } else {
                            outputMap.put(key, readField(inputMap.get(key), null));
                        }
                    }
                } else if (obj instanceof DBRef) {
                    DBRef ref = (DBRef) obj;
                    outputMap.put("$ref", ref.getCollectionName());
                    outputMap.put("$id", ref.getId().toString());
                }
                return outputMap;
            default:
                LOG.info("asfkjabskfjbsaf default for " + field.getName());
                return BSONLoader.convertBSONtoPigType(obj);
        }
    } catch (Exception e) {
        String fieldName = field.getName() == null ? "" : field.getName();
        String type = DataType.genTypeToNameMap().get(field.getType());
        LOG.warn("Type " + type + " for field " + fieldName + " can not be applied to " + obj.getClass().toString());
        return null;
    }
}
Also used : ResourceSchema(org.apache.pig.ResourceSchema) DataBag(org.apache.pig.data.DataBag) HashMap(java.util.HashMap) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) DBRef(com.mongodb.DBRef) DateTime(org.joda.time.DateTime) ExecException(org.apache.pig.backend.executionengine.ExecException) IOException(java.io.IOException) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) BasicBSONObject(org.bson.BasicBSONObject) ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) HashMap(java.util.HashMap) Map(java.util.Map) Tuple(org.apache.pig.data.Tuple)

Example 23 with BSONObject

use of org.bson.BSONObject in project mongo-hadoop by mongodb.

the class MongoLoader method getNext.

@Override
public Tuple getNext() throws IOException {
    BSONObject val;
    try {
        if (!in.nextKeyValue()) {
            return null;
        }
        val = (BSONObject) in.getCurrentValue();
    } catch (Exception ie) {
        throw new IOException(ie);
    }
    Tuple t;
    if (fields == null) {
        // dynamic schema mode - just output a tuple with a single element,
        // which is a map storing the keys/values in the document
        // Since there is no schema, no projection can be made, and
        // there's no need to worry about retrieving projected fields.
        t = tupleFactory.newTuple(1);
        t.set(0, BSONLoader.convertBSONtoPigType(val));
    } else {
        // A schema was provided. Try to retrieve the projection.
        int tupleSize;
        if (projectedFields != null) {
            tupleSize = projectedFields.size();
        } else {
            tupleSize = fields.length;
        }
        t = tupleFactory.newTuple(tupleSize);
        for (int i = 0; i < t.size(); i++) {
            String fieldTemp;
            ResourceFieldSchema fieldSchema;
            if (null == projectedFields) {
                fieldTemp = fields[i].getName();
                fieldSchema = fields[i];
                if (idAlias != null && idAlias.equals(fieldTemp)) {
                    fieldTemp = "_id";
                }
            } else {
                fieldTemp = projectedFields.get(i);
                // Use id alias in order to retrieve type info.
                if (idAlias != null && "_id".equals(fieldTemp)) {
                    fieldSchema = schemaMapping.get(idAlias);
                } else {
                    fieldSchema = schemaMapping.get(fieldTemp);
                }
            }
            t.set(i, BSONLoader.readField(val.get(fieldTemp), fieldSchema));
        }
    }
    return t;
}
Also used : ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) IOException(java.io.IOException) FrontendException(org.apache.pig.impl.logicalLayer.FrontendException) IOException(java.io.IOException) Tuple(org.apache.pig.data.Tuple)

Example 24 with BSONObject

use of org.bson.BSONObject in project mongo-hadoop by mongodb.

the class MongoUpdateOutputReader method initializeOutputWritable.

private void initializeOutputWritable(final BasicBSONObject value) throws IOException {
    if (!(value.containsField("modifiers") && value.containsField("_id"))) {
        outputWritable.setQuery(value);
        return;
    }
    Object query = value.get("_id");
    if (query instanceof BasicBSONObject) {
        outputWritable.setQuery((BasicBSONObject) query);
    } else {
        throw new IOException("_id must be a document describing the query of the update, not " + query);
    }
    Object modifiers = value.get("modifiers");
    if (modifiers instanceof BasicBSONObject) {
        outputWritable.setModifiers((BasicBSONObject) modifiers);
    } else {
        throw new IOException("modifiers must be a replacement or update document, not" + modifiers);
    }
    Object options = value.get("options");
    if (options instanceof BSONObject) {
        BSONObject updateOptions = (BSONObject) value.get("options");
        outputWritable.setUpsert(getBoolean(updateOptions, "upsert", true));
        outputWritable.setMultiUpdate(getBoolean(updateOptions, "multi", false));
        outputWritable.setReplace(getBoolean(updateOptions, "replace", false));
    } else if (options != null) {
        throw new IOException("options must either be null or a document providing update " + "options, not " + options);
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) IOException(java.io.IOException)

Example 25 with BSONObject

use of org.bson.BSONObject in project mongo-java-driver by mongodb.

the class DBObjectCodec method writeValue.

@SuppressWarnings("unchecked")
private void writeValue(final BsonWriter bsonWriter, final EncoderContext encoderContext, final Object initialValue) {
    Object value = BSON.applyEncodingHooks(initialValue);
    if (value == null) {
        bsonWriter.writeNull();
    } else if (value instanceof DBRef) {
        encodeDBRef(bsonWriter, (DBRef) value);
    } else if (value instanceof Map) {
        encodeMap(bsonWriter, (Map<String, Object>) value);
    } else if (value instanceof Iterable) {
        encodeIterable(bsonWriter, (Iterable) value);
    } else if (value instanceof BSONObject) {
        encodeBsonObject(bsonWriter, ((BSONObject) value));
    } else if (value instanceof CodeWScope) {
        encodeCodeWScope(bsonWriter, (CodeWScope) value);
    } else if (value instanceof byte[]) {
        encodeByteArray(bsonWriter, (byte[]) value);
    } else if (value.getClass().isArray()) {
        encodeArray(bsonWriter, value);
    } else if (value instanceof Symbol) {
        bsonWriter.writeSymbol(((Symbol) value).getSymbol());
    } else {
        Codec codec = codecRegistry.get(value.getClass());
        codec.encode(bsonWriter, value, encoderContext);
    }
}
Also used : Codec(org.bson.codecs.Codec) CollectibleCodec(org.bson.codecs.CollectibleCodec) Symbol(org.bson.types.Symbol) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) HashMap(java.util.HashMap) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) Map(java.util.Map) BsonTypeCodecMap(org.bson.codecs.BsonTypeCodecMap) CodeWScope(org.bson.types.CodeWScope)

Aggregations

BSONObject (org.bson.BSONObject)66 BasicBSONObject (org.bson.BasicBSONObject)31 BasicDBObject (com.mongodb.BasicDBObject)19 DBObject (com.mongodb.DBObject)10 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)6 LazyBSONObject (org.bson.LazyBSONObject)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 BSONFileSplit (com.mongodb.hadoop.input.BSONFileSplit)4 BSONWritable (com.mongodb.hadoop.io.BSONWritable)4 DBCollection (com.sequoiadb.base.DBCollection)4 Map (java.util.Map)4 ObjectId (org.bson.types.ObjectId)4 DBRef (com.mongodb.DBRef)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Configuration (org.apache.hadoop.conf.Configuration)3 Tuple (org.apache.pig.data.Tuple)3