Example usage for com.mongodb.client MongoCursor hasNext

List of usage examples for com.mongodb.client MongoCursor hasNext

Introduction

In this page you can find the example usage for com.mongodb.client MongoCursor hasNext.

Prototype

@Override
    boolean hasNext();

Source Link

Usage

From source file:mongotweet.MongoTweet.java

public static void showTimeline(String uname) {
    MongoCollection coll = db.getCollection("timeline");
    //        MongoCursor<Document> cursor = coll.find().iterator();
    BsonDocument where = new BsonDocument().append("username", new BsonString(uname));
    MongoCursor<Document> cursor = coll.find(where).iterator();

    String username, body, tweet_id;
    while (cursor.hasNext()) {
        tweet_id = (String) cursor.next().get("tweet_id");
        BsonDocument where2 = new BsonDocument().append("tweet_id", new BsonString(tweet_id));
        MongoCollection col2 = db.getCollection("tweets");
        MongoCursor<Document> cursor2 = col2.find(where2).iterator();
        while (cursor2.hasNext()) {
            Document tmp = cursor2.next();
            username = (String) tmp.get("username");
            body = (String) tmp.get("body");
            System.out.format("%s : %s \n", username, body);
        }//from  w  ww  . ja  v a  2s . c o  m
    }
}

From source file:mypackage.DBInformation.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        try {//w w w  .  ja va  2  s.c om
            String dbName = request.getParameter("dbname");
            MongoClient mongoClient = new MongoClient("localhost", 27017);
            HttpSession httpSession = request.getSession(false);
            MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName);
            MongoIterable<String> mongoIterable = mongoDatabase.listCollectionNames();
            MongoCursor<String> mongoCursor = mongoIterable.iterator();
            JSONObject jSONObject = new JSONObject();
            JSONObject jSONObject1 = new JSONObject();
            JSONArray jSONArray = new JSONArray();
            int i = 0;
            while (mongoCursor.hasNext()) {
                jSONArray.put(mongoCursor.next());
                i++;
            }
            jSONObject.put("db", jSONArray);
            jSONObject.put("counter", i);
            out.println(jSONObject);

        } catch (JSONException e) {

        }

    }
}

From source file:net.modelbased.proasense.storage.reader.EventReaderMongoSync.java

License:Apache License

