List of usage examples for com.mongodb.client FindIterable sort
FindIterable<TResult> sort(@Nullable Bson sort);
From source file:flipkart.mongo.replicator.node.ReplicationTask.java
License:Apache License
@Override public void run() { String shardId = rsConfig.shardName; Node masterNode = rsConfig.getMasterNode().get(); MongoClient client = MongoConnector.getMongoClient(Lists.newArrayList(masterNode)); MongoDatabase database = client.getDatabase("local"); lastCp = taskContext.checkPointHandler.getCheckPoint(shardId); logger.info(String.format("######## START REPLICATOR FOR MongoURI: %s. LastCheckpoint: %s #######", client.getAddress(), lastCp)); MongoCollection<Document> collection = database.getCollection("oplog.rs"); FindIterable<Document> iterable; MongoCursor<Document> cursor; do {//from www. ja v a 2s .c o m if (lastCp == null) { iterable = collection.find(); } else { iterable = collection.find(new Document("ts", new Document("$gt", lastCp))); } cursor = iterable.sort(new Document("$natural", 1)).noCursorTimeout(true) .cursorType(CursorType.TailableAwait).batchSize(3000).iterator(); try { executeCursor(cursor); Thread.sleep(WAIT_FOR_NEXT_ITERATION); } catch (MongoCursorNotFoundException e) { logger.info("Cursor has been closed. About to open a new cursor. ID: " + cursor.getServerCursor().getId()); } catch (Exception e) { logger.error("Exception while replicating", e); throw new RuntimeException(e); } finally { cursor.close(); } } while (true); }
From source file:io.dirigible.mongodb.jdbc.MongodbStatement.java
License:Apache License
/** * Input string: the document specification as defined in https://docs.mongodb.org/manual/reference/command/find/#dbcmd.find *///from ww w.ja v a2s . co m @Override public ResultSet executeQuery(String sql) throws SQLException { MongoDatabase db = this.conn.getMongoDb(); BsonDocument filterDocument = null; if (sql == null || sql.length() < 1)//that is a call to find() in terms of mongodb queries filterDocument = new BsonDocument(); else filterDocument = BsonDocument.parse(sql); /* * TODO: With 3.2 use https://docs.mongodb.org/manual/reference/command/find/#dbcmd.find instead * Document response = this.conn.getMongoDb().runCommand(filterDocument); //MongoDB 3.2 only. Won't work on 3.0 */ String collectionName = filterDocument.containsKey("find") ? filterDocument.getString("find").getValue() : null; if (collectionName == null) collectionName = this.conn.getCollectionName();//fallback if any if (collectionName == null) throw new IllegalArgumentException("Specifying a collection is mandatory for query operations"); BsonDocument filter = filterDocument.containsKey("filter") ? filterDocument.getDocument("filter") : null; FindIterable<Document> searchHits = null; if (filter == null) searchHits = db.getCollection(collectionName).find(); else searchHits = db.getCollection(collectionName).find(filter); if (filterDocument.containsKey("batchSize")) searchHits.batchSize(filterDocument.getInt32("batchSize").getValue()); if (filterDocument.containsKey("limit")) searchHits.limit(filterDocument.getInt32("limit").getValue()); if (filterDocument.containsKey("sort")) searchHits.sort(filterDocument.getDocument("sort")); return new MongodbResultSet(this, searchHits); }
From source file:io.lumeer.storage.mongodb.MongoDbStorage.java
License:Open Source License
@Override public List<DataDocument> search(String collectionName, DataFilter filter, final DataSort sort, List<String> attributes, final int skip, int limit) { MongoCollection<Document> collection = database.getCollection(collectionName); FindIterable<Document> documents = filter != null ? collection.find(filter.<Bson>get()) : collection.find(); if (sort != null) { documents = documents.sort(sort.<Bson>get()); }//from ww w.ja va 2s .c o m if (attributes != null && !attributes.isEmpty()) { documents.projection(Projections.fields(Projections.include(attributes))); } if (skip > 0) { documents = documents.skip(skip); } if (limit > 0) { documents = documents.limit(limit); } return MongoUtils.convertIterableToList(documents); }
From source file:io.sip3.tapir.twig.mongo.Mongo.java
License:Apache License
public <T> Iterator<T> find(String prefix, Query query, Class<T> clazz) { return new Iterator<T>() { private final Iterator<Long> intervals = TimeIntervalIterator.of(query.millis(), partition); private MongoCursor<T> cursor; @Override// w ww .ja v a2 s.c o m public boolean hasNext() { if (cursor != null && cursor.hasNext()) { return true; } while (intervals.hasNext()) { long millis = intervals.next(); String collection = collection(prefix, partition.define(millis, false)); FindIterable<T> fi = db.getCollection(collection, clazz).find(query.filter()) .batchSize(batchSize); if (query.sort() != null) { fi.sort(query.sort()); } cursor = fi.iterator(); if (cursor.hasNext()) { return true; } } return false; } @Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } return cursor.next(); } }; }
From source file:net.ymate.platform.persistence.mongodb.impl.MongoSession.java
License:Apache License
public <T extends IEntity> IResultSet<T> find(Class<T> entity, OrderBy orderBy, Page page) throws Exception { MongoCollection<Document> _collection = __doGetCollection(entity); FindIterable<Document> _findIterable = _collection.find(); if (orderBy != null) { _findIterable.sort(orderBy.toBson()); }// w w w . j a v a2 s . co m long _recordCount = 0; if (__doPageInit(_findIterable, page)) { _recordCount = _collection.count(); } return new DefaultResultSet<T>(ResultSetHelper.toEntities(entity, _findIterable), page.page(), page.pageSize(), _recordCount); }
From source file:org.apache.nifi.processors.mongodb.GetMongo.java
License:Apache License
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { final ComponentLog logger = getLogger(); final Document query = context.getProperty(QUERY).isSet() ? Document.parse(context.getProperty(QUERY).evaluateAttributeExpressions().getValue()) : null;/* ww w . jav a2s .c o m*/ final Document projection = context.getProperty(PROJECTION).isSet() ? Document.parse(context.getProperty(PROJECTION).evaluateAttributeExpressions().getValue()) : null; final Document sort = context.getProperty(SORT).isSet() ? Document.parse(context.getProperty(SORT).evaluateAttributeExpressions().getValue()) : null; final String jsonTypeSetting = context.getProperty(JSON_TYPE).getValue(); configureMapper(jsonTypeSetting); final MongoCollection<Document> collection = getCollection(context); try { final FindIterable<Document> it = query != null ? collection.find(query) : collection.find(); if (projection != null) { it.projection(projection); } if (sort != null) { it.sort(sort); } if (context.getProperty(LIMIT).isSet()) { it.limit(context.getProperty(LIMIT).evaluateAttributeExpressions().asInteger()); } if (context.getProperty(BATCH_SIZE).isSet()) { it.batchSize(context.getProperty(BATCH_SIZE).evaluateAttributeExpressions().asInteger()); } final MongoCursor<Document> cursor = it.iterator(); ComponentLog log = getLogger(); try { FlowFile flowFile = null; if (context.getProperty(RESULTS_PER_FLOWFILE).isSet()) { int ceiling = context.getProperty(RESULTS_PER_FLOWFILE).evaluateAttributeExpressions() .asInteger(); List<Document> batch = new ArrayList<>(); while (cursor.hasNext()) { batch.add(cursor.next()); if (batch.size() == ceiling) { try { if (log.isDebugEnabled()) { log.debug("Writing batch..."); } String payload = buildBatch(batch, jsonTypeSetting); writeBatch(payload, context, session); batch = new ArrayList<>(); } catch (IOException ex) { getLogger().error("Error building batch", ex); } } } if (batch.size() > 0) { try { writeBatch(buildBatch(batch, jsonTypeSetting), context, session); } catch (IOException ex) { getLogger().error("Error sending remainder of batch", ex); } } } else { while (cursor.hasNext()) { flowFile = session.create(); flowFile = session.write(flowFile, new OutputStreamCallback() { @Override public void process(OutputStream out) throws IOException { String json; if (jsonTypeSetting.equals(JSON_TYPE_STANDARD)) { json = mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(cursor.next()); } else { json = cursor.next().toJson(); } IOUtils.write(json, out); } }); flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/json"); session.getProvenanceReporter().receive(flowFile, getURI(context)); session.transfer(flowFile, REL_SUCCESS); } } session.commit(); } finally { cursor.close(); } } catch (final RuntimeException e) { context.yield(); session.rollback(); logger.error("Failed to execute query {} due to {}", new Object[] { query, e }, e); } }
From source file:org.apache.nifi.processors.mongodb.GetMongoRecord.java
License:Apache License
@Override public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { FlowFile input = null;/*from ww w . j a v a2 s . c om*/ if (context.hasIncomingConnection()) { input = session.get(); if (input == null && context.hasNonLoopConnection()) { return; } } final String database = context.getProperty(DATABASE_NAME).evaluateAttributeExpressions(input).getValue(); final String collection = context.getProperty(COLLECTION_NAME).evaluateAttributeExpressions(input) .getValue(); final String schemaName = context.getProperty(SCHEMA_NAME).evaluateAttributeExpressions(input).getValue(); final Document query = getQuery(context, session, input); MongoCollection mongoCollection = clientService.getDatabase(database).getCollection(collection); FindIterable<Document> find = mongoCollection.find(query); if (context.getProperty(SORT).isSet()) { find = find .sort(Document.parse(context.getProperty(SORT).evaluateAttributeExpressions(input).getValue())); } if (context.getProperty(PROJECTION).isSet()) { find = find.projection( Document.parse(context.getProperty(PROJECTION).evaluateAttributeExpressions(input).getValue())); } if (context.getProperty(LIMIT).isSet()) { find = find.limit(context.getProperty(LIMIT).evaluateAttributeExpressions(input).asInteger()); } MongoCursor<Document> cursor = find.iterator(); FlowFile output = input != null ? session.create(input) : session.create(); final FlowFile inputPtr = input; try { final Map<String, String> attributes = getAttributes(context, input, query, mongoCollection); try (OutputStream out = session.write(output)) { Map<String, String> attrs = inputPtr != null ? inputPtr.getAttributes() : new HashMap<String, String>() { { put("schema.name", schemaName); } }; RecordSchema schema = writerFactory.getSchema(attrs, null); RecordSetWriter writer = writerFactory.createWriter(getLogger(), schema, out, attrs); long count = 0L; writer.beginRecordSet(); while (cursor.hasNext()) { Document next = cursor.next(); if (next.get("_id") instanceof ObjectId) { next.put("_id", next.get("_id").toString()); } Record record = new MapRecord(schema, next); writer.write(record); count++; } writer.finishRecordSet(); writer.close(); out.close(); attributes.put("record.count", String.valueOf(count)); } catch (SchemaNotFoundException e) { throw new RuntimeException(e); } output = session.putAllAttributes(output, attributes); session.getProvenanceReporter().fetch(output, getURI(context)); session.transfer(output, REL_SUCCESS); if (input != null) { session.transfer(input, REL_ORIGINAL); } } catch (Exception ex) { ex.printStackTrace(); getLogger().error("Error writing record set from Mongo query.", ex); session.remove(output); if (input != null) { session.transfer(input, REL_FAILURE); } } }
From source file:org.axonframework.mongo.eventsourcing.eventstore.AbstractMongoEventStorageStrategy.java
License:Apache License
@Override public List<? extends TrackedEventData<?>> findTrackedEvents(MongoCollection<Document> eventCollection, TrackingToken lastToken, int batchSize) { FindIterable<Document> cursor; if (lastToken == null) { cursor = eventCollection.find(); } else {/*from ww w. j av a 2 s . c om*/ Assert.isTrue(lastToken instanceof MongoTrackingToken, () -> String.format("Token %s is of the wrong type", lastToken)); MongoTrackingToken trackingToken = (MongoTrackingToken) lastToken; cursor = eventCollection.find(and( gte(eventConfiguration.timestampProperty(), trackingToken.getTimestamp().minus(lookBackTime).toString()), nin(eventConfiguration.eventIdentifierProperty(), trackingToken.getKnownEventIds()))); } cursor = cursor.sort(new BasicDBObject(eventConfiguration().timestampProperty(), ORDER_ASC) .append(eventConfiguration().sequenceNumberProperty(), ORDER_ASC)); cursor = cursor.limit(batchSize); AtomicReference<MongoTrackingToken> previousToken = new AtomicReference<>((MongoTrackingToken) lastToken); List<TrackedEventData<?>> results = new ArrayList<>(); for (Document document : cursor) { extractEvents(document) .filter(ed -> previousToken.get() == null || !previousToken.get().getKnownEventIds().contains(ed.getEventIdentifier())) .map(event -> new TrackedMongoEventEntry<>(event, previousToken.updateAndGet(token -> token == null ? MongoTrackingToken.of(event.getTimestamp(), event.getEventIdentifier()) : token.advanceTo(event.getTimestamp(), event.getEventIdentifier(), lookBackTime)))) .forEach(results::add); } return results; }
From source file:org.bananaforscale.cormac.dao.document.DocumentDataServiceImpl.java
License:Apache License
/** * Returns all the documents in a collection. * * @param databaseName the database/* w w w . j a va2 s . co m*/ * @param collectionName the collection * @param query a JSON query param in the style of mongo * @param fields fields to return * @param skip the amount of documents to skip * @param limit the amount of documents to limit the result to * @param orderBy order ascending or descending by property * @param includeId determines whether to include the Mongo "_id" field * @return the documents in a collection * @throws DatasourceException * @throws NotFoundException */ @Override public List<String> getAll(String databaseName, String collectionName, String query, String fields, String skip, String limit, String orderBy, boolean includeId) throws DatasourceException, NotFoundException { try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } if (!collectionExists(databaseName, collectionName)) { throw new NotFoundException("The collection doesn't exist in the datasource"); } Integer intSkip, intLimit; try { intSkip = Integer.parseInt(skip); } catch (NumberFormatException ex) { intSkip = 0; } try { intLimit = Integer.parseInt(limit); } catch (NumberFormatException ex) { intLimit = 0; } // 1 or -1 to specify an ascending or descending sort respectively. Document orderByObject = null; if (orderBy != null && !orderBy.isEmpty()) { if (orderBy.contains("ascending")) { String[] parts = orderBy.split(":"); orderByObject = new Document(parts[0], 1); } else if (orderBy.contains("descending")) { String[] parts = orderBy.split(":"); orderByObject = new Document(parts[0], -1); } } MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName); MongoCollection collection = mongoDatabase.getCollection(collectionName); FindIterable iterable = (query == null || query.isEmpty()) ? collection.find() : collection.find(Document.parse(query)); // TODO: Figure out how to do this in new API // if (fields != null && !fields.isEmpty()) { // // expect the form to be field:value,field:value // Document document = new Document(); // String[] parts = fields.split(","); // for (String part : parts) { // String[] tempParts = part.split(":"); // document.append(tempParts[0], tempParts[1]); // } // iterable.projection(document); // } iterable.skip(intSkip); iterable.limit(intLimit); if (orderByObject != null) { iterable.sort(orderByObject); } Iterator<Document> curIter = iterable.iterator(); List<String> documentList = new ArrayList<>(); while (curIter.hasNext()) { Document current = curIter.next(); if (!includeId) { current.remove("_id"); } documentList.add(JSON.serialize(current)); } return documentList; } catch (MongoException ex) { logger.error("An error occured while retrieving the document list", ex); throw new DatasourceException("An error occured while retrieving the document list"); } }
From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java
License:Apache License
private MongoResult find(MongoQueryOptions mongoQueryOptions, final MongoResult mongoResult, MongoCollection<Document> collection) { Bson filter = mongoQueryOptions.getFilter(); Bson projection = mongoQueryOptions.getProjection(); Bson sort = mongoQueryOptions.getSort(); FindIterable<Document> findIterable; if (projection == null) { findIterable = collection.find(filter); } else {//w w w .j av a 2 s . c o m findIterable = collection.find(filter).projection(projection); } if (sort != null) { findIterable = findIterable.sort(sort); } findIterable.limit(mongoQueryOptions.getResultLimit()); findIterable.forEach(new Block<Document>() { @Override public void apply(Document document) { mongoResult.add(document); } }); return mongoResult; }