Example usage for com.mongodb MongoClientURI getHosts

List of usage examples for com.mongodb MongoClientURI getHosts

Introduction

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

Prototype

public List<String> getHosts() 

Source Link

Document

Gets the list of hosts

Usage

From source file:com.cloudbees.demo.beesshop.util.MongodbDBFactoryBean.java

License:Apache License

@Override
public void afterPropertiesSet() throws Exception {
    MongoClientURI mongoDbUri = new MongoClientURI(uri);
    logger.info("host=" + mongoDbUri.getHosts() + ",username=" + mongoDbUri.getUsername() + ",database="
            + mongoDbUri.getDatabase() + ",collection=" + mongoDbUri.getCollection());

    mongoClient = new MongoClient(mongoDbUri);
    db = mongoClient.getDB(mongoDbUri.getDatabase());
}

From source file:com.github.nlloyd.hornofmongo.MongoRuntime.java

License:Open Source License

/**
 * Creates a newly initialized {@link MongoScope} instance with a connection
 * to the specified mongodb instance/cluster. This will use
 * {@link MongoRuntime#call(MongoAction)} to initialize the
 * {@link MongoScope} instance, possibly resulting in the global
 * {@link MongoContextFactory} being set.
 * /*from  w  w w  .j  a  v a2  s .  c om*/
 * After the scope is initialized a call via the mongo JS API to the
 * "connect()" method will be made to initialize the global db instance.
 * 
 * @return
 */
public static final MongoScope createMongoScope(final MongoClientURI mongoClientURI,
        boolean useMongoShellWriteConcern, boolean mimicShellExceptionBehavior) throws UnknownHostException {
    if (StringUtils.isBlank(mongoClientURI.getDatabase()))
        throw new IllegalArgumentException("mongo client uri must have a database");
    MongoScope mongoScope = createMongoScope();
    mongoScope.setUseMongoShellWriteConcern(useMongoShellWriteConcern);
    mongoScope.setStdoutMongoErrorMessages(mimicShellExceptionBehavior);

    StringBuilder connectStrBuilder = new StringBuilder("db = connect('");

    if ((mongoClientURI.getHosts().size() == 1) && (mongoClientURI.getHosts().get(0).equals("localhost")
            || mongoClientURI.getHosts().get(0).equals("localhost:27017")))
        connectStrBuilder.append(mongoClientURI.getDatabase());
    else
        connectStrBuilder.append(mongoClientURI.getURI());

    connectStrBuilder.append("', null, null);");

    call(new MongoScriptAction(mongoScope, "connect", connectStrBuilder.toString()));

    return mongoScope;
}

From source file:com.streamsets.pipeline.stage.common.mongodb.MongoDBConfig.java

License:Apache License

