Example usage for com.mongodb MongoClientURI MongoClientURI

List of usage examples for com.mongodb MongoClientURI MongoClientURI

Introduction

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

Prototype

public MongoClientURI(final String uri) 

Source Link

Document

Creates a MongoURI from the given string.

Usage

From source file:com.staticvillage.recommender.indexer.MongoDBIndexer.java

License:Apache License

public MongoDBIndexer(String connectionString, String database, String collection) throws UnknownHostException {
    this.connectionString = connectionString;
    this.database = database;
    this.collection = collection;

    MongoClientURI uri = new MongoClientURI(connectionString);
    mongoClient = new MongoClient(uri);
    instanceDB = mongoClient.getDB(database);
}

From source file:com.streamsets.pipeline.stage.destination.mongodb.MongoDBTarget.java

License:Apache License

@Override
protected List<ConfigIssue> init() {
    List<ConfigIssue> issues = super.init();

    errorRecordHandler = new DefaultErrorRecordHandler(getContext());
    MongoClientURI connectionString = new MongoClientURI(mongoTargetConfigBean.mongoClientURI);
    mongoClient = new MongoClient(connectionString);
    MongoDatabase db = mongoClient.getDatabase(mongoTargetConfigBean.database);
    coll = db.getCollection(mongoTargetConfigBean.collection)
            .withWriteConcern(mongoTargetConfigBean.writeConcern.getWriteConcern());

    DataGeneratorFactoryBuilder builder = new DataGeneratorFactoryBuilder(getContext(),
            DataFormat.JSON.getGeneratorFormat());
    builder.setCharset(StandardCharsets.UTF_8);

    builder.setMode(JsonMode.MULTIPLE_OBJECTS);
    generatorFactory = builder.build();/*from   w w  w .  j ava 2 s  .c  o  m*/

    return issues;
}

From source file:com.streamsets.pipeline.stage.origin.mongodb.MongoDBSource.java

License:Apache License

private boolean parseServerList(String mongoConnectionString, List<ServerAddress> servers,
        List<ConfigIssue> issues) {
    boolean isOk = true;
    MongoClientURI mongoURI = new MongoClientURI(mongoConnectionString);
    List<String> hosts = mongoURI.getHosts();

    // Validate each host in the connection string is valid. MongoClient will not tell us
    // if something is wrong when we try to open it.
    for (String host : hosts) {
        String[] hostport = host.split(":");
        if (hostport.length != 2) {
            issues.add(getContext().createConfigIssue(Groups.MONGODB.name(), "mongoConnectionString",
                    Errors.MONGODB_07, host));
            isOk = false;/* www . j a  v  a 2  s. co m*/
        } else {
            try {
                InetAddress.getByName(hostport[0]);
                servers.add(new ServerAddress(hostport[0], Integer.parseInt(hostport[1])));
            } catch (UnknownHostException e) {
                issues.add(getContext().createConfigIssue(Groups.MONGODB.name(), "mongoConnectionString",
                        Errors.MONGODB_09, hostport[0]));
                isOk = false;
            } catch (NumberFormatException e) {
                issues.add(getContext().createConfigIssue(Groups.MONGODB.name(), "mongoConnectionString",
                        Errors.MONGODB_08, hostport[1]));
                isOk = false;
            }
        }
    }
    return isOk;
}

From source file:com.techngage.smartbin.Controller.java

License:Apache License

public Controller(String mongoURIString) throws IOException {
    final MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoURIString));
    final MongoDatabase smartbinDatabase = mongoClient.getDatabase("smartbin");

    userDAO = new UserDAO(smartbinDatabase);
    sessionDAO = new SessionDAO(smartbinDatabase);
    routeDAO = new RouteDAO(smartbinDatabase);
    truckDAO = new TruckDAO(smartbinDatabase);
    locationDAO = new LocationDAO(smartbinDatabase);

    cfg = createFreemarkerConfiguration();
    setPort(8082);//from ww w  . j a  v  a  2 s  .  co  m
    Spark.staticFileLocation("/public");
    initializeRoutes();
}

From source file:com.thaonedroid.mongodbexercise.MongoDB.java

public static void main(String[] args) {
    try {/*from w  ww. j  ava  2 s.c om*/
        MongoClientURI connStr = new MongoClientURI("mongodb://localhost:27017");
        MongoClient mongoClient = new MongoClient(connStr);

        MongoDatabase db = mongoClient.getDatabase("twitter");
        MongoCollection<Document> collection = db.getCollection("tweets");

        Scanner scanner = new Scanner(System.in);
        int input;
        OUTER: do {
            start();
            input = scanner.nextInt();
            switch (input) {
            case 1:
                //System.out.println("Distinct Twitter users : " + query.totalUsers());
                System.out.println("Question : " + q1);
                execute(query.totalUsers());
                break;
            case 2:
                //execute(query.linkOthers());
                System.out.println("Question : " + q2);
                System.out.println("Answer : Query hasn't been properly figured out yet");
                break;
            case 3:
                //execute(query.mostMentioned());
                System.out.println("Question : " + q3);
                System.out.println("Answer : Query hasn't been properly figured out yet");
                break;
            case 4:
                System.out.println("Question : " + q4);
                execute(query.mostActive());
                //System.out.println("Most active users : " + query.mostActive());
                break;
            case 5:
                System.out.println("Question : " + q5);
                execute(query.mostGrumpy());
                //System.out.println("Most grumpy users : " + query.mostGrumpy());
                break;
            case 6:
                System.out.println("Question : " + q6);
                execute(query.mostHappy());
                //System.out.println("Most happy users : " + query.mostHappy());
                break;
            case 7:
                break OUTER;
            default:
                break;
            }
        } while (true);
        mongoClient.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ":" + e.getMessage());
    }
}

