Example usage for com.mongodb BasicDBObject get

List of usage examples for com.mongodb BasicDBObject get

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject get.

Prototype

public Object get(final String key) 

Source Link

Document

Gets a value from this object

Usage

From source file:com.github.maasdi.mongo.wrapper.field.MongoField.java

License:Apache License

/**
 * Convert a mongo record object to a Kettle field value (for the field defined by this path)
 *
 * @param mongoObject//from   www . j a v  a2 s .c  o  m
 *          the record to convert
 * @return the kettle field value
 * @throws KettleException
 *           if a problem occurs
 */
public Object convertToKettleValue(BasicDBObject mongoObject) throws KettleException {

    if (mongoObject == null) {
        return null;
    }

    if (m_tempParts.size() == 0) {
        throw new KettleException(BaseMessages.getString(PKG, "MongoDbInput.ErrorMessage.MalformedPathRecord")); //$NON-NLS-1$
    }

    String part = m_tempParts.remove(0);

    if (part.charAt(0) == '[') {
        // we're not expecting an array at this point - this document does not
        // contain our field
        return null;
    }

    if (part.indexOf('[') > 0) {
        String arrayPart = part.substring(part.indexOf('['));
        part = part.substring(0, part.indexOf('['));

        // put the array section back into location zero
        m_tempParts.add(0, arrayPart);
    }

    // part is a named field of this record
    Object fieldValue = mongoObject.get(part);
    if (fieldValue == null) {
        return null;
    }

    // what have we got
    if (m_tempParts.size() == 0) {
        // we're expecting a leaf primitive - lets see if that's what we have
        // here...
        return getKettleValue(fieldValue);
    }

    if (fieldValue instanceof BasicDBObject) {
        return convertToKettleValue(((BasicDBObject) fieldValue));
    }

    if (fieldValue instanceof BasicDBList) {
        return convertToKettleValue(((BasicDBList) fieldValue));
    }

    // must mean we have a primitive here, but we're expecting to process more
    // path so this doesn't match us - return null
    return null;
}

From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java

License:Apache License

private static void processRecord(BasicDBObject rec, String path, String name, Map<String, MongoField> lookup) {
    for (String key : rec.keySet()) {
        Object fieldValue = rec.get(key);

        if (fieldValue instanceof BasicDBObject) {
            processRecord((BasicDBObject) fieldValue, path + "." + key, name + "." //$NON-NLS-1$ //$NON-NLS-2$
                    + key, lookup);/*from  w w  w  .j  av a 2s.  c o m*/
        } else if (fieldValue instanceof BasicDBList) {
            processList((BasicDBList) fieldValue, path + "." + key, name + "." //$NON-NLS-1$ //$NON-NLS-2$
                    + key, lookup);
        } else {
            // some sort of primitive
            String finalPath = path + "." + key; //$NON-NLS-1$
            String finalName = name + "." + key; //$NON-NLS-1$
            if (!lookup.containsKey(finalPath)) {
                MongoField newField = new MongoField();
                int kettleType = mongoToKettleType(fieldValue);
                // Following suit of mongoToKettleType by interpreting null as String type
                newField.m_mongoType = String.class;
                if (fieldValue != null) {
                    newField.m_mongoType = fieldValue.getClass();
                }
                newField.m_fieldName = finalName;
                newField.m_fieldPath = finalPath;
                newField.m_kettleType = ValueMeta.getTypeDesc(kettleType);
                newField.m_percentageOfSample = 1;

                lookup.put(finalPath, newField);
            } else {
                // update max indexes in array parts of name
                MongoField m = lookup.get(finalPath);
                Class<?> fieldClass = String.class;
                if (fieldValue != null) {
                    fieldClass = fieldValue.getClass();
                }
                if (!m.m_mongoType.isAssignableFrom(fieldClass)) {
                    m.m_disparateTypes = true;
                }
                m.m_percentageOfSample++;
                updateMaxArrayIndexes(m, finalName);
            }
        }
    }
}

From source file:com.groupon.jenkins.mongo.MongoRepository.java

License:Open Source License

protected ObjectId saveOrUpdate(BasicDBObject query, BasicDBObject doc) {
    DBObject exisistingObject = findOne(query);
    if (exisistingObject == null) {
        save(doc);/*ww  w  . ja  va  2  s  .c o  m*/
        return (ObjectId) doc.get("_id");
    } else {
        update(query, doc);
        return (ObjectId) exisistingObject.get("_id");
    }
}

From source file:com.hangum.tadpole.mongodb.core.editors.mapreduce.MapReduceEditor.java

License:Open Source License

/**
 * execute map reduce/*from   www . j av a  2 s  . c  o m*/
 */