private MongoClient createClient(Stage.Context context, List<Stage.ConfigIssue> issues,
        ReadPreference readPreference, WriteConcern writeConcern) {
    MongoClientOptions.Builder optionBuilder = new MongoClientOptions.Builder()
            .connectionsPerHost(connectionsPerHost).connectTimeout(connectTimeout)
            .cursorFinalizerEnabled(cursorFinalizerEnabled).heartbeatConnectTimeout(heartbeatConnectTimeout)
            .heartbeatFrequency(heartbeatFrequency).heartbeatSocketTimeout(heartbeatSocketTimeout)
            .localThreshold(localThreshold).maxConnectionIdleTime(maxConnectionIdleTime)
            .maxConnectionLifeTime(maxConnectionLifeTime).maxWaitTime(maxWaitTime)
            .minConnectionsPerHost(minConnectionsPerHost).minHeartbeatFrequency(minHeartbeatFrequency)
            .serverSelectionTimeout(serverSelectionTimeout).socketKeepAlive(socketKeepAlive)
            .socketTimeout(socketTimeout).sslEnabled(sslEnabled)
            .sslInvalidHostNameAllowed(sslInvalidHostNameAllowed)
            .threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);

    // the default value of requiredReplicaSetName is null, so it should be set only if a non-empty string is provided
    if (!requiredReplicaSetName.isEmpty()) {
        optionBuilder = optionBuilder.requiredReplicaSetName(requiredReplicaSetName);
    }//from   w w  w .  ja  va 2s . c o  m
    // read preference is only set by the source
    if (readPreference != null) {
        optionBuilder = optionBuilder.readPreference(readPreference);
    }
    // write concern is only set by the target
    if (writeConcern != null) {
        optionBuilder = optionBuilder.writeConcern(writeConcern);
    }

    MongoClientURI mongoURI;
    List<ServerAddress> servers = new ArrayList<>();
    try {
        mongoURI = new MongoClientURI(connectionString, optionBuilder);
    } catch (IllegalArgumentException e) {
        issues.add(context.createConfigIssue(Groups.MONGODB.name(), MONGO_CONFIG_PREFIX + "connectionString",
                Errors.MONGODB_00, e.toString()));
        return null;
    }

    validateServerList(context, mongoURI.getHosts(), servers, issues);
    if (!issues.isEmpty()) {
        return null;
    }

    MongoClient mongoClient = null;
    List<MongoCredential> credentials;
    try {
        credentials = createCredentials();
    } catch (StageException ex) {
        issues.add(context.createConfigIssue(Groups.MONGODB.name(), MONGO_CONFIG_PREFIX + "connectionString",
                Errors.MONGODB_34, ex.toString()));
        return null;
    }

    if (credentials.isEmpty()) {
        Optional.ofNullable(mongoURI.getCredentials()).ifPresent(credentials::add);
    }

    try {
        if (isSingleMode) {
            mongoClient = new MongoClient(servers.get(0), credentials, mongoURI.getOptions());
        } else {
            mongoClient = new MongoClient(servers, credentials, mongoURI.getOptions());
        }
    } catch (MongoException e) {
        issues.add(context.createConfigIssue(Groups.MONGODB.name(), MONGO_CONFIG_PREFIX + "connectionString",
                Errors.MONGODB_01, e.toString()));
    }

    return mongoClient;
}

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;//ww w .  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.zjy.mongo.splitter.MultiMongoCollectionSplitter.java

License:Apache License

