Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

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

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

From source file:com.ebay.oss.bark.repo.DqScheudleRepoImpl.java

License:Apache License

@Override
public void updateModelType(DBObject schedule, int type) {
    DBObject dbo = null;//from  www. j a  v a2 s.com
    if (type == ModelType.ACCURACY) {
        dbo = dbCollection.findOne(new BasicDBObject("modelList", schedule.get("modelList")));
    } else if (type == ModelType.VALIDITY) {
        dbo = getValiditySchedule(Integer.parseInt(schedule.get("assetId").toString()));
    }
    if (dbo != null)
        dbCollection.remove(dbo);
    dbCollection.save(schedule);
}

From source file:com.ebay.oss.bark.repo.SequenceRepoImpl.java

License:Apache License

@Override
public synchronized Long getNextSequence(String seq) {
    DBObject temp = dbCollection.findOne(new BasicDBObject("_id", seq));

    BasicDBObject document = new BasicDBObject();
    document.put("_id", seq);
    document.put(seq, Long.parseLong(temp.get(seq).toString()) + 1);

    dbCollection.save(document);//from   ww  w  .  j  a va 2 s.c o  m

    return new Long((Long.parseLong(temp.get(seq).toString()) + 1));
}

From source file:com.ebay.oss.bark.service.DataAssetServiceImpl.java

License:Apache License

@Override
public void updateDataAsset(DataAssetInput input) throws BarkDbOperationException {
    try {//from  w ww .ja  v a 2s .  c om

        DataAsset da = new DataAsset();

        List<Pair> queryList = new ArrayList<Pair>();
        queryList.add(new Pair("assetName", input.getAssetName()));
        queryList.add(new Pair("assetType", input.getAssetType()));
        queryList.add(new Pair("system", input.getSystem()));

        List<Pair> updateValues = new ArrayList<Pair>();
        updateValues.add(new Pair("schema", input.getSchema()));
        updateValues.add(new Pair("platform", input.getPlatform()));
        updateValues.add(new Pair("assetHDFSPath", input.getAssetHDFSPath()));
        updateValues.add(new Pair("owner", input.getOwner()));

        DBObject item = dataAssetRepo.getByCondition(queryList);
        if (item == null) {
            throw new BarkDbOperationException("The data asset doesn't exist");
        }

        da.setAssetName(input.getAssetName());
        da.setAssetType(input.getAssetType());
        da.setPlatform(input.getPlatform());
        da.setSystem(input.getSystem());
        da.setAssetHDFSPath(input.getAssetHDFSPath());
        da.setSchema(input.getSchema());
        da.setOwner(input.getOwner());
        da.setPartitions(input.getPartitions());
        da.setTimestamp(new Date());

        logger.warn("log: updated record, id is: " + (long) Double.parseDouble(item.get("_id").toString()));
        da.set_id(new Long((long) Double.parseDouble(item.get("_id").toString())));
        dataAssetRepo.update(da, item);
    } catch (Exception e) {
        throw new BarkDbOperationException("Failed to update data asset", e);
    }

}

From source file:com.ebay.oss.bark.service.DQMetricsServiceImpl.java

License:Apache License

@Override
public List<SampleOut> listSampleFile(String modelName) {

    List<SampleOut> samples = new ArrayList<SampleOut>();

    List<DBObject> dbos = missedFileRepo.findByModelName(modelName);

    for (DBObject dbo : dbos) {

        SampleOut so = new SampleOut();

        so.setDate(Long.parseLong(dbo.get("timestamp").toString()));
        so.setPath(dbo.get("hdfsPath").toString());

        samples.add(so);//from  ww w.  j  a va2  s .co m
    }

    return samples;

}

From source file:com.ebay.oss.bark.service.DqModelServiceImpl.java

License:Apache License

@Override
public int deleteModel(String name) throws BarkDbOperationException {
    try {/*from ww  w  . j  av  a2  s. c  o  m*/
        DqModel dqModel = dqModelRepo.findByName(name);

        // TODO need to mark related metrics as deleted, instead of real deletion
        // markMetricsDeleted(dqModel);

        dqModelRepo.delete(dqModel.get_id());

        if (dqModel.getModelType() == ModelType.ACCURACY) {
            scheduleRepo.deleteByModelList(name);
        } else if (dqModel.getModelType() == ModelType.VALIDITY) {
            DBObject currentSchedule = scheduleRepo.getValiditySchedule(dqModel.getAssetId());
            if (currentSchedule == null || currentSchedule.get("modelList") == null) {
                return -1;
            }

            String rawModelList = currentSchedule.get("modelList").toString();
            String newModelList = "";
            if (rawModelList.contains(ScheduleModelSeperator.SEPERATOR)) {
                String[] rawModelArray = rawModelList.split(ScheduleModelSeperator.SPLIT_SEPERATOR);
                for (int i = 0; i < rawModelArray.length; i++) {
                    if (!rawModelArray[i].equals(name))
                        newModelList = newModelList + ScheduleModelSeperator.SEPERATOR + rawModelArray[i];
                }
                newModelList = newModelList.substring(ScheduleModelSeperator.SEPERATOR.length());
                currentSchedule.put("modelList", newModelList);
                scheduleRepo.updateModelType(currentSchedule, dqModel.getModelType());

            } else if (rawModelList.equals(name)) {
                scheduleRepo.deleteByModelList(name);
            }
        }

        return 0;
    } catch (Exception e) {
        logger.warn(e.toString());
        throw new BarkDbOperationException("Failed to delete model of '" + name + "'", e);
    }
}

