Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

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

Prototype

public MongoClient(final MongoClientURI uri, final MongoDriverInformation mongoDriverInformation) 

Source Link

Document

Creates a Mongo described by a URI.

Usage

From source file:com.almende.eve.state.mongo.MongoStateFactory.java

License:Apache License

private static MongoClient createClient(String databaseUri, int port) throws UnknownHostException {
    MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(100)
            .threadsAllowedToBlockForConnectionMultiplier(1500).build();
    MongoClient client = new MongoClient(new ServerAddress(databaseUri, port), options);
    return client;
}

From source file:com.amertkara.tinkerpop.blueprints.impl.mongodb.embedded.InMemoryMongoDB.java

License:Apache License

/**
 * @throws java.lang.Exception void//from  www. j a v  a  2  s .c  o m
 */
@Before
public void setUp() throws Exception {
    mongoClient = new MongoClient(HOST, PORT);
    mongoDB = mongoClient.getDatabase(DB_NAME);
    mongoDB.createCollection(COLLECTION_EDGES);
    mongoDB.createCollection(COLLECTION_VERTICES);
}

From source file:com.amertkara.tinkerpop.blueprints.impl.mongodb.MongoDBGraph.java

License:Apache License

/**
 * TODO: Implement constructors that take {@link Properties} and {@link MongoClientOptions}
 * /*from w w w . j a va  2s .co m*/
 * @param host
 * @param port
 * @param databaseName
 * @param edgeCollection
 * @param vertexCollection
 */
public MongoDBGraph(final String host, final int port, final String databaseName, final String edgeCollection,
        final String vertexCollection) {

    if (edgeCollection == null || edgeCollection.isEmpty() || vertexCollection == null
            || vertexCollection.isEmpty() || databaseName == null || databaseName.isEmpty()) {
        throw new IllegalStateException("edgeCollection, vertexCollection and databaseName cannot be empty.");
    }

    if (host != null && !host.isEmpty()) {
        mongoClient = new MongoClient(host, port);
    } else {
        mongoClient = new MongoClient();
    }

    this.edgeCollection = edgeCollection;
    this.vertexCollection = vertexCollection;
    this.rawGraph = mongoClient.getDatabase(databaseName);

    this.rawGraph.getCollection(edgeCollection).createIndex(
            new Document(MongoDBConstants.FIELD_ID, new Integer(1)).append("unique", new Boolean(true)));
    this.rawGraph.getCollection(vertexCollection).createIndex(
            new Document(MongoDBConstants.FIELD_ID, new Integer(1)).append("unique", new Boolean(true)));
}

From source file:com.analogmountains.flume.MongoSink.java

License:Open Source License

@Override
public synchronized void start() {
    logger.info("Starting MongoDB sink");
    sinkCounter.start();/*from  w  w  w .  j  av  a  2s .  c  om*/
    try {
        client = new MongoClient(seeds, Arrays.asList(credential));
        MongoDatabase database = client.getDatabase(databaseName);
        collection = database.getCollection(collectionName);
        sinkCounter.incrementConnectionCreatedCount();
    } catch (Exception e) {
        logger.error("Exception while connecting to MongoDB", e);
        sinkCounter.incrementConnectionFailedCount();
        if (client != null) {
            client.close();
            sinkCounter.incrementConnectionClosedCount();
        }
    }
    super.start();
    logger.info("MongoDB sink started");
}

From source file:com.andersen.backendjaxrsproject.EmployeeServices.java

public EmployeeServices() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    employeeDB = mongo.getDatabase("Employees");
}

From source file:com.avbravo.jgmongodesktopexample.Start.java

/**
 * @param args the command line arguments
 */// w w  w  . j a v  a  2 s.c  o  m