public List<Document> call() {
    // Connect to MongoDB database
    MongoClient mongoClient = new MongoClient(new MongoClientURI(this.mongoURL));
    MongoDatabase database = mongoClient.getDatabase(this.database);

    MongoCollection<Document> collection = database.getCollection(this.collectionId);

    // Create document list for query result
    List<Document> foundDocuments = new ArrayList<Document>();

    if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);/*from w w  w.  j  a  v  a2s  . com*/
        }
    }

    if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.AVERAGE)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultAverageLong = 0;
        double resultAverageDouble = 0;
        long collectionSize = 0;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            collectionSize++;
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                Long value = (Long) eventProps.get(this.propertyKey);
                resultAverageLong = resultAverageLong + value;
            } else if (doubleProperty) {
                Double value = (Double) eventProps.get(this.propertyKey);
                resultAverageDouble = resultAverageDouble + value;
                doubleProperty = true;
            }
        }

        if (longProperty) {
            Long resultAverage = resultAverageLong / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            Double resultAverage = resultAverageDouble / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.MAXIMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMaximumLong = Long.MIN_VALUE;
        double resultMaximumDouble = Double.MIN_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value > resultMaximumLong)
                    resultMaximumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value > resultMaximumDouble)
                    resultMaximumDouble = value;
            }
        }

        if (longProperty) {
            Long resultMaximum = resultMaximumLong;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            Double resultMaximum = resultMaximumDouble;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.MINUMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMinimumLong = Long.MAX_VALUE;
        double resultMinimumDouble = Double.MAX_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value < resultMinimumLong)
                    resultMinimumLong = value;
            } else if (doubleProperty) {
                Double value = (Double) eventProps.get(this.propertyKey);
                if (value < resultMinimumDouble)
                    resultMinimumDouble = value;
            }
        }

        if (longProperty) {
            long resultMinimum = resultMinimumLong;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMinimum = resultMinimumDouble;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);
        }
    }

    if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.AVERAGE)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultAverageLong = 0;
        double resultAverageDouble = 0;
        long collectionSize = 0;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            collectionSize++;
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                Long value = (Long) eventProps.get(this.propertyKey);
                resultAverageLong = resultAverageLong + value;
            } else if (doubleProperty) {
                Double value = (Double) eventProps.get(this.propertyKey);
                resultAverageDouble = resultAverageDouble + value;
            }
        }

        if (longProperty) {
            Long resultAverage = resultAverageLong / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            Double resultAverage = resultAverageDouble / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.MAXIMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMaximumLong = Long.MIN_VALUE;
        double resultMaximumDouble = Double.MIN_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value > resultMaximumLong)
                    resultMaximumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value > resultMaximumDouble)
                    resultMaximumDouble = value;
                doubleProperty = true;
            }
        }

        if (longProperty) {
            long resultMaximum = resultMaximumLong;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMaximum = resultMaximumDouble;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.MINUMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMinimumLong = Long.MAX_VALUE;
        double resultMinimumDouble = Double.MAX_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value < resultMinimumLong)
                    resultMinimumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value < resultMinimumDouble)
                    resultMinimumDouble = value;
            }
        }

        if (longProperty) {
            long resultMinimum = resultMinimumLong;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMinimum = resultMinimumDouble;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);
        }
    }

    if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.AVERAGE)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultAverageLong = 0;
        double resultAverageDouble = 0;
        long collectionSize = 0;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            collectionSize++;
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                resultAverageLong = resultAverageLong + value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                resultAverageDouble = resultAverageDouble + value;
            }
        }

        if (longProperty) {
            long resultAverage = resultAverageLong / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultAverage = resultAverageDouble / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.MAXIMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMaximumLong = Long.MIN_VALUE;
        double resultMaximumDouble = Double.MIN_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value > resultMaximumLong)
                    resultMaximumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value > resultMaximumDouble)
                    resultMaximumDouble = value;
            }
        }

        if (longProperty) {
            long resultMaximum = resultMaximumLong;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMaximum = resultMaximumDouble;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.MINUMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMinimumLong = Long.MAX_VALUE;
        double resultMinimumDouble = Double.MAX_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value < resultMinimumLong)
                    resultMinimumLong = value;
            } else if (doubleProperty) {
                double value = (Long) eventProps.get(this.propertyKey);
                if (value < resultMinimumDouble)
                    resultMinimumDouble = value;
            }
        }

        if (longProperty) {
            long resultMinimum = resultMinimumLong;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMinimum = resultMinimumDouble;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.ANOMALY) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);
        }
    }

    if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);
        }
    }

    if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.AVERAGE)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultAverageLong = 0;
        double resultAverageDouble = 0;
        long collectionSize = 0;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            collectionSize++;
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                resultAverageLong = resultAverageLong + value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                resultAverageDouble = resultAverageDouble + value;
            }
        }

        if (longProperty) {
            long resultAverage = resultAverageLong / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultAverage = resultAverageDouble / collectionSize;
            Document resultDoc = new Document("RESULT", resultAverage);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.MAXIMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMaximumLong = Long.MIN_VALUE;
        double resultMaximumDouble = Double.MIN_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value > resultMaximumLong)
                    resultMaximumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value > resultMaximumDouble)
                    resultMaximumDouble = value;
            }
        }

        if (longProperty) {
            long resultMaximum = resultMaximumLong;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMaximum = resultMaximumDouble;
            Document resultDoc = new Document("RESULT", resultMaximum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.MINUMUM)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        boolean longProperty = false;
        boolean doubleProperty = false;
        MongoCursor<Document> firstCursor = it.iterator();
        if (firstCursor.hasNext()) {
            Document doc = firstCursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            Object valueObj = eventProps.get(this.propertyKey);
            if (valueObj instanceof Long)
                longProperty = true;
            else if (valueObj instanceof Double)
                doubleProperty = true;
        }

        long resultMinimumLong = Long.MAX_VALUE;
        double resultMinimumDouble = Double.MAX_VALUE;
        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            Document eventProps = (Document) doc.get("eventProperties");
            if (longProperty) {
                long value = (Long) eventProps.get(this.propertyKey);
                if (value < resultMinimumLong)
                    resultMinimumLong = value;
            } else if (doubleProperty) {
                double value = (Double) eventProps.get(this.propertyKey);
                if (value < resultMinimumDouble)
                    resultMinimumDouble = value;
            }
        }

        if (longProperty) {
            long resultMinimum = resultMinimumLong;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        } else if (doubleProperty) {
            double resultMinimum = resultMinimumDouble;
            Document resultDoc = new Document("RESULT", resultMinimum);
            foundDocuments.add(resultDoc);
        }
    }

    if (queryType.equals(EventQueryType.FEEDBACK) && queryOperation.equals(EventQueryOperation.DEFAULT)) {
        FindIterable<Document> it = collection
                .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime)));

        MongoCursor<Document> cursor = it.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            foundDocuments.add(doc);
        }
    }

    mongoClient.close();

    return foundDocuments;
}