From source file:com.ebay.oss.bark.service.DqModelServiceImpl.java

License:Apache License

@Override
public void enableSchedule4Model(DqModel dqModel) {
    if (dqModel == null)
        return;//from w  w  w. j a v  a 2s  .c o  m

    dqModel.setStatus(ModelStatus.DEPLOYED);
    dqModelRepo.update(dqModel);

    if (dqModel.getModelType() == ModelType.ACCURACY || dqModel.getModelType() == ModelType.VALIDITY) {
        DqSchedule schedule = new DqSchedule();
        schedule.set_id(scheduleRepo.getNextId());
        schedule.setScheduleType(dqModel.getSchedule());
        Date d = new Date(dqModel.getStarttime());
        d.setMinutes(0);
        d.setSeconds(0);
        schedule.setStarttime(d.getTime() / 1000 * 1000); // FIXME ????
        schedule.setStatus(0);
        schedule.setAssetId(dqModel.getAssetId());
        schedule.setJobType(dqModel.getModelType());

        String modellist = "";
        if (dqModel.getModelType() == ModelType.VALIDITY) {
            DBObject currentSchedule = scheduleRepo.getValiditySchedule(dqModel.getAssetId());
            if (currentSchedule != null) {
                if (currentSchedule.get("modelList") != null
                        && !currentSchedule.get("modelList").toString().equals("")) {
                    modellist = currentSchedule.get("modelList").toString();
                    modellist = modellist + ScheduleModelSeperator.SEPERATOR + dqModel.getModelName();
                }
            }
            if (modellist.equals(""))
                modellist = dqModel.getModelName();
        } else
            modellist = dqModel.getModelName();
        schedule.setModelList(modellist);

        scheduleRepo.updateByModelType(schedule, dqModel.getModelType());
    }
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void doImport(final ButtonBase button) throws IOException {
    ImportDialog dia = UMongo.instance.getGlobalStore().getImportDialog();
    if (!dia.show()) {
        return;/*from w w w . jav  a2  s .c o m*/
    }
    final boolean dropCollection = dia.getBooleanFieldValue(ImportDialog.Item.dropCollection);
    final boolean continueOnError = dia.getBooleanFieldValue(ImportDialog.Item.continueOnError);
    final boolean upsert = dia.getBooleanFieldValue(ImportDialog.Item.upsert);
    final boolean bulk = dia.getBooleanFieldValue(ImportDialog.Item.bulk);
    String supsertFields = dia.getStringFieldValue(ImportDialog.Item.upsertFields);
    final String[] upsertFields = supsertFields != null ? supsertFields.split(",") : null;
    if (upsertFields != null) {
        for (int i = 0; i < upsertFields.length; ++i) {
            upsertFields[i] = upsertFields[i].trim();
        }
    }
    final DocumentDeserializer dd = dia.getDocumentDeserializer();
    final DBCollection col = getCollectionNode().getCollection();

    new DbJob() {
        @Override
        public Object doRun() throws Exception {
            try {
                if (dropCollection) {
                    col.drop();
                }
                DBObject obj = null;
                List<DBObject> batch = new ArrayList<DBObject>();
                while ((obj = dd.readObject()) != null) {
                    try {
                        if (upsert) {
                            if (upsertFields == null) {
                                col.save(obj);
                            } else {
                                BasicDBObject query = new BasicDBObject();
                                for (int i = 0; i < upsertFields.length; ++i) {
                                    String field = upsertFields[i];
                                    if (!obj.containsField(field)) {
                                        throw new Exception("Upsert field " + field + " not present in object "
                                                + obj.toString());
                                    }
                                    query.put(field, obj.get(field));
                                }
                                col.update(query, obj, true, false);
                            }
                        } else {
                            if (bulk) {
                                if (batch.size() > 1000) {
                                    col.insert(batch);
                                    batch.clear();
                                }
                                batch.add(obj);
                            } else {
                                col.insert(obj);
                            }
                        }
                    } catch (Exception e) {
                        if (continueOnError) {
                            getLogger().log(Level.WARNING, null, e);
                        } else {
                            throw e;
                        }
                    }
                }

                if (!batch.isEmpty()) {
                    col.insert(batch);
                }

            } finally {
                dd.close();
            }
            return null;
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "Import";
        }

        @Override
        public ButtonBase getButton() {
            return button;
        }
    }.addJob();
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void shardCollection(ButtonBase button) {
    FormDialog dialog = (FormDialog) ((MenuItem) getBoundUnit(Item.shardCollection)).getDialog();
    ComboBox combo = (ComboBox) getBoundUnit(Item.shardKeyCombo);
    combo.value = 0;//w  ww  .  j  a v a 2s.c  om
    List<DBObject> indices = getCollectionNode().getCollection().getIndexInfo();
    String[] items = new String[indices.size() + 1];
    items[0] = "None";
    int i = 1;
    for (DBObject index : indices) {
        items[i++] = ((DBObject) index.get("key")).toString();
    }
    combo.items = items;
    combo.structureComponent();

    if (!dialog.show()) {
        return;
    }

    DBObject key = null;
    int index = combo.getComponentIntValue();
    if (index > 0) {
        key = (DBObject) indices.get(index - 1).get("key");
    } else {
        key = ((DocBuilderField) getBoundUnit(Item.shardCustomKey)).getDBObject();
    }

    if (key == null) {
        new InfoDialog(null, "Empty key", null, "You must select a shard key").show();
        return;
    }
    if (!new ConfirmDialog(null, "Confirm shard key", null,
            "About to shard collection with key " + key + ", is it correct? This operation cannot be undone.")
                    .show()) {
        return;
    }

    boolean unique = getBooleanFieldValue(Item.shardUniqueIndex);
    DB admin = getCollectionNode().getDbNode().getDb().getSisterDB("admin");
    DBObject cmd = new BasicDBObject("shardCollection", getCollectionNode().getCollection().getFullName());
    cmd.put("key", key);
    if (unique) {
        cmd.put("unique", unique);
    }
    new DbJobCmd(admin, cmd, this, null).addJob();

}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

private Object handleSpecialFields(DBObject doc) {
    for (String field : doc.keySet()) {
        if (field.equals("__rand")) {
            String type = (String) doc.get(field);
            if (type.equals("int")) {
                int min = (Integer) doc.get("min");
                int max = (Integer) doc.get("max");
                return min + (int) (Math.random() * ((max - min) + 1));
            } else if (type.equals("str")) {
                int len = (Integer) doc.get("len");
                StringBuilder sb = new StringBuilder(len);
                byte min = 0x61;
                byte max = 0x7a;
                for (int i = 0; i < len; ++i) {
                    char c = (char) (min + (byte) (Math.random() * ((max - min) + 1)));
                    sb.append(c);//from   ww  w . j  a va2  s. c  o m
                }
                return sb.toString();
            }
        }
        Object val = doc.get(field);
        if (val instanceof BasicDBObject) {
            BasicDBObject subdoc = (BasicDBObject) val;
            Object res = handleSpecialFields(subdoc);
            if (res != null) {
                doc.put(field, res);
            }
        } else if (val instanceof BasicDBList) {
            BasicDBList sublist = (BasicDBList) val;
            handleSpecialFields(sublist);
        }
    }
    return null;
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void fixCollection(ButtonBase button) {
    final MongoClient m = getCollectionNode().getDbNode().getMongoNode().getMongoClient();
    ArrayList<MongoNode> mongoNodes = UMongo.instance.getMongos();
    String[] mongonames = new String[mongoNodes.size() - 1];
    MongoClient[] mongos = new MongoClient[mongonames.length];
    int i = 0;//from   ww  w  .  j av  a2 s  .  c  o m
    for (MongoNode node : mongoNodes) {
        MongoClient m2 = node.getMongoClient();
        if (m == m2) {
            continue;
        }
        mongonames[i] = m2.toString();
        mongos[i] = m2;
        ++i;
    }
    ComboBox src = (ComboBox) getBoundUnit(Item.fcSrcMongo);
    src.items = mongonames;
    src.structureComponent();
    FormDialog dialog = (FormDialog) getBoundUnit(Item.fcDialog);
    if (!dialog.show()) {
        return;
    }

    final DBCollection dstCol = getCollectionNode().getCollection();
    final MongoClient srcMongo = mongos[src.getIntValue()];
    final boolean upsert = getBooleanFieldValue(Item.fcUpsert);

    final String dbname = dstCol.getDB().getName();
    final String colname = dstCol.getName();
    final DBCollection srcCol = srcMongo.getDB(dbname).getCollection(colname);
    String txt = "About to copy from ";
    txt += srcMongo.getConnectPoint() + "(" + srcCol.count() + ")";
    txt += " to ";
    txt += m.getConnectPoint() + "(" + dstCol.count() + ")";
    if (!new ConfirmDialog(null, "Confirm Fix Collection", null, txt).show()) {
        return;
    }

    new DbJob() {
        @Override
        public Object doRun() {
            DBCursor cur = srcCol.find();
            int count = 0;
            int dup = 0;
            while (cur.hasNext()) {
                DBObject obj = cur.next();
                if (upsert) {
                    BasicDBObject id = new BasicDBObject("_id", obj.get("_id"));
                    dstCol.update(id, obj, true, false);
                } else {
                    try {
                        dstCol.insert(obj);
                    } catch (DuplicateKey e) {
                        // dup keys are expected here
                        ++dup;
                    }
                }
                ++count;
            }
            DBObject res = new BasicDBObject("count", count);
            res.put("dups", dup);
            return res;
        }

        @Override
        public String getNS() {
            return "*";
        }

        @Override
        public String getShortName() {
            return "Fix Collection";
        }
    }.addJob();
}