Example usage for com.mongodb MongoClient setWriteConcern

List of usage examples for com.mongodb MongoClient setWriteConcern

Introduction

In this page you can find the example usage for com.mongodb MongoClient setWriteConcern.

Prototype

@Deprecated
public void setWriteConcern(final WriteConcern writeConcern) 

Source Link

Document

Sets the default write concern to use for write operations executed on any DBCollection created indirectly from this instance, via a DB instance created from #getDB(String) .

Usage

From source file:com.edgytech.umongo.MongoPanel.java

License:Apache License

public void readWriteOptions(ButtonBase button) {
    MongoClient mongo = getMongoNode().getMongoClient();
    OptionDialog od = UMongo.instance.getGlobalStore().getOptionDialog();
    od.update(mongo.getOptions(), mongo.getWriteConcern(), mongo.getReadPreference());
    if (!od.show()) {
        return;/*w  ww .  j  ava2 s.com*/
    }
    mongo.setOptions(od.getQueryOptions());
    mongo.setWriteConcern(od.getWriteConcern());
    mongo.setReadPreference(od.getReadPreference());
    refresh();
}

From source file:com.ifeng.vdn.report.rest.data.mongodb.ApplicationConfiguration.java

License:Apache License

@Override
public Mongo mongo() throws Exception {
    MongoClient mongo = new MongoClient();
    mongo.setWriteConcern(WriteConcern.SAFE);
    return mongo;
}

From source file:com.strategicgains.docussandra.controller.perf.remote.mongo.MongoLoader.java

License:Apache License

public static void loadMongoData(MongoClientURI uri, final int NUM_WORKERS, Database database,
        final int numDocs, final PerfTestParent clazz) {
    logger.info("------------Loading Data into: " + database.name() + " with MONGO!------------");
    try {//from ww  w  .jav a2 s . com
        try {
            MongoClient mongoClient = new MongoClient(uri);
            mongoClient.setWriteConcern(WriteConcern.MAJORITY);
            DB db = mongoClient.getDB(database.name());
            final DBCollection coll = db.getCollection(database.name());
            ArrayList<Thread> workers = new ArrayList<>(NUM_WORKERS + 1);
            int docsPerWorker = numDocs / NUM_WORKERS;
            try {
                List<Document> docs = clazz.getDocumentsFromFS();
                ArrayList<List<Document>> documentQueues = new ArrayList<>(NUM_WORKERS + 1);
                int numDocsAssigned = 0;
                while ((numDocsAssigned + 1) < numDocs) {
                    int start = numDocsAssigned;
                    int end = numDocsAssigned + docsPerWorker;
                    if (end > numDocs) {
                        end = numDocs - 1;
                    }
                    documentQueues.add(new ArrayList(docs.subList(start, end)));
                    numDocsAssigned = end;
                }
                for (final List<Document> queue : documentQueues) {
                    workers.add(new Thread() {
                        @Override
                        public void run() {
                            for (Document d : queue) {
                                DBObject o = (DBObject) JSON.parse(d.object());
                                coll.save(o);
                            }
                            logger.info("Thread " + Thread.currentThread().getName() + " is done. It processed "
                                    + queue.size() + " documents.");
                        }
                    });
                }
            } catch (UnsupportedOperationException e)//we can't read everything in at once
            {
                //all we need to do in this block is find a way to set "workers"
                for (int i = 0; i < NUM_WORKERS; i++) {
                    workers.add(new Thread() {
                        private final int chunk = (int) (Math.random() * 100) + 150;//pick a random chunk so we are not going back to the FS all at the same time and potentially causing a bottle neck

                        @Override
                        public void run() {
                            ThreadLocal<Integer> counter = new ThreadLocal<>();
                            counter.set(new Integer(0));
                            try {
                                List<Document> docs = clazz.getDocumentsFromFS(chunk);//grab a handful of documents
                                while (docs.size() > 0) {
                                    for (Document d : docs)//process the documents we grabbed
                                    {
                                        DBObject o = (DBObject) JSON.parse(d.object());
                                        coll.save(o);
                                        counter.set(counter.get() + 1);
                                    }
                                    docs = clazz.getDocumentsFromFS(chunk);//grab another handful of documents
                                }
                                logger.info("Thread " + Thread.currentThread().getName()
                                        + " is done. It processed " + counter.get() + " documents.");
                            } catch (IOException | ParseException e) {
                                logger.error("Couldn't read from document", e);
                            }
                        }
                    });
                }
            }

            long start = new Date().getTime();
            //start your threads!
            for (Thread t : workers) {
                t.start();
            }
            logger.info("All threads started, waiting for completion.");
            boolean allDone = false;
            boolean first = true;
            while (!allDone || first) {
                first = false;
                boolean done = true;
                for (Thread t : workers) {
                    if (t.isAlive()) {
                        done = false;
                        logger.info("Thread " + t.getName() + " is still running.");
                        break;
                    }
                }
                if (done) {
                    allDone = true;
                } else {
                    logger.info("We still have workers running...");
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                    }
                }
            }
            long end = new Date().getTime();
            long miliseconds = end - start;
            double seconds = (double) miliseconds / 1000d;
            output.info("Done loading data using: " + NUM_WORKERS + ". Took: " + seconds + " seconds");
            double tpms = (double) numDocs / (double) miliseconds;
            double tps = tpms * 1000;
            double transactionTime = (double) miliseconds / (double) numDocs;
            output.info(database.name() + " Mongo Average Transactions Per Second: " + tps);
            output.info(
                    database.name() + " Mongo Average Transactions Time (in miliseconds): " + transactionTime);

        } catch (UnknownHostException e) {
            logger.error("Couldn't connect to Mongo Server", e);
        }
    } catch (IOException | ParseException e) {
        logger.error("Couldn't read data.", e);
    }
}