public static void main(String[] args) {
    // TODO code application logic here
    try {

        PaisesFacade paisesFacade = new PaisesFacade();
        SedesFacade sedesFacade = new SedesFacade();
        ProvinciasFacade provinciasFacade = new ProvinciasFacade();

        MongoClient client = new MongoClient("localhost", 27017);
        MongoDatabase database = client.getDatabase("fantasy");
        MongoCollection<Document> collection = database.getCollection("paises");

        List<Document> documents = (List<Document>) collection.find().into(new ArrayList<Document>());

        //Document document = new Document("name","Cuba")
        //        .append("poblacion", 7777);
        //
        //       collection.insertOne(document);

        // DBCollection coll = database.getCollection("mycol");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss a");
        Date startDate = simpleDateFormat.parse(simpleDateFormat.format(new Date()));

        MongoCollection<BasicDBObject> coll = database.getCollection("paises", BasicDBObject.class);
        BasicDBObject doc = new BasicDBObject("title", "OrientDB").append("Fecha", startDate)
                .append("url", "http://www.tutorialspoint.com/mongodb/").append("by", "tutorials point");
        coll.insertOne(doc);

        for (Document document : documents) {
            System.out.println(document);
            System.out.println("Siglas" + document.get("Siglas"));
            System.out.println("Pais" + document.get("Pais"));
        }

        //            
        //            List<Provincias> list = provinciasFacade.findAll();
        //            list.forEach((p) -> {
        //                System.out.println(" "+p.toString());
        //            });

        //                    
        //            Provincias provincias = new Provincias();
        //            provincias.setIdprovincia("6");
        //            Provincias pr = provinciasFacade.findById(provincias);
        //            
        //            if(pr.getIdprovincia() == null){
        //                System.out.println("---> no hay provincia con ese id");
        //            }else{
        //                System.out.println("---> "+pr.toString());
        //            }

        //            Paises paises = new Paises();
        //            paises.setSiglas("pa");
        //            Paises p = paisesFacade.findById(paises);
        //
        //         if(p.getSiglas() == null){
        //                System.out.println("============================");
        //                System.out.println("no hay paises con siglas "+paises.getSiglas());
        //            }else{
        //                System.out.println("paises: "+p.toString());
        //            }
        //            Provincias provincias = new Provincias();
        //            provincias.setIdprovincia("2");
        //            provincias.setProvincia("Cocle");
        //            provincias.setPaises(p);
        //          //  provincias.setHola("Hola");
        //            
        //            if(provinciasFacade.save(provincias)){
        //                System.out.println("guardado");
        //            }else{
        //                System.out.println("no se guardo");
        //            }
        //          
        //            List<Paises> list = paisesFacade.findAll();
        //          Sedes sedes = new Sedes();
        //               sedes.setIdsede("s-2");
        //                sedes.setSede("Sede 2");
        //                sedes.setPaises(list);
        //                sedesFacade.save(sedes);
        //          

    } catch (Exception e) {
        System.out.println("error " + e.getLocalizedMessage());
    }

}

From source file:com.aw.util.MongoDbUtil.java