@Override
public List<InputSplit> calculateSplits() throws SplitFailedException {
    List<MongoClientURI> inputURIs = MongoConfigUtil.getMongoURIs(this.getConfiguration(),
            MongoConfigUtil.INPUT_URI);
    List<InputSplit> returnVal = new LinkedList<InputSplit>();
    List<MongoSplitter> splitters = new LinkedList<MongoSplitter>();

    //For each input URI that is specified, get the appropriate
    //splitter for each implementation.
    if (inputURIs.size() > 0) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Using global split settings for multiple URIs specified.");
        }/*from   ww w  .  j  a  v a 2 s.c  o  m*/
        //Get options from the hadoop config.
        //Use these options for all URIs in the list.

        //Splitter class is ignored here, since it would just be referring
        //to MultiMongoCollectionSplitter here anyway. If the user needs
        //to customize the splitter class, they should use the JSON key for
        //the configuration instead.
        for (MongoClientURI uri : inputURIs) {
            MongoCollectionSplitter splitter;
            Configuration confForThisUri = new Configuration(getConfiguration());
            MongoConfigUtil.setInputURI(confForThisUri, uri);
            confForThisUri.set(MongoConfigUtil.MONGO_SPLITTER_CLASS, "");
            splitter = MongoSplitterFactory.getSplitterByStats(uri, confForThisUri);
            splitters.add(splitter);
        }
    } else {
        //Otherwise the user has set options per-collection.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Loading multiple input URIs from JSON stored in " + MULTI_COLLECTION_CONF_KEY);
        }
        DBObject multiUriConfig = MongoConfigUtil.getDBObject(this.getConfiguration(),
                MULTI_COLLECTION_CONF_KEY);

        if (!(multiUriConfig instanceof List)) {
            throw new IllegalArgumentException(
                    "Invalid JSON format in multi uri config key: Must be an array where each element "
                            + "is an object describing the URI and config options for each split.");
        }
        for (Object obj : (List) multiUriConfig) {
            Map<String, Object> configMap;
            MongoClientURI inputURI;
            Configuration confForThisUri;
            try {
                configMap = (Map<String, Object>) obj;
                if (LOG.isDebugEnabled()) {
                    LOG.debug("building config from " + configMap.toString());
                }
                confForThisUri = MongoConfigUtil.buildConfiguration(configMap);
                inputURI = MongoConfigUtil.getInputURI(confForThisUri);
            } catch (ClassCastException e) {
                throw new IllegalArgumentException(
                        "Invalid JSON format in multi uri config key: each config item must be an "
                                + "object with keys/values describing options for each URI.");
            }
            MongoSplitter splitter;
            Class<? extends MongoSplitter> splitterClass = MongoConfigUtil.getSplitterClass(confForThisUri);

            if (splitterClass != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(format("Using custom Splitter class for namespace: %s.%s; hosts: %s",
                            inputURI.getDatabase(), inputURI.getCollection(), inputURI.getHosts()));
                }
                //User wants to use a specific custom class for this URI.
                //Make sure that the custom class isn't this one
                if (splitterClass == MultiMongoCollectionSplitter.class) {
                    throw new IllegalArgumentException("Can't nest uses of MultiMongoCollectionSplitter");
                }
                //All clear.
                MongoCollectionSplitter collectionSplitter;
                collectionSplitter = (MongoCollectionSplitter) ReflectionUtils.newInstance(splitterClass,
                        confForThisUri);
                //Since we use no-arg constructor, need to inject
                //configuration and input URI.
                collectionSplitter.setConfiguration(confForThisUri);
                splitter = collectionSplitter;
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(format(
                            "Fetching collection stats on namespace: %s.%s; hosts: %s to choose splitter implementation.",
                            inputURI.getDatabase(), inputURI.getCollection(), inputURI.getHosts()));
                }
                //No class was specified, so choose one by looking at
                //collection stats.
                splitter = MongoSplitterFactory.getSplitterByStats(inputURI, confForThisUri);
            }
            splitters.add(splitter);
        }
    }

    //Now we have splitters for all the input collections.
    //Run through all of em, get all the splits for each,
    //compile them into one big ol' list.
    for (MongoSplitter splitter : splitters) {
        returnVal.addAll(splitter.calculateSplits());
    }
    return returnVal;
}

From source file:com.zjy.mongo.splitter.SingleMongoSplitter.java

License:Apache License

@Override
public List<InputSplit> calculateSplits() {
    MongoClientURI inputURI = MongoConfigUtil.getInputURI(getConfiguration());
    if (LOG.isDebugEnabled()) {
        LOG.debug(format("SingleMongoSplitter calculating splits for namespace: %s.%s; hosts: %s",
                inputURI.getDatabase(), inputURI.getCollection(), inputURI.getHosts()));
    }/*from  ww w  . j av a2 s .c om*/
    final List<InputSplit> splits = new ArrayList<InputSplit>();
    MongoInputSplit mongoSplit = new MongoInputSplit();

    mongoSplit.setInputURI(MongoConfigUtil.getInputURI(getConfiguration()));
    mongoSplit.setAuthURI(MongoConfigUtil.getAuthURI(getConfiguration()));
    mongoSplit.setQuery(MongoConfigUtil.getQuery(getConfiguration()));
    mongoSplit.setNoTimeout(MongoConfigUtil.isNoTimeout(getConfiguration()));
    mongoSplit.setFields(MongoConfigUtil.getFields(getConfiguration()));
    mongoSplit.setSort(MongoConfigUtil.getSort(getConfiguration()));
    mongoSplit.setKeyField(MongoConfigUtil.getInputKey(getConfiguration()));

    //Not using any index min/max bounds, so range query is
    //meaningless here - don't set it
    //mongoSplit.setUseRangeQuery(...)

    splits.add(mongoSplit);
    return splits;
}

From source file:com.zjy.mongo.splitter.StandaloneMongoSplitter.java

License:Apache License