From source file:net.ymate.platform.persistence.mongodb.support.ResultSetHelper.java

License:Apache License

public static <T extends IEntity> List<T> toEntities(Class<T> entity, MongoIterable<Document> iterable)
        throws Exception {
    MongoCursor<Document> _documentIt = iterable.iterator();
    List<T> _resultSet = new ArrayList<T>();
    while (_documentIt.hasNext()) {
        _resultSet.add(toEntity(entity, _documentIt.next()));
    }/*  www.j  ava2 s  .c o m*/
    return _resultSet;
}

From source file:nl.mvdb.mongodb.RemoveStudentsLowestHomeworkScore.java

License:Apache License

public static void main(String[] args) {
    MongoClient client = new MongoClient();
    try {/*from   w  w w .j a  v  a2 s. c o m*/
        MongoDatabase database = client.getDatabase("school");
        MongoCollection<Document> collection = database.getCollection("students");

        MongoCursor<Document> iterator = collection.find().sort(ascending("scores.score")).iterator();

        while (iterator.hasNext()) {
            Document studentDoc = iterator.next();
            System.out.println(studentDoc);

            Document lowestScoreDoc = null;

            List<Document> scoreDocs = (ArrayList) studentDoc.get("scores");
            for (Document scoreDoc : scoreDocs) {
                if ("homework".equals(scoreDoc.getString("type"))) {
                    Double score = scoreDoc.getDouble("score");
                    if (lowestScoreDoc == null || score < lowestScoreDoc.getDouble("score")) {
                        lowestScoreDoc = scoreDoc;
                    }
                }
            }

            if (lowestScoreDoc != null) {
                scoreDocs.remove(lowestScoreDoc);
                studentDoc.put("scores", scoreDocs);
                collection.replaceOne(new Document("_id", studentDoc.getInteger("_id")), studentDoc);
            }

        }

        System.out.println(collection.count());
    } finally {
        client.close();
    }
}

From source file:nl.mvdb.mongodb.RemoveStudentsLowestScore.java

License:Apache License

public static void main(String[] args) {
    MongoClient client = new MongoClient();
    try {/*from  ww  w. j a va 2 s.com*/
        MongoDatabase database = client.getDatabase("students");
        MongoCollection<Document> collection = database.getCollection("grades");

        MongoCursor<Document> iterator = collection.find().sort(ascending("student_id", "score")).iterator();

        Integer lastStudentId = null;
        while (iterator.hasNext()) {
            Document gradingDoc = iterator.next();
            Integer studentId = gradingDoc.getInteger("student_id");
            if (!studentId.equals(lastStudentId)) {
                lastStudentId = studentId;
                System.out.println(lastStudentId);
                collection.deleteOne(gradingDoc);
            }
        }

        System.out.println(collection.count());
    } finally {
        client.close();
    }
}

From source file:org.apache.drill.exec.store.mongo.MongoGroupScan.java

License:Apache License

