Example usage for com.mongodb DBCursor next

List of usage examples for com.mongodb DBCursor next

Introduction

In this page you can find the example usage for com.mongodb DBCursor next.

Prototype

@Override
public DBObject next() 

Source Link

Document

Returns the object the cursor is at and moves the cursor ahead by one.

Usage

From source file:com.bosscs.spark.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

/**
 * Gets shards./*from w w w . j a v a 2s .  com*/
 *
 * @param collection the collection
 * @return the shards
 */
private Map<String, String[]> getShards(DBCollection collection) {
    DB config = collection.getDB().getSisterDB("config");
    DBCollection configShards = config.getCollection("shards");

    DBCursor cursorShards = configShards.find();

    Map<String, String[]> map = new HashMap<>();
    while (cursorShards.hasNext()) {
        DBObject currentShard = cursorShards.next();
        String currentHost = (String) currentShard.get("host");
        int slashIndex = currentHost.indexOf("/");
        if (slashIndex > 0) {
            map.put((String) currentShard.get(MONGO_DEFAULT_ID),
                    currentHost.substring(slashIndex + 1).split(","));
        }
    }
    return map;
}

From source file:com.bosscs.spark.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

/**
 * Calculates shard chunks.// w ww. j  a  va 2 s  .c  o m
 *
 * @param collection the collection
 * @return the deep partition [ ]
 */
private HadoopPartition[] calculateShardChunks(DBCollection collection) {

    DBCursor chuncks = getChunks(collection);

    Map<String, String[]> shards = getShards(collection);

    MongoPartition[] deepPartitions = new MongoPartition[chuncks.count()];
    int i = 0;
    boolean keyAssigned = false;
    String key = null;
    while (chuncks.hasNext()) {

        DBObject dbObject = chuncks.next();
        if (!keyAssigned) {
            Set<String> keySet = ((DBObject) dbObject.get("min")).keySet();
            for (String s : keySet) {
                key = s;
                keyAssigned = true;
            }
        }
        deepPartitions[i] = new MongoPartition(
                mongoDeepJobConfig.getRddId(), i, new TokenRange(shards.get(dbObject.get("shard")),
                        ((DBObject) dbObject.get("min")).get(key), ((DBObject) dbObject.get("max")).get(key)),
                key);
        i++;
    }
    List<MongoPartition> mongoPartitions = Arrays.asList(deepPartitions);

    Collections.shuffle(mongoPartitions);
    return mongoPartitions.toArray(new MongoPartition[mongoPartitions.size()]);
}

From source file:com.bugull.mongo.fs.BuguFS.java

License:Apache License

private List<GridFSDBFile> toFileList(DBCursor cursor) {
    List<GridFSDBFile> list = new ArrayList<GridFSDBFile>();
    while (cursor.hasNext()) {
        DBObject dbo = cursor.next();
        list.add((GridFSDBFile) dbo);/*from  ww w  .  j  a va  2 s  . c o m*/
    }
    cursor.close();
    return list;
}

From source file:com.bugull.mongo.utils.MapperUtil.java

License:Apache License

public static <T> List<T> toList(Class<T> clazz, DBCursor cursor) {
    List<T> list = new ArrayList<T>();
    while (cursor.hasNext()) {
        DBObject dbo = cursor.next();
        list.add(fromDBObject(clazz, dbo));
    }/*from   w  w  w  .j a  v a 2  s  . co m*/
    cursor.close();
    return list;
}

From source file:com.buzz.buzzdata.MongoBuzz.java

private String getBuzz(BasicDBObject query, Double lat, Double lng) {
    String retval = "";
    DBCollection buzzCollection = mongoDB.getCollection("BuzzInfo");
    DBObject sort = new BasicDBObject();
    sort.put("modified", -1);
    DBCursor cursor = buzzCollection.find(query).sort(sort);
    try {//from  w  w  w  . j av a  2  s  . com
        while (cursor.hasNext()) {
            //get buzzid
            DBObject buzz_obj = cursor.next();
            ObjectId buzz_id = (ObjectId) buzz_obj.get("_id");
            //get images for buzzid
            GridFS gridFS = new GridFS(mongoDB);
            BasicDBObject check_images = new BasicDBObject("BuzzID", buzz_id);
            List<GridFSDBFile> dbfiles = gridFS.find(check_images);
            String image_links = "";
            for (GridFSDBFile file : dbfiles) {
                String _buzz_id = buzz_id.toString();
                String pic_num = file.get("PicNum").toString();

                if (!image_links.equals("")) {
                    image_links += ",http://192.168.0.11:8080/BuzzRestAPI/webresources/buzz/Image?buzzid="
                            + _buzz_id + "&pic_num=" + pic_num;
                } else {
                    image_links += "http://192.168.0.11:8080/BuzzRestAPI/webresources/buzz/Image?buzzid="
                            + _buzz_id + "&pic_num=" + pic_num;
                }
            }
            String imgs = "\"Images\": " + "\"" + image_links + "\"";
            retval += buzz_obj;
            retval = retval.substring(0, retval.length() - 1);
            retval += ", " + imgs;
            double lat2 = (double) ((BasicDBList) buzz_obj.get("loc")).get(0);
            double lng2 = (double) ((BasicDBList) buzz_obj.get("loc")).get(1);
            double dist = this.haversine(lat, lng, lat2, lng2);
            retval += ", \"Distance\": " + dist;
            String directions_url = "https://maps.google.com/maps?saddr=" + lat + "," + lng + "&daddr=" + lat2
                    + "," + lng2 + "&hl=en&sll=" + lat + "," + lng + "&sspn=" + lat2 + "," + lng2
                    + "&t=m&mra=mift&mrsp=1&sz=5&z=18";
            retval += ", \"Directions\": \"" + directions_url + "\"";
            retval += "},";
        }
    } catch (Exception exc) {
    } finally {
        cursor.close();
    }
    retval = retval.substring(0, retval.length() - 1);
    retval = "callback({\"Buzzes\": [" + retval + "]})";
    return retval;
}

