Example usage for com.mongodb Mongo Mongo

List of usage examples for com.mongodb Mongo Mongo

Introduction

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

Prototype

Mongo(final MongoClientURI mongoURI, @Nullable final MongoDriverInformation mongoDriverInformation) 

Source Link

Usage

From source file:pe.edu.ulima.util.MongoUtil.java

public static Mongo getMongo() {
    if (mongo == mongo) {
        try {/*  w ww  .  j  a v a 2s  .  co m*/
            mongo = new Mongo(host, port);
        } catch (UnknownHostException | MongoException e) {

        }
    }
    return mongo;
}

From source file:piecework.repository.concrete.EmbeddedMongoInstance.java

License:Educational Community License

public void importData() throws Exception {

    Mongo mongo = new Mongo(bindIp, port);
    DB db = mongo.getDB(dbName);//from  www . jav a 2s .  co  m

    File directory = new File(storePath, "dbs");

    if (!directory.exists()) {
        LOG.debug("No startup data exists");
        return;
    }

    File dbDirectory = new File(directory, dbName);

    if (!dbDirectory.exists()) {
        LOG.debug("No startup data exists for " + dbName);
        return;
    }

    File[] collectionFiles = dbDirectory.listFiles();

    for (File collectionFile : collectionFiles) {

        if (collectionFile.isFile() && collectionFile.exists()) {

            String collectionName = collectionFile.getName();

            LOG.debug("Loading collection '" + collectionName + "'...");

            DBCollection dbCollection = db.getCollection(collectionName);

            try {
                LOG.debug("Loading JSON from file '" + collectionFile.getName() + "'");
                importCollection(dbCollection, new FileInputStream(collectionFile));
            } catch (FileNotFoundException fnfe) {
                LOG.warn("Unable to initialize local mongo with data from " + collectionFile.getAbsolutePath(),
                        fnfe);
            }
        }
    }

    mongo.close();
}

From source file:pl.nask.hsn2.os.MongoConnector.java

License:Open Source License

private MongoConnector(String host, int port) throws IOException {
    Mongo mongo = null;/*from w  ww.  j a v  a  2  s  .  com*/
    try {
        mongo = new Mongo(host, port);
        mongo.dropDatabase("object-store");
        db = mongo.getDB("object-store");
        collection = db.createCollection("allJobsInOne", null);
        collection.ensureIndex(new BasicDBObject("parent", 1));
    } catch (UnknownHostException e) {
        LOGGER.error("MongoDB host is unreachable: {}", e.getMessage());
        throw new IOException("MongoDB is unreachable", e);
    } catch (MongoException.Network e) {
        LOGGER.error("Couldn't connect to MongoDB: {}", e.getMessage());
        throw new IOException("MongoDB is unreachable", e);
    }
}

From source file:pl.solr.ag.mongodb.sink.ComplexDataSink.java

License:Apache License

@Override
public void init(PlayerConfig config) throws InitializationFailedException {
    super.init(config);

    host = config.get(BASE_HOST_KEY);/* ww  w  .  j a v  a 2s  . c om*/
    port = Integer.valueOf(config.get(BASE_PORT_KEY));
    dbName = config.get(DB_NAME_KEY);
    collectionName = config.get(COLLECTION_NAME_KEY);

    if (host == null || "".equals(host.trim())) {
        throw new IllegalArgumentException(
                this.getClass().getName() + " expects configuration property " + BASE_HOST_KEY);
    }

    if (port == null) {
        throw new IllegalArgumentException(
                this.getClass().getName() + " expects configuration property " + BASE_PORT_KEY);
    }

    if (dbName == null || "".equals(dbName.trim())) {
        throw new IllegalArgumentException(
                this.getClass().getName() + " expects configuration property " + DB_NAME_KEY);
    }

    if (collectionName == null || "".equals(collectionName.trim())) {
        throw new IllegalArgumentException(
                this.getClass().getName() + " expects configuration property " + COLLECTION_NAME_KEY);
    }

    Mongo mongo = null;
    try {
        mongo = new Mongo(host, port);
    } catch (Exception e) {
        throw new InitializationFailedException(e.getMessage());
    }
    db = mongo.getDB(dbName);
    collection = db.getCollection(collectionName);
}

