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) 

Source Link

Usage

From source file:eu.cassandra.server.mongo.util.DBConn.java

License:Apache License

public static DB getConn(String dbname) {
    if (dbname == null)
        return getConn();
    else if (dbRuns.containsKey(dbname)) {
        return dbRuns.get(dbname);
    } else {/*w w  w .j a v a 2 s  .  c  om*/
        try {
            Mongo m = new Mongo(DB_HOST);
            dbRuns.put(dbname, m.getDB(dbname));
            return dbRuns.get(dbname);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:eu.cassandra.sim.PricingPolicy.java

License:Apache License

/**
 * @param args/*  w  w  w.ja  v a 2s  .  c  o  m*/
 * @throws MongoException 
 * @throws UnknownHostException 
 * @throws ParseException 
 */
public static void main(String[] args) throws UnknownHostException, MongoException, ParseException {
    // TODO Auto-generated method stub
    String prc_id = "51778737e4b02bc3aca36960";
    DBObject query = new BasicDBObject(); // A query
    query.put("_id", new ObjectId(prc_id));
    Mongo m = new Mongo("localhost");
    DB db = m.getDB("test");
    DBObject pricingPolicy = db.getCollection(MongoPricingPolicy.COL_PRICING).findOne(query);
    PricingPolicy pp = new PricingPolicy(pricingPolicy);
    System.out.println(pp.getType());
    System.out.println(pp.getFixedCharge());
    System.out.println(pp.calculateCost(1500, 0, 0, 0, 1));
}

From source file:eu.cassandra.sim.utilities.DBConn.java

License:Apache License

/**
 * Instantiates a new database connection.
 *//*from   ww  w .  ja va 2 s  . c o  m*/
private DBConn() {
    try {
        m = new Mongo(DB_HOST);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
    db = m.getDB(DB_NAME);
}

From source file:eu.cassandra.sim.utilities.DBConn.java

License:Apache License

/**
 * Gets a connection to the specified database.
 *
 * @param dbname the database name/*from ww w.  ja  va  2  s  .  co  m*/
 * @return the database connection
 */
public static DB getConn(String dbname) {
    if (dbname == null)
        return getConn();
    else if (dbRuns.containsKey(dbname)) {
        return dbRuns.get(dbname);
    } else {
        try {
            Mongo m = new Mongo(DB_HOST);
            dbRuns.put(dbname, m.getDB(dbname));
            return dbRuns.get(dbname);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (MongoException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:fi.vm.sade.osoitepalvelu.kooste.config.MongoConfig.java

License:EUPL

@Bean
@Override//from   w ww  . ja v  a  2s  . c  om
public Mongo mongo() throws UnknownHostException {
    return new Mongo(new MongoURI(new MongoClientURI(mongoUri)));
}

From source file:fi.vm.sade.osoitepalvelu.MongoTestConfig.java

License:EUPL

@Bean
@Override
public Mongo mongo() throws Exception {
    initMongo();
    return new Mongo(hostAndPort());
}

From source file:fr.xebia.cocktail.CocktailRepository.java

License:Apache License

@Inject
public CocktailRepository(@Value("${mongodb_uri}") String mongoUri, @Value("${solr_url}") String solrUri)
        throws UnknownHostException, MalformedURLException {
    MongoURI databaseUri = new MongoURI(mongoUri);
    mongo = new Mongo(databaseUri);

    db = mongo.getDB(databaseUri.getDatabase());
    if (!Strings.isNullOrEmpty(databaseUri.getUsername())) {
        db.authenticate(databaseUri.getUsername(), databaseUri.getPassword());
    }/*ww w  .  j ava2s.com*/
    cocktails = db.getCollection("cocktails");

    solrServer = new CommonsHttpSolrServer(solrUri);

}

From source file:home.vitaly.simulator.mongoCursorReader.java

License:Open Source License

private Mongo getConnection() throws Exception {
    return new Mongo("localhost:27017");
}

From source file:implementations.mongoDB.java

License:GNU General Public License

@Override
public int connectNode(String nodeAddress) {
    int ret;//  w ww. j  a  va 2s  .co m
    try {
        mdb = new Mongo(nodeAddress);
        //mdb.slaveOk();
        db = mdb.getDB("test");
        dbCol = db.getCollection("test");
        DBCursor cur = dbCol.find();
        ret = 1;
    } catch (UnknownHostException e) {
        ret = -1;
        e.printStackTrace();
    } catch (MongoException e) {
        ret = 1;
        e.printStackTrace();
    }
    return ret;
}

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 {//from  ww  w .  j  a  v a 2  s  .  c om
            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");
}