From source file:com.terkaly.JavaMongoDB.App.java

License:Open Source License

public static void main(String[] args) {
    try {//from   w w  w  . ja v  a  2s . co m
        // Create a connection using mongoClient
        // 23.99.88.154 is obtained from the portal
        MongoClient mongoClient = new MongoClient("[ put your ip address here ]", 27017);

        // Get a connection to mydb
        DB db = mongoClient.getDB("mydb");

        // mydb has one or more collections, get "testCollection"
        DBCollection collection = db.getCollection("testCollection");

        // Create an empty object
        BasicDBObject empty = new BasicDBObject();

        // Clear out testCollection
        collection.remove(empty);

        // Acknowledges the write operation only
        // after committing the data to the journal
        mongoClient.setWriteConcern(WriteConcern.JOURNALED);

        // Here is the data format in JSON
        // {
        //   "name": "MongoDB",
        //   "type": "database",
        //   "count": 1,
        //   "info": {
        //         "x": 203,
        //         "y": 102
        //    }
        // }

        BasicDBObject doc = new BasicDBObject("name", "MongoDB").append("type", "database").append("count", 1)
                .append("info", new BasicDBObject("x", 203).append("y", 102));
        collection.insert(doc);

        DBObject myDoc = collection.findOne();
        System.out.println(myDoc);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:laboratorio_2_sd.Laboratorio_2_SD.java

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

    //archivo de configuracion
    File archivo = new File("config.ini");
    FileReader fr = new FileReader(archivo);
    BufferedReader br = new BufferedReader(fr);
    String linea = br.readLine();
    int cantParticiones = Integer.parseInt(linea);
    linea = br.readLine();//w w  w  . ja  v a2  s  . c  o m
    String[] data = linea.split("\n");
    String rutaDocumentos = data[0];
    linea = br.readLine();
    data = linea.split("\n");
    String rutaStopwords = data[0];
    if (imprime)
        System.out.println("Se configura con:\n- Particiones: " + cantParticiones + "\n- Ruta Documentos: '"
                + rutaDocumentos + "'\n- Ruta StopWords: '" + rutaStopwords + "'\n");

    //Archivo stopwords
    File archivo3 = new File(rutaStopwords);
    FileReader fr3 = new FileReader(archivo3);
    BufferedReader br3 = new BufferedReader(fr3);
    ArrayList<String> stopwords = new ArrayList<>();
    if (imprime) {
        System.out.println("StopWords: \n");
        int contador = 0;
        while ((linea = br3.readLine()) != null && linea.length() != 0) {//mientras no sea EOF
            stopwords.add(linea);
            if (contador < 9) {
                System.out.print(linea + " ");
                contador++;
            } else if (contador == 9) {
                contador = 0;
                System.out.println(linea);
            }
        }
        System.out.println("");
    }
    //Crea db y tablas
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    mongoClient.dropDatabase("indexDB");
    DB db = mongoClient.getDB("indexDB");
    mongoClient.setWriteConcern(WriteConcern.JOURNALED);
    db.setWriteConcern(WriteConcern.JOURNALED);

    DBCollection colDocumentos = db.getCollection("Documentos");
    DBCollection colIndiceInvertido = db.getCollection("IndiceInvertido");
    DBCollection colVocabulario = db.getCollection("Vocabulario");

    colDocumentos.createIndex(new BasicDBObject("idDoc", 1));
    colIndiceInvertido.createIndex(new BasicDBObject("idPalabra", 1));
    colVocabulario.createIndex(new BasicDBObject("palabra", 1));

    //Archivo de documentos
    File archivo2 = new File(rutaDocumentos);
    FileReader fr2 = new FileReader(archivo2);
    BufferedReader br2 = new BufferedReader(fr2);

    int idDoc = 0;
    int idPalabraActual = 0;

    while ((linea = br2.readLine()) != null && !linea.contains("</mediawiki>")) {//mientras no sea EOF

        while (!linea.contains("<page>")) {
            linea = br2.readLine();
        }
        //guarda el titulo
        linea = br2.readLine();
        int indice = linea.indexOf(">");
        String sub = linea.substring(indice + 1);
        indice = sub.indexOf("<");
        String titulo = sub.substring(0, indice);

        //guarda el username
        while (!linea.contains("<username>")) {
            linea = br2.readLine();
        }
        indice = linea.indexOf(">");
        sub = linea.substring(indice + 1);
        indice = sub.indexOf("<");
        String username = sub.substring(0, indice);

        while (linea.contains("<text") == false) {
            linea = br2.readLine();
        }

        //Aqui comienza a leer el contenido del documento
        ArrayList<String> palabrasTemp = new ArrayList<String>();

        while (linea.contains("</text>") == false) {

            linea = br2.readLine();

            if (!linea.contains("</text>")) {
                StringTokenizer st = new StringTokenizer(linea, " #%_-*.,;:|/\\(){}[]=&+'\"?!");
                while (st.hasMoreTokens()) {
                    String palabra = st.nextToken();
                    palabra = palabra.toLowerCase();
                    if (palabra.length() > 1 && !stopwords.contains(palabra)) {
                        palabrasTemp.add(palabra);
                    }
                }
            }

        }
        Documento docTemp = new Documento(idDoc, palabrasTemp, titulo, username);
        if (imprime)
            docTemp.print();
        //Se agrega el documento directamente a la coleccion documentos
        colDocumentos.insert(docTemp.toDBObject());

        for (int i = 0; i < docTemp.cantPalabras; i++) {

            String palabra = docTemp.palabras.get(i);
            if (imprime)
                System.out.println("***********************");
            if (imprime)
                System.out.println("Palabra: " + palabra);
            //revisa si la palabra esta en la coleccion vocabulario
            DBCursor cursor = colVocabulario.find((DBObject) new BasicDBObject("palabra", palabra));
            if (cursor.hasNext()) { //si esta
                if (imprime)
                    System.out.println("Esta en vocabulario");
                Vocabulario vocTemp = new Vocabulario((BasicDBObject) cursor.next());
                if (imprime)
                    System.out.println("idPalabra: " + vocTemp.idPalabra);
                DBCursor cursor2 = colIndiceInvertido
                        .find((DBObject) new BasicDBObject("idPalabra", vocTemp.idPalabra));
                BasicDBObject find = (BasicDBObject) cursor2.next();
                IndiceInvertido indiceTemp = new IndiceInvertido(find);
                //revisa si ya est ingresado el documento actual en el indiceInvertido
                int contador = 0;
                int frec = 0;
                for (int j = 0; j < indiceTemp.frecuencias.size(); j++) {
                    if (indiceTemp.frecuencias.get(j).idDocumento == idDoc) {
                        contador = 1;
                        frec = indiceTemp.frecuencias.get(j).frecuencia;
                        break;
                    }
                }
                if (contador == 1) { //si encontro el id del documento actual
                    if (imprime)
                        System.out.println("Esta en indice invertido");
                    //actualizar frecuencia en indice Invertido
                    indiceTemp.ActualizarFrecuencias(frec + 1, idDoc);
                    colIndiceInvertido.update(find, indiceTemp.toDBObject(), false, true);
                    if (imprime)
                        indiceTemp.print();
                } else {//si no est
                    if (imprime)
                        System.out.println("No est en indice invertido");
                    //actualizar la cantidad de documentos del vocabulario
                    vocTemp.cantDocumentos++;
                    colVocabulario.insert(vocTemp.toDBObject());
                    if (imprime)
                        vocTemp.print();
                    //agregar nueva tupla de frecuencia/idDoc a indice
                    indiceTemp.ActualizarFrecuencias(1, idDoc);
                    if (imprime)
                        indiceTemp.print();
                    colIndiceInvertido.insert(indiceTemp.toDBObject());

                }
            } else {//no esta
                if (imprime)
                    System.out.println("No esta en vocabulario\nInserta nuevo elemento");
                if (idDoc == 0 && i == 0) { //no se ha insertado ningun dato
                    //inserta palabra en vocabulario
                    Vocabulario vocTemp = new Vocabulario(palabra, 0, 1);
                    colVocabulario.insert(vocTemp.toDBObject());
                    if (imprime)
                        vocTemp.print();
                    //inserta entrada en indice invertido
                    IndiceInvertido indiceTemp = new IndiceInvertido(vocTemp.idPalabra, 1, idDoc);
                    colIndiceInvertido.insert(indiceTemp.toDBObject());
                    if (imprime)
                        indiceTemp.print();
                    idPalabraActual++;
                } else {
                    //se obtiene un nuevo id
                    //se inserta a vocabulario
                    Vocabulario vocTemp = new Vocabulario(palabra, idPalabraActual, 1);
                    colVocabulario.insert(vocTemp.toDBObject());
                    if (imprime)
                        vocTemp.print();
                    //se inserta a indice invertido
                    IndiceInvertido indiceTemp = new IndiceInvertido(vocTemp.idPalabra, 1, idDoc);
                    if (imprime)
                        indiceTemp.print();
                    colIndiceInvertido.insert(indiceTemp.toDBObject());
                    idPalabraActual++;
                }
            }
        }

        idDoc++;
        while (!linea.contains("</page>")) {
            linea = br2.readLine();
        }
        pasarGarbageCollector();
    }

}

From source file:mongodb.JavaDocAdd.java

public static void main(String[] args) {
    try {//from ww w.  ja  v  a 2  s  . c om
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        mongoClient.setWriteConcern(WriteConcern.MAJORITY);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        JavaDocAdd.showNewDocs(collection, "Before Additions");
        JavaDocAdd.addSelfie(collection);
        JavaDocAdd.showNewDocs(collection, "After adding single");
        JavaDocAdd.addGoogleAndTweet(collection);
        JavaDocAdd.showNewDocs(collection, "After adding mutliple");
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:mongodb.JavaDocDelete.java

public static void main(String[] args) {
    try {//  w  w w .  j  a v a 2 s  .  co m
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        mongoClient.setWriteConcern(WriteConcern.MAJORITY);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        JavaDocDelete.showNewDocs(collection, "Before delete");
        JavaDocDelete.removeNewDocs(collection);
        JavaDocDelete.showNewDocs(collection, "After delete");
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:mongodb.JavaDocSave.java

public static void main(String[] args) {
    try {//from  w w w  . ja  v  a 2  s  .  c  o m
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        mongoClient.setWriteConcern(WriteConcern.JOURNAL_SAFE);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        JavaDocSave.showWord(collection, "Before save");
        JavaDocSave.saveBlueDoc(collection);
        JavaDocSave.showWord(collection, "After save");
        JavaDocSave.resetDoc(collection);
        JavaDocSave.showWord(collection, "After reset");
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:mongodb.JavaDocUpdate.java

public static void main(String[] args) {
    try {//w  w w. j a va  2  s  .c om
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        mongoClient.setWriteConcern(WriteConcern.JOURNAL_SAFE);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        JavaDocUpdate.showWord(collection, "Before update");
        JavaDocUpdate.updateDoc(collection);
        JavaDocUpdate.showWord(collection, "After update");
        JavaDocUpdate.resetDoc(collection);
        JavaDocUpdate.showWord(collection, "After reset");
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:mongodb.JavaDocUpsert.java

public static void main(String[] args) {
    try {/*from www  .ja  va  2s  .  co m*/
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        mongoClient.setWriteConcern(WriteConcern.JOURNAL_SAFE);
        DB db = mongoClient.getDB("words");
        DBCollection collection = db.getCollection("word_stats");
        JavaDocUpsert.showWord(collection, "Before upsert");
        JavaDocUpsert.addUpsert(collection);
        JavaDocUpsert.updateUpsert(collection);
    } catch (Exception e) {
        System.out.println(e);
    }
}