public static MongoClient getMongoClient() {
    if (mongoClient != null) {
        return mongoClient;
    }/* ww w  . j a v  a2s.c  o  m*/
    try {
        MongoClient mongo = new MongoClient(HOST_NAME, port);
        mongoClient = mongo;
        return mongoClient;
    } catch (UnknownHostException ex) {
        Logger.getLogger(MongoDbUtil.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:com.azure.api.MongoDAO.java

public static LinkedList<Pojoo> MongoConnection() throws IOException {

    MongoClient mongo = new MongoClient("localhost", 27017);

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

    DBCollection table = db.getCollection("DataFinal");

    List<String> uniId = table.distinct("Asin");

    //BasicDBObject searchQuery = new BasicDBObject();
    //searchQuery.put("Asin", "000100039X");
    DBObject groupFields = new BasicDBObject("_id", "$Asin");

    groupFields.put("count", new BasicDBObject("$sum", 1));
    //DBObject group = new BasicDBObject("$group", groupFields );
    DBObject sortFields = new BasicDBObject("count", -1);
    //AggregationOutput output = table.aggregate(group, new BasicDBObject("$sort", sortFields ));
    //System.out.println(output.results());//Top reviews 

    groupFields.put("average", new BasicDBObject("$avg", "$Ratings"));
    DBObject group2 = new BasicDBObject("$group", groupFields);

    AggregationOutput output2 = table.aggregate(group2, new BasicDBObject("$sort", sortFields));

    Iterable<DBObject> iterable = output2.results();

    //LinkedList<String>stringResult = new LinkedList<String>();

    //LinkedList<String>stringrating = new LinkedList<String>();

    //if(awsResult.size()!=0){awsResult.remove();}
    //if(stringResult.size()!=0){stringResult.remove();}

    //System.out.println("fffffffffffffffff"+stringResult.size());

    int count = 0;

    LinkedList<Pojoo> uiList = new LinkedList<Pojoo>();
    for (DBObject res : output2.results()) {

        //System.out.println("TTTTTTTT "+res.get("_id").toString());
        //System.out.println("VVVVVVVV "+res.get("average").toString());
        String awsResult = awsApi.getDetails(res.get("_id").toString());
        //stringResult.add(res.get("_id").toString());
        //stringrating.add(res.get("average").toString());
        Pojoo polo = new Pojoo();
        polo.setStringResult(awsResult);
        polo.setStringrating(res.get("average").toString());
        uiList.add(polo);//from ww w  .ja va  2  s.c om

        count++;
        if (count == 8) {
            res = null;
            break;
        }
    }

    count = 0;

    //awsResult = awsApi.getDetails(stringResult);
    //for(String htp: awsResult){
    //Pojoo polo = new Pojoo();
    //polo.setStringResult(htp);
    //polo.setStringrating(res.get("average").toString());
    //uiList.add(polo);
    //}

    // System.out.println("WWWWWW"+awsResult);
    //Pojoo img = new Pojoo();
    //img.setStringrating(stringrating);
    //img.setStringResult(awsResult);
    //stringResult.remove();

    //System.out.println("eeeeeeeeeeeeeee"+awsResult.size());
    //for(String ret:awsResult)
    //{
    //   System.out.println(ret);
    //}

    return uiList;

}

From source file:com.azure.api.MongoDAO.java

public static LinkedList<String> MongoConnection2() throws IOException {

    MongoClient mongo = new MongoClient("localhost", 27017);

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

    DBCollection table = db.getCollection("DataFinal");
    DBObject sortFields = new BasicDBObject("count", -1);
    DBObject groupGenreFields = new BasicDBObject("_id", "$Genre1");
    groupGenreFields.put("count", new BasicDBObject("$sum", 1));
    DBObject genregroup = new BasicDBObject("$group", groupGenreFields);
    AggregationOutput Genreoutput = table.aggregate(genregroup, new BasicDBObject("$sort", sortFields));

    LinkedList<String> stringResult = new LinkedList<String>();

    int count = 0;
    for (DBObject res : Genreoutput.results()) {
        stringResult.add(res.get("_id").toString());
        System.out.println(res.get("_id").toString());
        count++;/*from w ww .  j ava 2  s . c  o  m*/
        if (count == 8)
            break;

    }

    System.out.print("strinResult Size" + stringResult.size());

    return stringResult;

}

From source file:com.azure.api.MongoDAO.java

public static LinkedList<String> MongoConnection3() throws IOException {

    MongoClient mongo = new MongoClient("localhost", 27017);

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

    DBCollection table = db.getCollection("DataFinal");
    DBObject sortFields = new BasicDBObject("count", -1);
    DBObject groupGenreFields = new BasicDBObject("_id", "$Author");
    groupGenreFields.put("count", new BasicDBObject("$sum", 1));
    DBObject genregroup = new BasicDBObject("$group", groupGenreFields);
    AggregationOutput Genreoutput = table.aggregate(genregroup, new BasicDBObject("$sort", sortFields));

    LinkedList<String> stringResult = new LinkedList<String>();

    int count = 0;
    for (DBObject res : Genreoutput.results()) {
        stringResult.add(res.get("_id").toString());
        System.out.println(res.get("_id").toString());
        count++;/*from w  ww . j a v a2  s  .  c  o m*/
        if (count == 8)
            break;

    }

    System.out.print("strinResult Size" + stringResult.size());

    return stringResult;

}