Example usage for com.mongodb Mongo slaveOk

List of usage examples for com.mongodb Mongo slaveOk

Introduction

In this page you can find the example usage for com.mongodb Mongo slaveOk.

Prototype

@SuppressWarnings("deprecation")
@Deprecated
public void slaveOk() 

Source Link

Document

Makes it possible to run read queries on secondary nodes

Usage

From source file:de.fhg.igd.mongomvcc.impl.MongoDBVDatabase.java

License:Open Source License

@Override
public void connectToReplicaSet(String name, Map<String, Integer> hostsWithPort, boolean slaveOk)
        throws VException {
    Mongo mongo = null;
    try {//from w w  w.j  a v a2  s.  c  om
        List<ServerAddress> addrs = new ArrayList<ServerAddress>();
        for (Map.Entry<String, Integer> e : hostsWithPort.entrySet()) {
            addrs.add(new ServerAddress(e.getKey(), e.getValue()));
        }
        mongo = new Mongo(addrs);

        if (slaveOk && mongo != null) {
            mongo.slaveOk();
            //            mongo.setReadPreference(ReadPreference.SECONDARY); needed with version 2.2
        }
    } catch (UnknownHostException e) {
        throw new VException("Unknown host", e);
    }
    connectInternal(name, mongo);
}

From source file:org.apache.hadoop.contrib.mongoreduce.MongoRecordReader.java

License:Apache License

private void connect(String location, Configuration conf) throws IOException {

    String[] parts = location.split(":");

    // default port for sharded server
    int port = 27018;
    if (parts.length > 1)
        port = Integer.parseInt(parts[1]);

    Mongo mongo = new Mongo(parts[0], port);

    // figure out if we can read from this server

    // allow reading from secondaries
    mongo.slaveOk();

    String database = conf.get("mongo.input.database");
    String collection = conf.get("mongo.input.collection");
    String query = conf.get("mongo.input.query", "");
    String select = conf.get("mongo.input.select", "");

    if (!query.equals("")) {
        DBObject q = (DBObject) JSON.parse(query);

        if (!select.equals("")) {
            DBObject s = (DBObject) JSON.parse(select);
            cursor = mongo.getDB(database).getCollection(collection).find(q, s);
        } else {//from  w  w  w  .ja  v a 2s.co m
            cursor = mongo.getDB(database).getCollection(collection).find(q);
        }
    } else {
        if (!select.equals("")) {
            DBObject s = (DBObject) JSON.parse(select);
            cursor = mongo.getDB(database).getCollection(collection).find(new BasicDBObject(), s);
        } else {
            cursor = mongo.getDB(database).getCollection(collection).find();
        }
    }

    cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);

    // thanks mongo, for this handy method
    totalResults = cursor.count();
    resultsRead = 0.0f;

}