下面列出了com.mongodb.DBCursor#batchSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void importCollection() {
DBCursor cursor = mongo.getCollection("studentSectionAssociation").find();
cursor.batchSize(BATCH_SIZE);
while (cursor.hasNext()) {
DBObject spa = cursor.next();
@SuppressWarnings("unchecked")
Map<String, Object> body = (Map<String, Object>) spa.get("body");
String studentId = (String) body.get("studentId");
String sectionId = (String) body.get("sectionId");
for (Vertex student : graph.getVertices("mongoid", studentId)) {
for (Vertex section : graph.getVertices("mongoid", sectionId)) {
Edge e = graph.addEdge(null, section, student, "studentSection");
e.setProperty("mongoid", spa.get("_id"));
if (body.containsKey("endDate")) {
e.setProperty("endDate", body.get("endDate"));
}
logger.log(Level.INFO, "Adding an edge between section: " + sectionId + " --> student: " + studentId);
}
}
}
}
public static void getSamplesFromCursor(DBCursor cursor, int limit, List<Sample> outList){
try{
// exhaust the cursor adding each sample
cursor.batchSize(limit);
cursor.limit(limit);
while(cursor.hasNext()) {
outList.add(new Sample(cursor.next()));
}
} finally {
// ensure cursor is closed
cursor.close();
}
}
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);
}
}
}
}
}
/**
* 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);
}
}
}
}