Example usage for com.mongodb Mongo getDB

List of usage examples for com.mongodb Mongo getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

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

License:Open Source License

/**
 * Constructor of OplogMonitorThread./*from ww w  .  j  av a 2s  .co  m*/
 * @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 query The query that will be used in the tailed cursor.
 **/
public OplogMonitorThread(Mongo mongo, String oplogDBName, String oplogCollectionName, DBObject query) {
    database = mongo.getDB(oplogDBName);
    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: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  w  ww .j  a  va2 s  .  c o m*/
 * @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:home.vitaly.simulator.mongoCursorReader.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Mongo conn = null;

    mongoCursorReader manager = new mongoCursorReader();
    try {/*  w ww . j  a v  a 2  s.  c om*/
        // Get the MongoDB connection.  Mongo is the connection object.
        conn = manager.getConnection();

        // Get the database from the Connection (Schema in RDBMS World).
        DB db = conn.getDB("DBTR");

        // Get the collection (Table in RDBMS World).  Don't worry, even if it is not there,
        // it can be a lazy fetch and will be created when the first object is inserted
        DBCollection collection = manager.getCollection("tr", db);

        // Check whether there is any document (RDBMS world -> Row), present
        //            if (collection.count() == 0) {
        //            if(collection.count() < 10) {
        //                // Create the document in the TEST collection (RDBMS worlqd -> Table)
        //                DBObject object = new BasicDBObject();
        //                object.put("name", "? ");
        //                object.put("message", " ? !");
        //                collection.insert(object);
        //            }
        manager.printCollection(collection);
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}

From source file:interfaces.Rsuarios.java

public Rsuarios() {
    try {//from   w  ww  .ja  va 2 s  . c o m

        //realizamos la conexion de la base de datos MOngoDB
        Mongo mongo = new Mongo("localhost", 27017);
        db = mongo.getDB("basedadatos");
        tabla = db.getCollection("usuario");
    } catch (UnknownHostException ex) {
        Logger.getLogger(Rsuarios.class.getName()).log(Level.SEVERE, null, ex);
    } catch (MongoException ex) {
        Logger.getLogger(Rsuarios.class.getName()).log(Level.SEVERE, null, ex);
    }

    //codigo pra centrar, quitar margen y transarencia del formulario
    this.setUndecorated(true);
    initComponents();
    //this.setLocationRelativeTo(null);
    //this.setOpacity(0.9f);

}

From source file:it.sayservice.platform.smartplanner.utils.LegGenerator.java

License:Apache License

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

    Mongo m = new Mongo("localhost"); // default port 27017
    DB db = m.getDB("smart-planner-15x");
    DBCollection coll = db.getCollection("stops");

    // read trips.txt(trips,serviceId).
    List<String[]> trips = readFileGetLines("src/main/resources/schedules/17/trips.txt");
    List<String[]> stopTimes = readFileGetLines("src/main/resources/schedules/17/stop_times.txt");
    for (String[] words : trips) {
        try {/*  w  w  w . ja va 2  s  .  c  o m*/
            String routeId = words[0].trim();
            String serviceId = words[1].trim();
            String tripId = words[2].trim();
            // fetch schedule for trips.
            for (int i = 0; i < stopTimes.size(); i++) {
                // already ordered by occurence.
                String[] scheduleLeg = stopTimes.get(i);
                if (scheduleLeg[0].equalsIgnoreCase(tripId)) {
                    // check if next leg belongs to same trip
                    if (stopTimes.get(i + 1)[0].equalsIgnoreCase(tripId)) {

                        String arrivalT = scheduleLeg[1];
                        String departT = scheduleLeg[2];
                        String sourceId = scheduleLeg[3];
                        String destId = stopTimes.get(i + 1)[3];
                        // get coordinates of stops.
                        /**
                         * make sure that mongo stop collection is
                         * populated. if, not, invoke
                         * http://localhost:7070/smart
                         * -planner/rest/getTransitTimes
                         * /TB_R2_R/1366776000000/1366819200000
                         */
                        Stop source = (Stop) getObjectByField(db, "id", sourceId, coll, Stop.class);
                        Stop destination = (Stop) getObjectByField(db, "id", destId, coll, Stop.class);
                        // System.out.println(tripId + ","
                        // + routeId + ","
                        // + source.getId() + ","
                        // + source.getLatitude() + ","
                        // + source.getLongitude() + ","
                        // + arrivalT + ","
                        // + destination.getId() + ","
                        // + destination.getLatitude() + ","
                        // + destination.getLongitude() + ","
                        // + departT + ","
                        // + serviceId
                        // );
                        String content = tripId + "," + routeId + "," + source.getStopId() + ","
                                + source.getLatitude() + "," + source.getLongitude() + "," + arrivalT + ","
                                + destination.getStopId() + "," + destination.getLatitude() + ","
                                + destination.getLongitude() + "," + departT + "," + "Giornaliero" + "\n";

                        File file = new File("src/main/resources/legs/legs.txt");
                        // single leg file
                        if (!file.exists()) {
                            file.createNewFile();
                        }

                        FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
                        BufferedWriter bw = new BufferedWriter(fw);
                        bw.write(content);
                        bw.close();
                        // individual trip leg file.
                        File fileT = new File("src/main/resources/legs/legs_" + routeId + ".txt");
                        FileWriter fwT = new FileWriter(fileT.getAbsoluteFile(), true);
                        BufferedWriter bwT = new BufferedWriter(fwT);
                        bwT.write(content);
                        bwT.close();

                    }
                }
            }

        } catch (Exception e) {
            System.out.println("Error parsing trip: " + words[0] + "," + words[1] + "," + words[2]);
        }
    }
    System.out.println("Done");
}