From source file:plugins.MongoDB.java

License:Open Source License

public static void start() {
    self = new MongoDB();
    Logger.info("Mongo connecting");
    try {/*  w w w . j  ava  2  s. c  om*/
        /* The Mongo object instance actually represents a pool of connections
        to the database; you will only need one object of class Mongo
        even with multiple threads.
         */
        mongo = new Mongo(ADDR, Integer.parseInt(PORT));
        db = mongo.getDB(DBNAME);
        /*
        Set<String> colls = db.getCollectionNames();
        for (String s : colls) {
        Logger.info(s);
        morphia.map(Class.forName(s));
        }
         */
        morphia = new Morphia();

        morphia.map(Message.class);
        morphia.map(User.class);
        morphia.map(MessageThread.class);
        morphia.map(Bookmark.class);
        morphia.map(Activity.class);
        morphia.map(NodeContent.class);
        morphia.map(UserLocation.class);
        morphia.map(Friend.class);
        morphia.map(Fook.class);
        morphia.map(Ignore.class);
        morphia.map(UserGroup.class);
        morphia.map(Tag.class);
        morphia.map(Vote.class);
        morphia.map(Page.class);
        morphia.map(ViewTemplate.class);
        morphia.map(Feed.class);

    } catch (Exception e) {
        Logger.info("Brekeke @ mongo:: " + e.toString());
    }
}

From source file:Script.SaxManejadorIndice.java

public SaxManejadorIndice() throws UnknownHostException, IOException {
    mongo = new Mongo("localhost", 27017);

    // nombre de la base de datos
    database = mongo.getDB("paginas");
    // coleccion de la db
    collection = database.getCollection("indice");
    ArrayList lee = new ArrayList<>();
    lee = lista();/*from   w w  w  .  j  a  v a  2 s .c o  m*/
    listaStopWords = StopWordsArray((String) lee.get(1));

}

From source file:SentimentAnalyses.PrintSampleStream.java

License:Apache License

public void LinkMongodb() throws Exception {

    /*/* w  w w  .  j  a v  a  2  s  .  com*/
     * Link Mongodb
     * build a data named FourS2
     * build a collection named Foursquare
     *
     */
    mongo = new Mongo("localhost", 27017);
    DB db = mongo.getDB("tstream");

    //collection = db.createCollection("movie", db);
    collection = db.getCollection("Flight MH370");
    //        collection.drop();
    System.out.println("Link Mongodb!");
}

From source file:servertcp.DataBase.java

public static void initMongoDB(String ip, int port, String DBname, String CollectionName)
        throws UnknownHostException {

    Mongo mongo = new Mongo(ip, port);
    db = mongo.getDB(DBname);//w w w.j a v  a2 s .  c  o  m
    collection = db.getCollection(CollectionName);

}

From source file:session.BookSessionBean.java

@PostConstruct
private void initDB() {
    Mongo mongo = null;/*w  w w.  ja v  a 2  s  .c  om*/
    try {
        mongo = new Mongo("ds033133.mongolab.com", 33133);
    } catch (UnknownHostException ex) {
        Logger.getLogger(BookSessionBean.class.getName()).log(Level.SEVERE, null, ex);
    }

    DB db = mongo.getDB("librosdb");
    boolean auth = db.authenticate("cposdaa", "--".toCharArray());
    if (auth) {
        bookCollection = db.getCollection("books");
        //valido si la colleccion existe si no la crea
        if (bookCollection == null) {
            bookCollection = db.createCollection("books", null);
        }

    } else {
        System.out.println("Login failed");
    }

}

From source file:stirling.fix.session.store.MongoSessionStore.java

License:Apache License

public MongoSessionStore(String address, int port) throws SessionStoreException {
    // Fail-fast if MongoDB is not up and running
    try {/*from www  .jav a2s.  c om*/
        mongo = new Mongo(address, port);
        mongo.getDatabaseNames();
        db = mongo.getDB("fixengine");
    } catch (Exception e) {
        throw new SessionStoreException("MongoDB is not listening at " + address + ":" + port);
    }
}