Java Code Examples for com.mongodb.DBCursor#count()
The following examples show how to use
com.mongodb.DBCursor#count() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoNativeExtractor.java From deep-spark with Apache License 2.0 | 5 votes |
/** * Calculates shard chunks. * * @param collection the collection * @return the deep partition [ ] */ private DeepPartition[] 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 DeepTokenRange(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()]); }
Example 2
Source File: DataServlet.java From BLELocalization with MIT License | 4 votes |
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) * * Get resource */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (mDS != MongoService.getInstance()) { System.out.println("MongoService db name is changed"); mDS = MongoService.getInstance(); mCollRef = mDS.getCollection("refpoints"); mCollSamp = mDS.getCollection("samplings"); mCollMap = mDS.getCollection("maps"); } String type = request.getParameter("type"); String id = request.getParameter("id"); System.out.println("doGet: type=" + type + " id=" + id); if ("file".equals(type)) { if (id == null) { // Get list of files mDS.sendJSON(mDS.getFileList(), request, response); return; } try { mDS.sendFile(id, response); } catch (Exception e) { System.err.println("Send error: " + id); } return; } // Get document(s) String distinct = request.getParameter("distinct"); String pipeline = request.getParameter("pipeline"); String query = request.getParameter("query"); String keys = request.getParameter("keys"); String format = request.getParameter("format"); DBObject queryObj = query != null ? (DBObject) JSON.parse(query) : null; DBObject keysObj = keys != null ? (DBObject) JSON.parse(keys) : null; Object result = null; DBCollection collection = mDS.getCollection(type); if (id != null) { result = collection.findOne(new ObjectId(id), keysObj); } else if (distinct != null) { result = collection.distinct(distinct, queryObj); } else if (pipeline != null) { DBObject pipelineObj = (DBObject) JSON.parse(pipeline); if (pipelineObj instanceof List<?>) { result = collection.aggregate((List<DBObject>) pipelineObj).results(); } else { result = collection.aggregate(pipelineObj).results(); } } else { DBCursor cursor = collection.find(queryObj, keysObj); String sort = request.getParameter("sort"); String skip = request.getParameter("skip"); String limit = request.getParameter("limit"); String count = request.getParameter("count"); if (sort != null) { cursor = cursor.sort((DBObject) JSON.parse(sort)); } if (skip != null) { cursor = cursor.skip(Integer.parseInt(skip)); } if (limit != null) { cursor = cursor.limit(Integer.parseInt(limit)); } result = "true".equals(count) ? cursor.count() : cursor; } if (id != null && result == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Document %s does not exist", id)); return; } if ("csv".equals(format)) { mDS.sendCSV(result, request, response); return; } mDS.sendJSON(result, request, response); }