Example usage for com.mongodb Bytes QUERYOPTION_AWAITDATA

List of usage examples for com.mongodb Bytes QUERYOPTION_AWAITDATA

Introduction

In this page you can find the example usage for com.mongodb Bytes QUERYOPTION_AWAITDATA.

Prototype

int QUERYOPTION_AWAITDATA

To view the source code for com.mongodb Bytes QUERYOPTION_AWAITDATA.

Click Source Link

Document

Use with TailableCursor.

Usage

From source file:HAL.libraries.blackboard_client.OplogMonitorThread.java

License:Open Source License

/**
 * Constructs a tailed cursor for the specified query on the oplog collection.
 * This constructor should be used when user authentication is required.
 * /*from ww w .  j ava2  s  . com*/
 * @param mongo The Mongo database connection that should be used.
 * @param oplogDBName The database in which the oplog collection resides.
 * @param oplogCollectionName The name of the oplog collection.
 * @param username Username that will be used to authenticate with the oplog database. This user should have read access.
 * @param password The password belonging to the specified user.
 * @param query The query that will be used in the tailed cursor.
 **/
public OplogMonitorThread(Mongo mongo, String oplogDBName, String oplogCollectionName, String username,
        String password, DBObject query) {
    database = mongo.getDB(oplogDBName);
    database.authenticate(username, password.toCharArray());
    DBCollection collection = database.getCollection(oplogCollectionName);

    tailedCursor = collection.find(query);
    tailedCursor.addOption(Bytes.QUERYOPTION_TAILABLE);
    tailedCursor.addOption(Bytes.QUERYOPTION_AWAITDATA);
    tailedCursor.skip(tailedCursor.size());
    callbackThread = new OplogCallbackThread();
}

From source file:org.apache.camel.component.mongodb.MongoDbTailingProcess.java

License:Apache License

private DBCursor initializeCursor() {
    Object lastVal = tailTracking.lastVal;
    // lastVal can be null if we are initializing and there is no persistence enabled
    DBCursor answer;//from  w  ww. ja  v  a 2  s .  co m
    if (lastVal == null) {
        answer = dbCol.find().addOption(Bytes.QUERYOPTION_TAILABLE).addOption(Bytes.QUERYOPTION_AWAITDATA);
    } else {
        DBObject queryObj = new BasicDBObject(tailTracking.getIncreasingFieldName(),
                new BasicDBObject("$gt", lastVal));
        answer = dbCol.find(queryObj).addOption(Bytes.QUERYOPTION_TAILABLE)
                .addOption(Bytes.QUERYOPTION_AWAITDATA);
    }
    return answer;
}

From source file:org.mongodb.demos.tailable.RealTimeAppServer.java

License:Apache License

public static void main(String[] args) throws Exception {

    RealTimeAppServer realTimeAppServer = new RealTimeAppServer();
    DBCollection coll = realTimeAppServer.createAndGetCappedCollection("messages");

    DBCursor cur = coll.find().sort(BasicDBObjectBuilder.start("$natural", 1).get())
            .addOption(Bytes.QUERYOPTION_TAILABLE).addOption(Bytes.QUERYOPTION_AWAITDATA);

    System.out.println("== open cursor ==");

    Runnable task = () -> {/*  w w  w  . j  a  v  a 2  s .c o m*/
        System.out.println("\tWaiting for events");
        while (cur.hasNext()) {
            DBObject obj = cur.next();
            System.out.println(obj);

        }
    };
    new Thread(task).start();

}

From source file:Rpackage.MongoFixPostCode.java