From source file:com.buzz.buzzdata.MongoBuzz.java

private String executeQueries(BasicDBObject[] queries, String combine, String collName) {
    String retval = "";
    DBCollection buzzCollection = mongoDB.getCollection(collName);
    BasicDBObject filters = new BasicDBObject(combine, queries);
    DBCursor cursor = buzzCollection.find(filters);
    try {//from   w w  w . ja va 2  s . c  o m
        while (cursor.hasNext()) {
            retval += cursor.next();
            retval += "\n";
        }
    } finally {
        cursor.close();
    }
    return retval;
}

From source file:com.ca.apm.mongo.ShardCluster.java

License:Open Source License

private List<String> getShardsFromConfig(final String host, final int port) {

    final List<String> shardList = new ArrayList<String>();

    MongoClient dbClient = null;//from ww w  .j a  va 2 s .c o  m

    try {
        dbClient = setupDbClient(host, port);

        final DB configDB = dbClient.getDB("config");
        final DBCursor shardsCursor = configDB.getCollectionFromString("shards").find();
        while (shardsCursor.hasNext()) {
            final DBObject dbo = shardsCursor.next();
            String shards = (String) dbo.get("host");
            String[] shardMembers = getShardMembers(shards);
            for (String member : shardMembers) {
                shardList.add(member);
            }
        }
    } catch (Exception e) {
        logger.log(Level.WARNING, "Exception getting shards from cfg servers: {0}", e);
    } finally {
        if (dbClient != null) {
            dbClient.close();
        }
    }
    return shardList;
}

From source file:com.ca.apm.mongo.ShardCluster.java

License:Open Source License

private List<String> getMongosFromConfig(final String host, final int port) {

    final List<String> shardRouters = new ArrayList<String>();

    MongoClient dbClient = null;//ww  w. ja v  a  2  s  .  c o  m

    try {
        dbClient = setupDbClient(host, port);
        final DB configDB = dbClient.getDB("config");
        final DBCursor mongosCursor = configDB.getCollectionFromString("mongos").find();
        while (mongosCursor.hasNext()) {
            final DBObject dbo = mongosCursor.next();
            final String mongos = (String) dbo.get("_id");
            shardRouters.add(mongos);
        }
    } catch (Exception e) {
        logger.log(Level.WARNING, "Exception getting mongos from cfg server(s): {0}", e);
    } finally {
        if (dbClient != null) {
            dbClient.close();
        }
    }
    return shardRouters;
}

From source file:com.caci.dummyserver.MongoRepository.java

public GetObjectsResult getObjects(String table, Collection<Pair<String, String>> keys,
        Collection<String> fields, Collection<Pair<String, Collection<String>>> filters,
        Collection<String> sort, Integer offset, Integer limit) {
    DB db = mongo.getDB("Objects");
    DBCollection col = db.getCollection(table);

    GetObjectsResult result = new GetObjectsResult();

    DBObject queryObject = makeQueryObject(keys, filters);
    DBCursor cursor = col.find(queryObject);
    try {/* ww  w .j  a va 2  s . c om*/
        if (sort != null && !sort.isEmpty()) {
            DBObject sortObj = new BasicDBObject();
            for (String fieldName : sort) {
                if (fieldName.startsWith("-")) {
                    sortObj.put("data." + fieldName.substring(1), -1);
                } else {
                    sortObj.put("data." + fieldName, 1);
                }
            }
            cursor.sort(sortObj);
        }

        result.setTotal(cursor.count());

        if (offset != null && offset > 0) {
            cursor.skip(offset);
        }
        if (limit != null && limit > 0) {
            cursor.limit(limit);
        }

        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            obj = pruneDBObject(getData(obj), fields);
            result.getResults().add(obj.toString());
        }
    } finally {
        cursor.close();
    }

    return result;
}

From source file:com.callidusrobotics.droptables.model.DocumentDao.java

License:Open Source License

protected List<DBObject> getDocuments(DBCursor cursor) {
    List<DBObject> result = new LinkedList<DBObject>();
    try {//from   w  w w .  j  av a  2s .c  o  m
        while (cursor.hasNext()) {
            result.add(flatten(cursor.next()));
        }
    } finally {
        cursor.close();
    }

    return result;
}