@Override
public List<InputSplit> calculateSplits() throws SplitFailedException {
    final DBObject splitKey = MongoConfigUtil.getInputSplitKey(getConfiguration());
    final DBObject splitKeyMax = MongoConfigUtil.getMaxSplitKey(getConfiguration());
    final DBObject splitKeyMin = MongoConfigUtil.getMinSplitKey(getConfiguration());
    final int splitSize = MongoConfigUtil.getSplitSize(getConfiguration());
    final MongoClientURI inputURI;
    DBCollection inputCollection = null;
    final ArrayList<InputSplit> returnVal;
    try {//from  www . ja  v a2 s.  com
        inputURI = MongoConfigUtil.getInputURI(getConfiguration());
        MongoClientURI authURI = MongoConfigUtil.getAuthURI(getConfiguration());
        if (authURI != null) {
            inputCollection = MongoConfigUtil.getCollectionWithAuth(inputURI, authURI);
        } else {
            inputCollection = MongoConfigUtil.getCollection(inputURI);
        }

        returnVal = new ArrayList<InputSplit>();
        final String ns = inputCollection.getFullName();

        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Running splitVector on namespace: %s.%s; hosts: %s",
                    inputURI.getDatabase(), inputURI.getCollection(), inputURI.getHosts()));
        }
        final DBObject cmd = BasicDBObjectBuilder.start("splitVector", ns).add("keyPattern", splitKey)
                .add("min", splitKeyMin).add("max", splitKeyMax)
                // force:True is misbehaving it seems
                .add("force", false).add("maxChunkSize", splitSize).get();

        CommandResult data;
        boolean ok = true;
        try {
            data = inputCollection.getDB().getSisterDB(inputURI.getDatabase()).command(cmd,
                    ReadPreference.primary());
        } catch (final MongoException e) { // 2.0 servers throw exceptions rather than info in a CommandResult
            data = null;
            LOG.info(e.getMessage(), e);
            if (e.getMessage().contains("unrecognized command: splitVector")) {
                ok = false;
            } else {
                throw e;
            }
        }

        if (data != null) {
            if (data.containsField("$err")) {
                throw new SplitFailedException("Error calculating splits: " + data);
            } else if (!data.get("ok").equals(1.0)) {
                ok = false;
            }
        }

        if (!ok) {
            final CommandResult stats = inputCollection.getStats();
            if (stats.containsField("primary")) {
                final DBCursor shards = inputCollection.getDB().getSisterDB("config").getCollection("shards")
                        .find(new BasicDBObject("_id", stats.getString("primary")));
                try {
                    if (shards.hasNext()) {
                        final DBObject shard = shards.next();
                        final String host = ((String) shard.get("host")).replace(shard.get("_id") + "/", "");
                        final MongoClientURI shardHost;
                        if (authURI != null) {
                            shardHost = new MongoClientURIBuilder(authURI).host(host).build();
                        } else {
                            shardHost = new MongoClientURIBuilder(inputURI).host(host).build();
                        }
                        MongoClient shardClient = null;
                        try {
                            shardClient = new MongoClient(shardHost);
                            data = shardClient.getDB(shardHost.getDatabase()).command(cmd,
                                    ReadPreference.primary());
                        } catch (final Exception e) {
                            LOG.error(e.getMessage(), e);
                        } finally {
                            if (shardClient != null) {
                                shardClient.close();
                            }
                        }
                    }
                } finally {
                    shards.close();
                }
            }
            if (data != null && !data.get("ok").equals(1.0)) {
                throw new SplitFailedException("Unable to calculate input splits: " + data.get("errmsg"));
            }

        }

        // Comes in a format where "min" and "max" are implicit
        // and each entry is just a boundary key; not ranged
        final BasicDBList splitData = (BasicDBList) data.get("splitKeys");

        if (splitData.size() == 0) {
            LOG.warn(
                    "WARNING: No Input Splits were calculated by the split code. Proceeding with a *single* split. Data may be too"
                            + " small, try lowering 'mongo.input.split_size' if this is undesirable.");
        }

        BasicDBObject lastKey = null; // Lower boundary of the first min split

        // If splitKeyMin was given, use it as first boundary.
        if (!splitKeyMin.toMap().isEmpty()) {
            lastKey = new BasicDBObject(splitKeyMin.toMap());
        }
        for (final Object aSplitData : splitData) {
            final BasicDBObject currentKey = (BasicDBObject) aSplitData;
            returnVal.add(createSplitFromBounds(lastKey, currentKey));
            lastKey = currentKey;
        }

        BasicDBObject maxKey = null;
        // If splitKeyMax was given, use it as last boundary.
        if (!splitKeyMax.toMap().isEmpty()) {
            maxKey = new BasicDBObject(splitKeyMax.toMap());
        }
        // Last max split
        final MongoInputSplit lastSplit = createSplitFromBounds(lastKey, maxKey);
        returnVal.add(lastSplit);
    } finally {
        if (inputCollection != null) {
            MongoConfigUtil.close(inputCollection.getDB().getMongo());
        }
    }

    return returnVal;
}