public static void main(String[] args) {

    YelpAPI yelpApi = new YelpAPI();
    JSONParser parser = new JSONParser();
    try {/*from   w  w  w  .  j  av a2  s .co  m*/

        // Connect to mongodb
        MongoClient mongo = new MongoClient("10.33.2.142", 27017);

        // get database
        // if database doesn't exists, mongodb will create it for you
        DB db = mongo.getDB("test");

        // get collection
        // if collection doesn't exists, mongodb will create it for you
        DBCollection collection = db.getCollection("twitter_Stream");

        DBCursor cursor;
        BasicDBObject query;
        //------------------------------------
        // ( 1 ) collection.find() --> get all document

        DBObject clause1 = new BasicDBObject("Latitude", new BasicDBObject("$exists", true));
        DBObject clause2 = new BasicDBObject("tweet_Country_Code", new BasicDBObject("$exists", true));
        DBObject clause3 = new BasicDBObject("R_Post_Code", new BasicDBObject("$exists", false));
        /*{
          "$and":[
            {
              "tweet_Country_Code":{
                "$exists":true
              }
            },
            {
              "Latitude":{
                "$exists":false
              }
            },
            {
              "tweet_Country_Code":"AU"
            }
          ]
        }                
        */
        BasicDBList or = new BasicDBList();
        or.add(clause1);
        or.add(clause2);
        or.add(clause3);
        query = new BasicDBObject("$and", or);

        //{  "$and":[    {      "Latitude":{        "$exists":true      }    },    {      "R_Post_Code":{        "$exists":false      }    }  ]}            
        cursor = collection.find(query).addOption(Bytes.QUERYOPTION_NOTIMEOUT) //
                .addOption(Bytes.QUERYOPTION_AWAITDATA);

        System.out.println("( 1 ) .get the user id within  latitide");
        System.out.println("results --> " + cursor.count());
        FoursquareAPI_backup qui4squreApi = new FoursquareAPI_backup();
        try {
            while (cursor.hasNext()) {
                DBObject data = cursor.next();
                String v_user_Name = (String) data.get("user_name");
                Long v_twitte_id = (Long) data.get("tweet_ID");
                String v_twitte_text = (String) data.get("tweet_text");
                Long v_user_Id = (Long) data.get("user_Id");
                Double v_Latitude = (Double) data.get("Latitude");
                Double v_Longtitude = (Double) data.get("Longitude");
                String v_tweet_Place_Name = (String) data.get("tweet_Place_Name");
                String v_tweet_Country_Code = (String) data.get("tweet_Country_Code");

                if (v_user_Id == null) {
                    /*                        System.out.print("update:" +v_user_Name+"/status/"+ v_twitte_id);
                     try{
                     Status status = twitter.showStatus(v_twitte_id);
                     BasicDBObject jo = GetMongoRecord(status);
                     System.out.println( "-->" + status.getId() + " : " + jo.getString("Re_user_screenName") + ":" + jo.getString("tweet_text"));
                     collection.update(new BasicDBObject("tweet_ID", v_twitte_id), jo); // set the document in the DB to the new document for Jo                     
                     }catch (TwitterException ex){
                     if (ex.getStatusCode()==144) continue;
                     }
                     */
                    continue;
                }
                JSONObject businesses = yelpApi.search4Yelp("city", v_Latitude, v_Longtitude);//-27.497835,153.017472);                      
                boolean searchAgain = false;

                if (businesses == null)
                    searchAgain = true;
                else if (businesses.size() < 1)
                    searchAgain = true;

                if (searchAgain) {
                    System.out.println("La:" + v_Latitude + "\tLo:" + v_Longtitude);
                    businesses = qui4squreApi.search4Square("city", v_Latitude, v_Longtitude);
                    searchAgain = false;
                }
                if (businesses == null)
                    searchAgain = true;
                else if (businesses.size() < 1)
                    searchAgain = true;

                if (searchAgain) {
                    businesses = qui4squreApi.searchGoogleMap("city", v_Latitude, v_Longtitude);
                    if (businesses == null) {
                        System.out.println("\t" + v_tweet_Country_Code + "\t:" + v_tweet_Place_Name);
                        continue;
                    } else if (businesses.size() < 1) {
                        System.out.println("\t" + v_tweet_Country_Code + "\t:" + v_tweet_Place_Name);
                        continue;
                    }
                }

                String country_code = (String) businesses.get("country_code");
                String city_code = (String) businesses.get("city_name");
                String state_code = (String) businesses.get("state_code");
                String post_code = (String) businesses.get("post_code");

                BasicDBObject setNewFieldQuery = new BasicDBObject().append("$set",
                        new BasicDBObject().append("R_Country_Code", country_code)
                                .append("R_State_Code", state_code).append("R_City", city_code)
                                .append("R_Post_Code", post_code));

                collection.update(new BasicDBObject().append("tweet_ID", v_twitte_id), setNewFieldQuery); // set the document in the DB to the new document for Jo                                             

                setNewFieldQuery.clear();
                setNewFieldQuery = null;
            }
        } finally {
            cursor.close();
        }

        System.out.println("---------------------------------");

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }

}

From source file:thingynet.event.EventListener.java

License:Apache License

private void refreshCursor() {
    if (cursor != null) {
        cursor.close();//from  w  ww . j  av a 2  s .c om
        if (cursorPollMillis > 0) {
            try {
                Thread.sleep(cursorPollMillis);
            } catch (InterruptedException e) {
                log.error("Thread was interrupted", e);
            }
        }
    }
    if (handler.getLast() == null) {
        cursor = collection.getDBCollection().find().addOption(Bytes.QUERYOPTION_TAILABLE)
                .addOption(Bytes.QUERYOPTION_AWAITDATA);
    } else {
        DBObject queryObj = new BasicDBObject("_id", new BasicDBObject("$gt", handler.getLast()));
        cursor = collection.getDBCollection().find(queryObj).addOption(Bytes.QUERYOPTION_TAILABLE)
                .addOption(Bytes.QUERYOPTION_AWAITDATA);
    }
}