From source file:com.threecrickets.sincerity.logging.MongoDbManager.java

License:LGPL

@Override
protected void startupInternal() throws Exception {
    if (collection != null)
        return;//from  w  w  w . j a v a  2s  .  com

    client = null;
    database = null;
    collection = null;

    MongoClientURI uri;
    try {
        if (this.uri == null)
            uri = new MongoClientURI(this.uri);
        else
            uri = new MongoClientURI("mongodb://localhost:27017/");
    } catch (IllegalArgumentException x) {
        throw new AppenderLoggingException("Can't parse MongoDB uri: " + this.uri);
    }

    try {
        client = new MongoClient(uri);
    } catch (MongoException x) {
        throw new AppenderLoggingException("Can't create MongoDB client: " + uri);
    }

    try {
        database = client.getDatabase(databaseName);
    } catch (MongoException x) {
        client.close();
        client = null;
        throw new AppenderLoggingException("Can't access MongoDB database: " + databaseName);
    }

    try {
        collection = database.getCollection(collectionName);
    } catch (MongoException x) {
        client.close();
        client = null;
        database = null;
        throw new AppenderLoggingException("Can't access MongoDB collection: " + collectionName);
    }

    if (writeConcernName != null) {
        WriteConcern writeConcern = WriteConcern.valueOf(writeConcernName);
        if (writeConcern == null)
            throw new AppenderLoggingException("Unsupported MongoDB write concern: " + writeConcernName);
        collection = collection.withWriteConcern(writeConcern);
    }
}

From source file:com.tremolosecurity.mongodb.myvd.MongoInsert.java

License:Apache License

public void configure(String name, Properties props, NameSpace nameSpace) throws LDAPException {
    this.name = name;
    this.nameSpace = nameSpace;
    this.mongo = new MongoClient(new MongoClientURI(props.getProperty("url")));
    this.lcaseBase = nameSpace.getBase().getDN().toString().toLowerCase();
    this.database = props.getProperty("database");

}

From source file:com.tremolosecurity.mongodb.unison.MongoDBTarget.java

License:Apache License

public void init(Map<String, Attribute> cfg, ConfigManager cfgMgr, String name) throws ProvisioningException {
    this.cfgMgr = cfgMgr;
    this.name = name;
    this.mongo = new MongoClient(new MongoClientURI(cfg.get("url").getValues().get(0)));
    this.database = cfg.get("database").getValues().get(0);

    this.userObjectClass = cfg.get("userObjectClass").getValues().get(0);
    this.userRDN = cfg.get("userRDN").getValues().get(0);
    this.userIdAttribute = cfg.get("userIdAttribute").getValues().get(0);
    this.groupIdAttribute = cfg.get("groupIdAttribute").getValues().get(0);
    this.groupObjectClass = cfg.get("groupObjectClass").getValues().get(0);
    this.groupRDN = cfg.get("groupRDN").getValues().get(0);
    this.groupMemberAttribute = cfg.get("groupMemberAttribute").getValues().get(0);
    this.groupUserIdAttribute = cfg.get("groupUserIdAttribute").getValues().get(0);
    this.supportExternalUsers = cfg.get("supportExternalUsers").getValues().get(0).equalsIgnoreCase("true");
    this.collectionAttributeName = cfg.get("collectionAttributeName").getValues().get(0);

    cfgMgr.addThread(new StopableThread() {

        public void run() {

        }//from www.  j  av a  2  s .com

        public void stop() {
            mongo.close();

        }
    });

}

From source file:com.twiceagain.mywebdriver.driver.mongo.Mongos.java

License:Open Source License

/**
 * Get a (singleton) instance of the Mongo client.
 * @return /*from   w ww  . j av  a 2 s .co  m*/
 */
public static MongoClient getClient() {
    if (client == null) {
        client = new MongoClient(new MongoClientURI(connection));
    }
    return client;
}

From source file:com.udoheld.aws.lambda.json.to.mongodb.ProcessDataHandler.java

License:Open Source License

private MongoClient initConnection(String connectionUri) {
    MongoClientURI uri = new MongoClientURI(connectionUri);
    MongoClient mongoClient;//  ww  w .  j  av  a 2  s  . co m
    if (createGlobalConnection) {
        mongoClient = initGlobalConnection(uri);
    } else {
        mongoClient = new MongoClient(uri);
        localMongoClient = mongoClient;
    }
    return mongoClient;
}