From source file:logica.MovieSessionBean.java

@PostConstruct
private void initDB() {
    Mongo m;
    try {/*from w  w w.ja va2s . c  om*/
        m = new Mongo();
        DB db = m.getDB("movieDB");
        movieColl = db.getCollection("movies");
        if (movieColl == null) {
            movieColl = db.createCollection("movies", null);
        }
    } catch (UnknownHostException e) {
        Logger.getLogger(MovieSessionBean.class.getName()).log(Level.SEVERE, null, e);
    }
}

From source file:MI_PAQUETE.Ventana.java

public Ventana() {

    this.setTitle("Guadalupe");
    initComponents();//  w  ww  . j a v a 2 s.c o m
    this.getContentPane().setBackground(Color.WHITE);
    this.setBounds(10, 10, 345, 51);
    setResizable(false);

    fields[0] = jLabel1;
    fields[1] = jLabel2;
    fields[2] = jLabel3;
    fields[3] = jLabel4;
    fields[4] = jLabel5;
    fields[5] = jLabel6;
    fields[6] = jLabel7;
    fields[7] = jLabel8;
    fields[8] = jLabel9;
    fields[9] = jLabel10;
    fields[10] = jLabel11;
    fields[11] = jLabel12;

    texts[0] = jTextField1;
    texts[1] = jTextField2;
    texts[2] = jTextField3;
    texts[3] = jTextField4;
    texts[4] = jTextField5;
    texts[5] = jTextField6;
    texts[6] = jTextField7;
    texts[7] = jTextField8;
    texts[8] = jTextField9;
    texts[9] = jTextField10;
    texts[10] = jTextField11;
    texts[11] = jTextField12;

    add_to_ActionListenner();
    make_lab_and_text_invisible();

    try {
        Mongo mongo = new Mongo("localhost", 27017);

        db = mongo.getDB("Guadalupe");

    } catch (UnknownHostException ex) {
        Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:mongodb.Movies_import.java

public static void main(String[] args)
        throws UnknownHostException, MongoException, FileNotFoundException, IOException {

    Mongo mongo = new Mongo(); //creating an instance of mongodb called mongo

    //using mongo object to get the database name
    List<String> databases = mongo.getDatabaseNames();
    for (String string : databases) {
        System.out.println(string);
    }// w  w  w  .  j a  va  2  s  .c  o m

    //assigning db variable to mongoDB directory 'db' created in the terminal
    DB db = mongo.getDB("db");

    DBCollection Collection = db.getCollection("movies_import");

    FileReader filereader = null;
    BufferedReader bufferreader = null;

    try {
        filereader = new FileReader("/Users/cheryldsouza/Documents/UPITT/Data Analytics/ml-10M100K/movies.dat");
        bufferreader = new BufferedReader(filereader);
        System.out.println("Mongodb Files");

        String read = bufferreader.readLine();

        while (read != null) {
            System.out.println(read);

            String[] reads = read.split("::");

            BasicDBObject object = new BasicDBObject();

            object.append("MovieID", reads[0]);

            object.append("Title", reads[1]);

            object.append("Genres", reads[2]);

            Collection.insert(object);
            read = bufferreader.readLine();
        }

    } catch (FileNotFoundException e) {
        System.out.println("Documents not found");
        System.exit(-1);
    }

    catch (IOException e) {
        System.out.println("FAILED");
        System.exit(-1);
    }
}

From source file:mongodb.question2.java

public static void main(String[] args) throws UnknownHostException, MongoException {

    Mongo mongoclient = new Mongo();

    DB db = mongoclient.getDB("db");

    DBCollection Collection = db.getCollection("tags_import");

    //Need to enter the userid
    System.out.println("Enter the User Id here");
    Scanner userid = new Scanner(System.in);
    String UserId = userid.next();

    BasicDBObject user = new BasicDBObject();
    user.put("UserID", UserId);

    DBCursor usercursor = Collection.find();
    usercursor = Collection.find(user);

    while (usercursor.hasNext()) {
        DBObject object = usercursor.next();
        String MovieID = (String) object.get("MovieID");

        System.out.println("MovieID is " + MovieID);

        //for each Movie Id find the target id
        BasicDBObject targetid = new BasicDBObject();
        targetid.put("MovieID", MovieID);

        DBCursor moviecursor = Collection.find();
        moviecursor = Collection.find(targetid);

        int count = moviecursor.count();
        System.out.println("This movie has been rated " + count + " users");
        System.out.println(count);

        while (moviecursor.hasNext()) {
            DBObject object2 = moviecursor.next();
            String folluserID = (String) object2.get("UserID");
            System.out.println("The following  Users are familiar to the given user  " + folluserID);
        }/*from  w  w  w  .  java 2  s. co  m*/
        System.out.println();
    }

}

From source file:mongodb.tagsimport.java

public static void main(String[] args) throws UnknownHostException, MongoException {

    Mongo mongo = new Mongo();

    List<String> databases = mongo.getDatabaseNames();
    for (String str : databases) {
        System.out.println(str);/*w w  w.  java 2  s  . c  o m*/
    }

    DB db = mongo.getDB("db");

    DBCollection dbCollection = db.getCollection("tags_import");

    FileReader filereader = null;
    BufferedReader bufferreader = null;

    try {
        filereader = new FileReader("/Users/cheryldsouza/Documents/UPITT/Data Analytics/ml-10M100K/tags.dat");
        bufferreader = new BufferedReader(filereader);
        System.out.println("Mongodb Tags File");

        String read = bufferreader.readLine();

        while (read != null) {
            System.out.println(read);

            String[] reads = read.split("::");

            //inserting keys

            BasicDBObject object = new BasicDBObject();

            //UserID::MovieID::Tag::Timestamp

            object.append("UserID", reads[0]);

            object.append("MovieID", reads[1]);

            object.append("Tag", reads[2]);

            object.append("Timestamp", reads[3]);

            dbCollection.insert(object);
            read = bufferreader.readLine();
        }

    } catch (FileNotFoundException e) {
        System.out.println("No documents found. Please try again");
        System.exit(-1);
    }

    catch (IOException e) {
        System.out.println("FAILED");
        System.exit(-1);
    }
}