private void executeMapReduce() throws Exception {
    String strMap = textMap.getText();
    String strReduce = textReduce.getText();
    String strFinilize = textFinalize.getText();
    String strOutputTarget = textOutputTarget.getText();
    MapReduceCommand.OutputType outputType = (MapReduceCommand.OutputType) comboOutputType
            .getData(comboOutputType.getText());

    DBObject dbQuery = null;
    if (!"".equals(textQuery.getText()))
        dbQuery = (DBObject) JSON.parse(textQuery.getText());

    DBObject dbSort = null;
    if (!"".equals(textSort.getText()))
        dbSort = (DBObject) JSON.parse(textSort.getText());

    //  .
    DBCollection dbCol = MongoDBQuery.findCollection(userDB, initColName);
    MapReduceCommand mrCmd = new MapReduceCommand(dbCol, strMap, strReduce, strOutputTarget, outputType,
            dbQuery);
    if (!"".equals(strFinilize))
        mrCmd.setFinalize(strFinilize);
    if (dbSort != null)
        mrCmd.setSort(dbSort);
    if (getLimit() > 0)
        mrCmd.setLimit(getLimit());
    if (btnJsMode.getSelection())
        mrCmd.addExtraOption("jsMode", true);

    final BasicDBObject searchObj = (BasicDBObject) mrCmd.toDBObject();
    if (btnSharded.getSelection())
        ((BasicDBObject) searchObj.get("out")).put("sharded", true);
    if (btnNoneAtomic.getSelection())
        ((BasicDBObject) searchObj.get("out")).put("nonAtomic", true);

    goMapReduce(dbCol, searchObj, outputType);
}

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestCurrentOp.java

License:Open Source License

/**
 * @param args//w ww.j  ava 2s  .c o  m
 */
public static void main(String[] args) throws Exception {
    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");

    DBObject dbObj = (DBObject) db.eval("db.currentOp()");
    System.out.println(JSONUtil.getPretty(dbObj.toString()));

    BasicDBList dbInprogs = (BasicDBList) dbObj.get("inprog");
    for (Object object : dbInprogs) {
        BasicDBObject obj = (BasicDBObject) object;
        System.out.println("[opid]" + obj.get("opid"));

        //         System.out.println("##[start] killOp##############");
        //         db.eval("db.killOp(" + obj.get("opid") + ")");
        //         System.out.println("##[stop] killOp##############");
    }

    mongo.close();
}

From source file:com.hangum.tadpole.mongodb.core.utils.MongoDBTableColumn.java

License:Open Source License

/**
 * sub column  .//from  w  w  w  . j av  a  2 s .  co m
 * 
 * @param column
 * @param dbObject
 */
private static void makeTableColumn(CollectionFieldDAO column, BasicDBObject dbObject) {
    Set<String> names = dbObject.keySet();

    List<CollectionFieldDAO> listChildField = new ArrayList<CollectionFieldDAO>();
    for (String name : names) {
        CollectionFieldDAO columnSub = new CollectionFieldDAO(name, //$NON-NLS-1$
                dbObject.get(name) != null ? dbObject.get(name).getClass().getSimpleName() : "Unknow", //$NON-NLS-1$
                "NO"); //$NON-NLS-1$

        if (dbObject.get(name) instanceof BasicDBObject) {
            makeTableColumn(columnSub, (BasicDBObject) dbObject.get(name));
        }

        listChildField.add(columnSub);
    }

    column.setChildren(listChildField);
}

From source file:com.heisenberg.mongo.MongoCollection.java

License:Apache License

public void writeListElementOpt(BasicDBObject o, String fieldName, Object element) {
    if (element != null) {
        @SuppressWarnings("unchecked")
        List<Object> list = (List<Object>) o.get(fieldName);
        if (list == null) {
            list = new ArrayList<>();
            o.put(fieldName, list);//from   w ww. j a  va 2 s  . c  om
        }
        list.add(element);
    }
}

From source file:com.heisenberg.mongo.MongoCollection.java

License:Apache License

@SuppressWarnings("unchecked")
protected List<BasicDBObject> readList(BasicDBObject dbScope, String fieldName) {
    return (List<BasicDBObject>) dbScope.get(fieldName);
}

From source file:com.heisenberg.mongo.MongoCollection.java

License:Apache License

protected Object readObject(BasicDBObject dbObject, String fieldName) {
    return dbObject.get(fieldName);
}

From source file:com.heisenberg.mongo.MongoCollection.java

License:Apache License

protected BasicDBObject readBasicDBObject(BasicDBObject dbObject, String fieldName) {
    return (BasicDBObject) dbObject.get(fieldName);
}