下面列出了com.mongodb.DBCursor#hasNext ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public List<Seq> getSeqWithRxnRef(Long rxnId) {
List<Seq> seqs = new ArrayList<>();
BasicDBObject query = new BasicDBObject();
query.put("rxn_refs", rxnId);
DBCursor cur = this.dbSeq.find(query, new BasicDBObject());
try {
while (cur.hasNext()) {
DBObject o = cur.next();
seqs.add(convertDBObjectToSeq(o));
}
} finally {
if (cur != null) {
cur.close();
}
}
return seqs;
}
protected final void extractBasicNode(String collectionName) {
int count = 0;
long startTime = System.currentTimeMillis();
logger.log(Level.INFO, "Building basic node for type: " + collectionName);
DBCursor cursor = mongo.getCollection(collectionName).find();
long recordCount = mongo.getCollection(collectionName).count();
while (cursor.hasNext()) {
DBObject item = cursor.next();
Vertex v = graph.addVertex(null);
logger.log(Level.FINE, "Adding vertex for {0}#{1} \t {2}",
new String[] { collectionName, (String) item.get("_id"), v.getId().toString() });
v.setProperty("mongoid", (String) item.get("_id"));
count++;
if (count % 200 == 0) {
logger.log(Level.FINE, "Importing {0} @ {1}", new String[] { collectionName, "" + count });
}
}
logger.log(Level.INFO, "\t RPS: {0}", (recordCount / (System.currentTimeMillis() - startTime)) * 1000);
}
public List<Long> getAllCollectionUUIDs(DBCollection collection) {
List<Long> ids = new ArrayList<Long>();
BasicDBObject query = new BasicDBObject();
BasicDBObject keys = new BasicDBObject();
keys.put("_id", 1); // 0 means exclude, rest are included
DBCursor cur = collection.find(query, keys);
while (cur.hasNext()) {
DBObject o = cur.next();
long uuid = (Integer)o.get("_id"); // checked: db type IS int
ids.add(uuid);
}
cur.close();
return ids;
}
/**
* Gets shards.
*
* @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;
}
private List<Seq> keywordInSequence(String in_field, String keyword) {
List<Seq> seqs = new ArrayList<Seq>();
BasicDBObject query = new BasicDBObject();
query.put(in_field, keyword);
BasicDBObject keys = new BasicDBObject();
DBCursor cur = this.dbSeq.find(query, keys);
while (cur.hasNext()) {
DBObject o = cur.next();
seqs.add( convertDBObjectToSeq(o) );
}
cur.close();
return seqs;
}
@GET
@Path("/product/{product}/{major}.{minor}.{servicePack}/{build}")
@Produces(MediaType.APPLICATION_JSON)
public Response getProductHistory(@BeanParam final Coordinates coordinates) {
List<String> buildList = new ArrayList<>();
final DBCollection collection = this.mongoLegacyDb.getCollection("summary");
final DBObject productQuery = coordinates.getQueryObject(Field.PRODUCT, Field.MAJOR, Field.MINOR, Field.SERVICEPACK);
final DBCursor results = collection.find(productQuery);
while (results.hasNext()) {
buildList = (List<String>) results.next().get("builds");
}
final DBCollection featureCollection = this.mongoLegacyDb.getCollection("features");
final List<BasicDBObject> buildDBObjectList = new ArrayList<>();
for (final String build : buildList) {
coordinates.setBuild(build);
final DBObject buildQuery = coordinates.getQueryObject();
final BasicDBObject buildObj = new BasicDBObject();
buildObj.put("passed", getNumberOfState(featureCollection, buildQuery, "passed"));
buildObj.put("failed", getNumberOfState(featureCollection, buildQuery, "failed"));
buildObj.put("skipped", getNumberOfState(featureCollection, buildQuery, "skipped"));
buildObj.put("undefined", getNumberOfState(featureCollection, buildQuery, "undefined"));
buildObj.put("name", build);
buildDBObjectList.add(buildObj);
}
return Response.ok(SerializerUtil.serialise(buildDBObjectList)).build();
}
/**
* Get all user profiles.
*
* @return All user profiles (excluding private fields like password).
*/
@GET
@Produces("application/json")
public Response getAllUsers() {
// Validate the JWT. The JWT must be in the 'users' group.
try {
validateJWT(new HashSet<String>(Arrays.asList("users")));
} catch (JWTException jwte) {
return Response.status(Status.UNAUTHORIZED)
.type(MediaType.TEXT_PLAIN)
.entity(jwte.getMessage())
.build();
}
// Get all the users from the database, and add them to an array.
DB database = mongo.getMongoDB();
DBCollection dbCollection = database.getCollection(User.DB_COLLECTION_NAME);
DBCursor cursor = dbCollection.find();
JsonArrayBuilder userArray = Json.createArrayBuilder();
while (cursor.hasNext()) {
// Exclude all private information from the list.
userArray.add((new User(cursor.next()).getPublicJsonObject()));
}
// Return the user list to the caller.
JsonObjectBuilder responseBuilder = Json.createObjectBuilder().add("users", userArray.build());
return Response.ok(responseBuilder.build(), MediaType.APPLICATION_JSON).build();
}
@Override
public KeyValueIterable<K, V> search(K startKey) {
final DBCursor cursor = collection.find(new BasicDBObject(map("key", new BasicDBObject("$gt", startKey))));
return new KeyValueIterable<K, V>() {
@Override
public void close() {
cursor.close();
}
@Override
public Iterator<Entry<K, V>> iterator() {
return new Iterator<Entry<K, V>>() {
@Override
public boolean hasNext() {
return cursor.hasNext();
}
@Override
public Entry<K, V> next() {
//TODO left off here
return null;
}
@Override
public void remove() {
}
};
}
};
}
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody Result onRootAccess() {
DBCollection collection = mongoTemplate.getCollection("test");
long count = collection.getCount();
log.info("Object count in 'test' collection before insert: " + count + "<br/> Inserting one object.<br/>");
BasicDBObject dBObject = new BasicDBObject();
dBObject.put("hello", "world");
collection.insert(dBObject);
count = collection.count();
log.info("Object count in test collection after insert:" + count);
Result result = new Result();
List<DBObject> dbObjects = new ArrayList<DBObject>();
DBCursor cursor = collection.find();
while (cursor.hasNext()) {
com.mongodb.DBObject obj = cursor.next();
final String value = (String) obj.get("hello");
DBObject object = new DBObject();
object.setKey("hello");
object.setValue(value);
dbObjects.add(object);
}
result.setDbObjects(dbObjects);
result.setStatus(
"Successfully accessed Mongodb service. Retrieving the data object inserted in test collection.");
collection.drop();
return result;
}
public static void main__(String[] args) throws Exception {
File ns_pmIds = new File(
System.getProperty("user.home")
+ "/Dropbox/dev_shared/tmp2/rrrrrabbit_fetchlist_meshNeuron_SUS.txt");
Set<Integer> ns_pmIds_set = new HashSet<Integer>();
for (String line : new LineReader(new FileInputStream(ns_pmIds))) {
ns_pmIds_set.add(Integer.parseInt(line));
}
System.out.println("done reading list");
MongoConnection mongo = new MongoConnection(MONGO_FT_CONNECTION);
// retrieve only _id
BasicDBObject keys = new BasicDBObject();
keys.put("pmid", 1);
DBCursor it = mongo.coll.find(new BasicDBObject(), keys)
.batchSize(1000);
int i = 0, flagged = 0;
while (it.hasNext()) {
DBObject dbObject = (DBObject) it.next();
int pmId = Integer.parseInt(dbObject.get("_id").toString());
if (ns_pmIds_set.contains(pmId)) {
MongoUtils.addFlag(pmId, "ns", mongo);
flagged++;
}
if (i++ % 1000 == 0)
System.err.println("updated " + i + "th, on " + pmId
+ ", flagged:" + flagged);
}
System.err.println("END updated " + i + ", flagged:" + flagged);
}
public void importEducationOrganizations(String type) {
logger.log(Level.INFO, "Importing education organizations into graph: " + type);
DBObject query = new BasicDBObject();
query.put("type", type);
DBCursor cursor = mongo.getCollection(EDUCATION_ORGANIZATION).find(query);
cursor.batchSize(BATCH_SIZE);
while (cursor.hasNext()) {
DBObject edOrg = cursor.next();
String currentEdOrgId = (String) edOrg.get("_id");
Vertex v = graph.addVertex(null);
logger.log(Level.INFO, "Adding vertex for {0}#{1} \t {2}", new String[] { type,
currentEdOrgId, v.getId().toString() });
v.setProperty("mongoid", currentEdOrgId);
@SuppressWarnings("unchecked")
Map<String, Object> body = (Map<String, Object>) edOrg.get("body");
if (body.containsKey("parentEducationAgencyReference")) {
String parentId = (String) body.get("parentEducationAgencyReference");
for (Vertex child : graph.getVertices("mongoid", currentEdOrgId)) {
for (Vertex parent : graph.getVertices("mongoid", parentId)) {
graph.addEdge(null, parent, child, EDUCATION_ORGANIZATION_ASSOCIATION);
logger.log(Level.INFO, "Adding an edge between ed org: " + parentId + " --> " + currentEdOrgId);
}
}
}
}
}
/**
* 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()]);
}
/**
* This function writes the Bing Search ranks for a chunk of inchis in a TSV file, append only option.
* @param inchis (Set<String>) set of InChI string representations
* @param outputPath (String) path indicating the output file
* @param appendOutput (Boolean) whether to append the results to the output file
* @throws IOException
*/
private void writeBingSearchRanksAsTSVForInchiChunk(Set<String> inchis, String outputPath, Boolean appendOutput)
throws IOException {
// Define headers
List<String> bingRankerHeaderFields = new ArrayList<>();
addChemicalHeaders(bingRankerHeaderFields);
// Open TSV writer
try(TSVWriter<String, String> tsvWriter = new TSVWriter<>(bingRankerHeaderFields)) {
tsvWriter.open(new File(outputPath), appendOutput);
int counter = 0;
DBCursor cursor = mongoDB.fetchNamesAndUsageForInchis(inchis);
// Iterate through the target chemicals
while (cursor.hasNext()) {
counter++;
BasicDBObject o = (BasicDBObject) cursor.next();
Map<String, String> row = new HashMap<>();
updateRowWithChemicalInformation(o, row);
tsvWriter.append(row);
tsvWriter.flush();
}
LOGGER.info("Wrote %d Bing Search results to %s", counter, outputPath);
}
}
/**
* Imports student school associations (as edges) into the graph structure.
*/
@Override
public void importCollection() {
logger.log(Level.INFO, "Importing collection into graph: " + STUDENT_SCHOOL_ASSOCIATION);
DBCursor cursor = mongo.getCollection(STUDENT_SCHOOL_ASSOCIATION).find();
cursor.batchSize(BATCH_SIZE);
while (cursor.hasNext()) {
DBObject association = cursor.next();
@SuppressWarnings("unchecked")
Map<String, Object> body = (Map<String, Object>) association.get("body");
String studentId = (String) body.get("studentId");
String schoolId = (String) body.get("schoolId");
for (Vertex student : graph.getVertices("mongoid", studentId)) {
for (Vertex school : graph.getVertices("mongoid", schoolId)) {
Edge e = graph.addEdge(null, school, student, "studentSchoolAssociation");
e.setProperty("mongoid", association.get("_id"));
if (body.containsKey("exitWithdrawDate")) {
e.setProperty("exitWithdrawDate", body.get("exitWithdrawDate"));
}
logger.log(Level.INFO, "Adding an edge between school: " + schoolId + " --> student: " + studentId);
}
}
}
}
public List<Long> getRxnsWithSubstrate(String enzyme, Long org, List<Long> substrates) {
BasicDBObject query = new BasicDBObject();
query.put("organisms.id", org);
BasicDBObject enzymeQuery = new BasicDBObject();
enzymeQuery.put("ecnum", enzyme);
query.put("$ne", enzymeQuery);
for (Long substrate: substrates) {
BasicDBList queryList = new BasicDBList();
DBObject querySubstrate = new BasicDBObject();
querySubstrate.put("enz_summary.substrates.pubchem", substrate);
DBObject queryProduct = new BasicDBObject();
queryProduct.put("enz_summary.products.pubchem", substrate);
queryList.add(querySubstrate);
queryList.add(queryProduct);
query.put("$or", queryList);
}
DBCursor cur = this.dbReactions.find(query);
List<Long> reactions = new ArrayList<Long>();
while (cur.hasNext()) {
DBObject o = cur.next();
long id = (Integer) o.get("_id"); // checked: db type IS int
reactions.add(id);
}
cur.close();
return reactions;
}
@Override
public List<? extends BoundedSource<ObjectId>> split(
long desiredBundleSizeBytes, PipelineOptions options) throws Exception {
Mongo mongo = spec.connectionConfiguration().setupMongo();
try {
GridFS gridfs = spec.connectionConfiguration().setupGridFS(mongo);
DBCursor cursor = createCursor(gridfs);
long size = 0;
List<BoundedGridFSSource> list = new ArrayList<>();
List<ObjectId> objects = new ArrayList<>();
while (cursor.hasNext()) {
GridFSDBFile file = (GridFSDBFile) cursor.next();
long len = file.getLength();
if ((size + len) > desiredBundleSizeBytes && !objects.isEmpty()) {
list.add(new BoundedGridFSSource(spec, objects));
size = 0;
objects = new ArrayList<>();
}
objects.add((ObjectId) file.getId());
size += len;
}
if (!objects.isEmpty() || list.isEmpty()) {
list.add(new BoundedGridFSSource(spec, objects));
}
return list;
} finally {
mongo.close();
}
}
@Override
public Representation doRepresent() {
String view = (String) getRequest().getAttributes().get("view");
DBCollection col = mongo.getDB("scava").getCollection("projects");
BasicDBObject query = new BasicDBObject();
switch (view) {
case "error":
query.put("executionInformation.inErrorState", true);
break;
case "analysed":
query.put("executionInformation.analysed", true);
break;
default:
break;
}
DBCursor cursor = col.find(query);
ArrayNode res = mapper.createArrayNode();
while (cursor.hasNext()) {
DBObject project = cursor.next();
ObjectNode node = mapper.createObjectNode();
node.put("id", project.get("shortName").toString());
node.put("name", project.get("name").toString());
BasicDBObject ei = (BasicDBObject)project.get("executionInformation");
node.put("lastExecuted", (String)ei.get("lastExecuted"));
node.put("analysed", (Boolean)ei.get("analysed"));
node.put("inErrorState", (Boolean)ei.get("inErrorState"));
node.put("monitor", (Boolean)ei.get("monitor"));
res.add(node);
}
return Util.createJsonRepresentation(res);
}
public static void main(String[] args) {
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB database = mongoClient.getDB("myMongoDb");
// print existing databases
mongoClient.getDatabaseNames().forEach(System.out::println);
database.createCollection("customers", null);
// print all collections in customers database
database.getCollectionNames().forEach(System.out::println);
// create data
DBCollection collection = database.getCollection("customers");
BasicDBObject document = new BasicDBObject();
document.put("name", "Shubham");
document.put("company", "Baeldung");
collection.insert(document);
// update data
BasicDBObject query = new BasicDBObject();
query.put("name", "Shubham");
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("name", "John");
BasicDBObject updateObject = new BasicDBObject();
updateObject.put("$set", newDocument);
collection.update(query, updateObject);
// read data
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("name", "John");
DBCursor cursor = collection.find(searchQuery);
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
// delete data
BasicDBObject deleteQuery = new BasicDBObject();
deleteQuery.put("name", "John");
collection.remove(deleteQuery);
}
public Set<Sha256Hash> getSet(String key, int max_results)
{
Set<Sha256Hash> ret = new HashSet<Sha256Hash>();
DBCursor c = collection.find(new BasicDBObject(KEY, key));
int count =0;
while(c.hasNext())
{
ret.add(new Sha256Hash( MongoEntry.getValueString(c.next())));
count++;
if (count > max_results) throw new DBTooManyResultsException();
}
return ret;
}
@ResponseBody
@RequestMapping(value="/getMgmtCmdResult.do")
public Map<String, Object> getMgmtCmdResult(HttpServletRequest request) throws Exception {
Map<String, Object> response = new HashMap<String, Object>();
HttpSession session = request.getSession(false);
if(session != null){
//페이지 권한 확인
GroupAuthorization requestAuth = (GroupAuthorization) session.getAttribute("requestAuth");
if(!requestAuth.getAuthorizationDBRead().equals("1")){
response.put("result", 1);
response.put("errorCode", -1);
response.put("content", "authMessage: onem2m 페이지 접근 권한이 없습니다.");
} else {
String collection = HeritProperties.getProperty("Globals.MongoDB.Collection");
DBCollection col = db.getCollection(collection);
String rn = request.getParameter("rn");
if (rn == null || rn.equals("")) {
response.put("result", 1);
response.put("errorCode", -1);
response.put("content", "parameters[rn] is missing");
}
BasicDBObject query = new BasicDBObject();
query.put("ty", 8);
query.put("ext", rn);
DBCursor cursor = col.find(query);
List<DBObject> dbObjList = new ArrayList<DBObject>();
try {
while (cursor.hasNext()) {
dbObjList.add(cursor.next());
////System.out.println("############# dbObjList.toString() = " + dbObjList.toString());
}
} finally {
cursor.close();
}
response.put("result", 0);
response.put("errorCode", 0);
response.put("content", dbObjList.toString());
}
} else {
response.put("result", 1);
response.put("errorCode", -1);
response.put("content", "session is null");
}
return response;
}