@SuppressWarnings({ "rawtypes" })
private void init() throws IOException {

    List<String> h = storagePluginConfig.getHosts();
    List<ServerAddress> addresses = Lists.newArrayList();
    for (String host : h) {
        addresses.add(new ServerAddress(host));
    }/*from w w w  . jav  a2 s  .  co m*/
    MongoClient client = storagePlugin.getClient();
    chunksMapping = Maps.newHashMap();
    chunksInverseMapping = Maps.newLinkedHashMap();
    if (isShardedCluster(client)) {
        MongoDatabase db = client.getDatabase(CONFIG);
        MongoCollection<Document> chunksCollection = db.getCollection(CHUNKS);
        Document filter = new Document();
        filter.put(NS, this.scanSpec.getDbName() + "." + this.scanSpec.getCollectionName());

        Document projection = new Document();
        projection.put(SHARD, select);
        projection.put(MIN, select);
        projection.put(MAX, select);

        FindIterable<Document> chunkCursor = chunksCollection.find(filter).projection(projection);
        MongoCursor<Document> iterator = chunkCursor.iterator();

        MongoCollection<Document> shardsCollection = db.getCollection(SHARDS);

        projection = new Document();
        projection.put(HOST, select);

        boolean hasChunks = false;
        while (iterator.hasNext()) {
            Document chunkObj = iterator.next();
            String shardName = (String) chunkObj.get(SHARD);
            String chunkId = (String) chunkObj.get(ID);
            filter = new Document(ID, shardName);
            FindIterable<Document> hostCursor = shardsCollection.find(filter).projection(projection);
            MongoCursor<Document> hostIterator = hostCursor.iterator();
            while (hostIterator.hasNext()) {
                Document hostObj = hostIterator.next();
                String hostEntry = (String) hostObj.get(HOST);
                String[] tagAndHost = StringUtils.split(hostEntry, '/');
                String[] hosts = tagAndHost.length > 1 ? StringUtils.split(tagAndHost[1], ',')
                        : StringUtils.split(tagAndHost[0], ',');
                List<String> chunkHosts = Arrays.asList(hosts);
                Set<ServerAddress> addressList = getPreferredHosts(storagePlugin.getClient(addresses),
                        chunkHosts);
                if (addressList == null) {
                    addressList = Sets.newHashSet();
                    for (String host : chunkHosts) {
                        addressList.add(new ServerAddress(host));
                    }
                }
                chunksMapping.put(chunkId, addressList);
                ServerAddress address = addressList.iterator().next();
                List<ChunkInfo> chunkList = chunksInverseMapping.get(address.getHost());
                if (chunkList == null) {
                    chunkList = Lists.newArrayList();
                    chunksInverseMapping.put(address.getHost(), chunkList);
                }
                List<String> chunkHostsList = new ArrayList<String>();
                for (ServerAddress serverAddr : addressList) {
                    chunkHostsList.add(serverAddr.toString());
                }
                ChunkInfo chunkInfo = new ChunkInfo(chunkHostsList, chunkId);
                Document minMap = (Document) chunkObj.get(MIN);

                Map<String, Object> minFilters = Maps.newHashMap();
                Set keySet = minMap.keySet();
                for (Object keyObj : keySet) {
                    Object object = minMap.get(keyObj);
                    if (!(object instanceof MinKey)) {
                        minFilters.put(keyObj.toString(), object);
                    }
                }
                chunkInfo.setMinFilters(minFilters);

                Map<String, Object> maxFilters = Maps.newHashMap();
                Map maxMap = (Document) chunkObj.get(MAX);
                keySet = maxMap.keySet();
                for (Object keyObj : keySet) {
                    Object object = maxMap.get(keyObj);
                    if (!(object instanceof MaxKey)) {
                        maxFilters.put(keyObj.toString(), object);
                    }
                }

                chunkInfo.setMaxFilters(maxFilters);
                chunkList.add(chunkInfo);
            }
            hasChunks = true;
        }
        // In a sharded environment, if a collection doesn't have any chunks, it is considered as an
        // unsharded collection and it will be stored in the primary shard of that database.
        if (!hasChunks) {
            handleUnshardedCollection(getPrimaryShardInfo(client));
        }
    } else {
        handleUnshardedCollection(storagePluginConfig.getHosts());
    }

}

From source file:org.apache.nifi.mongodb.MongoDBLookupService.java

License:Apache License

private Document findOne(Document query, Document projection) {
    MongoCollection col = controllerService.getDatabase(databaseName).getCollection(collection);
    MongoCursor<Document> it = (projection != null ? col.find(query).projection(projection) : col.find(query))
            .iterator();//from  w w w.  j a v  a 2  s . c  o m
    Document retVal = it.hasNext() ? it.next() : null;
    it.close();

    return retVal;
}

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;//from ww w.j  av a 2  s . c  om
    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;/*ww  w.j  a v a 2s. co  m*/

    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);
        }
    }
}