From source file:de.adorsys.oauth.tokenstore.mongodb.MongoDbProvider.java

License:Apache License

@SuppressWarnings("ReplaceAllDot")
private String createLogUri(MongoClientURI clientURI) {
    StringBuilder sb = new StringBuilder();
    String password = clientURI.getPassword() == null ? null
            : new String(clientURI.getPassword()).replaceAll(".", "x");
    String username = clientURI.getUsername();
    for (String host : clientURI.getHosts()) {
        sb.append("mongodb://");
        if (username != null) {
            sb.append(username);//from w w  w  . j a  v  a  2 s  .c  o  m
            if (password != null) {
                sb.append(':').append(password);
            }
            sb.append('@');
        }
        sb.append(host).append(" ");
    }

    return sb.toString();
}

From source file:io.opentracing.contrib.mongo.TracingMongoClient.java

License:Apache License

public TracingMongoClient(Tracer tracer, final MongoClientURI uri,
        final MongoDriverInformation mongoDriverInformation) {
    this(tracer, toServerAddressList(uri.getHosts()),
            uri.getCredentials() != null ? Collections.singletonList(uri.getCredentials())
                    : Collections.<MongoCredential>emptyList(),
            uri.getOptions(), mongoDriverInformation);
}

From source file:localdomain.localhost.MongoDbTroubleshooter.java

License:Open Source License

public void run() throws Exception {
    try (PrintWriter writer = new PrintWriter(System.out)) {

        MongoClientURI mongoClientUri = new MongoClientURI(uri);
        writer.println("" + "host=" + mongoClientUri.getHosts() + ",username=" + mongoClientUri.getUsername()
                + ",database=" + mongoClientUri.getDatabase() + ",collection=" + mongoClientUri.getCollection()
                + "");

        writer.println();//  w  w w .j a v a2 s . co m
        writer.println();

        writer.println("# MongoClient");
        writer.println();

        MongoClient mongoClient = new MongoClient(mongoClientUri);
        writer.println("" + mongoClient + "");

        writer.println();
        writer.println();
        writer.println("# Databases");
        writer.println();

        try {
            List<String> databaseNames = mongoClient.getDatabaseNames();
            for (String databaseName : databaseNames) {
                writer.println("* " + databaseName
                        + (databaseName.equals(mongoClientUri.getDatabase()) ? " - default database" : ""));
            }
        } catch (Exception e) {
            writer.println("Could not list the databases of the MongoDB instance: '" + e.getMessage() + "'");

        }

        writer.println();
        writer.println();
        writer.println("# Database");
        writer.println();

        DB db = mongoClient.getDB(mongoClientUri.getDatabase());
        writer.println("DB: " + db.getName() + "");

        writer.println();
        writer.println();
        writer.println("## Collections");
        writer.println();
        Set<String> myCollections = db.getCollectionNames();
        if (myCollections.isEmpty()) {
            writer.println("NO COLLECTIONS!");

        } else {

            for (String collection : myCollections) {
                DBCollection dbCollection = db.getCollection(collection);
                writer.println("* " + dbCollection.getName() + " - " + dbCollection.getCount() + " entries");
            }
        }
    }
}