Example usage for com.mongodb ServerAddress ServerAddress

List of usage examples for com.mongodb ServerAddress ServerAddress

Introduction

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

Prototype

public ServerAddress(final InetSocketAddress inetSocketAddress) 

Source Link

Document

Creates a ServerAddress

Usage

From source file:org.gennai.gungnir.metastore.MongoDbMetaStore.java

License:Apache License

@Override
public void open() throws MetaStoreException {
    GungnirConfig config = GungnirManager.getManager().getConfig();
    List<String> servers = config.getList(METASTORE_MONGODB_SERVERS);
    List<ServerAddress> addresses = Lists.newArrayListWithCapacity(servers.size());
    for (String server : servers) {
        addresses.add(new ServerAddress(server));
    }//  w w w  . j  a va2 s  .  c  o  m
    mongoClient = new MongoClient(addresses);
    metaStoreDB = mongoClient.getDatabase(META_STORE_DB);
    userAccountCollection = metaStoreDB.getCollection(USER_ACCOUNT_COLLECTION);
    schemaCollection = metaStoreDB.getCollection(SCHEMA_COLLECTION);
    topologyCollection = metaStoreDB.getCollection(TOPOLOGY_COLLECTION);
    trackingCollection = metaStoreDB.getCollection(TRACKING_COLLECTION);

    trackingMap = Maps.newConcurrentMap();
}

From source file:org.gennai.gungnir.topology.processor.MongoFetchProcessor.java

License:Apache License

@Override
public void open(GungnirConfig config, GungnirContext context) throws ProcessorException {
    dbName = context.replaceVariable(dbName);
    collectionName = context.replaceVariable(collectionName);
    query = parseQueryString(queryString);
    fetchFields = new Document();
    for (String fieldName : fetchFieldNames) {
        fetchFields.append(fieldName, 1);
    }/*from  www  .jav  a2s. c om*/
    if (sortString != null) {
        sort = parseSortString(sortString);
    }
    if (expire != null) {
        expireSecs = expire.toSeconds();
    }

    List<String> servers = config.getList(FETCH_SERVERS);
    List<ServerAddress> addresses = Lists.newArrayListWithCapacity(servers.size());
    for (String server : servers) {
        addresses.add(new ServerAddress(server));
    }
    mongoClient = new MongoClient(addresses);
    MongoDatabase db = mongoClient.getDatabase(dbName);
    collection = db.getCollection(collectionName);

    if (expireSecs > 0) {
        cache = CacheBuilder.newBuilder().maximumSize(config.getInteger(CACHE_SIZE))
                .expireAfterWrite(expireSecs, TimeUnit.SECONDS).build();
    }

    LOG.info("MongoFetchProcessor opened({})", this);
}

From source file:org.gennai.gungnir.topology.processor.MongoPersistProcessor.java

License:Apache License

@Override
public void open(GungnirConfig config, GungnirContext context, OperatorContext operatorContext,
        Map<String, List<String>> outputFieldNames) throws ProcessorException {
    dbName = context.replaceVariable(dbName);
    collectionName = context.replaceVariable(collectionName);
    this.outputFieldNames = outputFieldNames;

    if (keyFieldNames != null) {
        keyFieldsIndexes = Maps.newHashMap();
        for (Map.Entry<String, List<String>> entry : outputFieldNames.entrySet()) {
            int[] index = new int[keyFieldNames.length];
            Arrays.fill(index, -1);
            for (int i = 0; i < keyFieldNames.length; i++) {
                for (int j = 0; j < entry.getValue().size(); j++) {
                    if (entry.getValue().get(j).equals(keyFieldNames[i])) {
                        index[i] = j;/*from ww  w  .  j a  v a 2  s.c o m*/
                        break;
                    }
                }
                if (index[i] < 0) {
                    throw new ProcessorException("Can't found key field '" + keyFieldNames[i] + "'");
                }
            }
            keyFieldsIndexes.put(entry.getKey(), index);
        }
    }

    List<String> servers = config.getList(MONGO_PERSIST_SERVERS);
    List<ServerAddress> addresses = Lists.newArrayListWithCapacity(servers.size());
    for (String server : servers) {
        addresses.add(new ServerAddress(server));
    }
    mongoClient = new MongoClient(addresses);
    MongoDatabase db = mongoClient.getDatabase(dbName);
    collection = db.getCollection(collectionName);

    if (autoIndexing && keyFieldNames != null) {
        Document doc = new Document();
        for (String keyFieldName : keyFieldNames) {
            doc.append(keyFieldName, 1);
        }

        collection.createIndex(doc, new IndexOptions().unique(true));
    }

    LOG.info("MongoPersistProcessor opened({})", this);
}

From source file:org.metaservice.core.utils.MongoCache.java

License:Apache License

public MongoCache(@NotNull String address, @NotNull String database, @NotNull String username,
        @NotNull String password) {
    try {//from   w w  w .j  a v a 2s  . c o m
        MongoCredential credential = MongoCredential.createMongoCRCredential(username, database,
                password.toCharArray());
        client = new MongoClient(new ServerAddress(address), Arrays.asList(credential));
        db = client.getDB(database);
        db.authenticate(username, password.toCharArray());
        collection = db.getCollection("debiansnapshot");
        fileCollection = db.getCollection("files");
        collection.ensureIndex("uri");
        fileCollection.ensureIndex("md5");
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.metaservice.kryo.mongo.MongoConnectionWrapper.java

License:Apache License

public MongoConnectionWrapper(String address, String database, String username, String password) {
    try {//w w w .ja va 2  s  . c o m
        MongoCredential credential = MongoCredential.createMongoCRCredential(username, database,
                password.toCharArray());
        client = new MongoClient(new ServerAddress(address), Arrays.asList(credential));
        DB db = client.getDB(database);

        db.authenticate(username, password.toCharArray());
        ObjectMapper objectMapper = MongoJackModule.configure(new ObjectMapper());
        objectMapper.addMixInAnnotations(URI.class, UriMixin.class);

        queueCollection = JacksonDBCollection.wrap(db.getCollection("queues"), QueueConfig.class, Long.class,
                objectMapper);
        postProcessorMessageCollection = JacksonDBCollection.wrap(db.getCollection("postProcessor"),
                PostProcessorMessage.class, Long.class, objectMapper);
        providerCreateMessageCollection = JacksonDBCollection.wrap(db.getCollection("create"),
                ProviderCreateMessage.class, Long.class, objectMapper);
        providerRefreshMessageCollection = JacksonDBCollection.wrap(db.getCollection("refresh"),
                ProviderRefreshMessage.class, Long.class, objectMapper);
        postProcessorMessageCollectionFailed = JacksonDBCollection.wrap(db.getCollection("postProcessorFailed"),
                ResponseMessage.class, Long.class, objectMapper);
        providerCreateMessageCollectionFailed = JacksonDBCollection.wrap(db.getCollection("createFailed"),
                ResponseMessage.class, Long.class, objectMapper);
        providerRefreshMessageCollectionFailed = JacksonDBCollection.wrap(db.getCollection("refreshFailed"),
                ResponseMessage.class, Long.class, objectMapper);
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.metaservice.Md5Runner.java

License:Apache License

public static void main(String[] args) {
    try {/*from  w  w  w  . j  av  a  2  s  . com*/
        MongoCredential credential = MongoCredential.createMongoCRCredential("nilo", "crawlcache",
                "alokin".toCharArray());
        client = new MongoClient(new ServerAddress("metaservice.org"), Arrays.asList(credential));
        db = client.getDB("crawlcache");
        db.authenticate("nilo", "alokin".toCharArray());
        collection = db.getCollection("debiansnapshot");
        fileCollection = db.getCollection("files");
        collection.ensureIndex("uri");
        fileCollection.ensureIndex("md5");

        DBObject query = new BasicDBObject();
        query.put("md5", new BasicDBObject("$exists", true));
        query.put("content", new BasicDBObject("$type", 5));
        BasicDBObject group = new BasicDBObject();
        group.put("_id", "$md5");
        group.put("total", new BasicDBObject("$sum", 1));

        System.err.println(
                fileCollection.aggregate(new BasicDBObject("$match", query), new BasicDBObject("$group", group),
                        new BasicDBObject("$sort", new BasicDBObject("total", -1))));

        /*
        while(true){
                
        DBObject query = new BasicDBObject();
        query.put("md5",new BasicDBObject("$exists",false));
        query.put("content",new BasicDBObject("$type",5));
                
        DBObject object = collection.findOne(query);
        if(object == null)
        break;
                
                
        System.err.println(object.get("uri"));
                
        byte[] x = (byte[]) object.get("content");
                
        object.put("md5",DigestUtils.md5Hex(x));
        collection.save(object,WriteConcern.SAFE);
        }
                
                
        while (true){
        DBObject query = new BasicDBObject();
        query.put("md5",new BasicDBObject("$exists",true));
        query.put("content",new BasicDBObject("$type",5));
        DBObject object = collection.findOne(query);
                
                
        String md5  = (String) object.get("md5");
        object.removeField("md5");
        collection.save(object,WriteConcern.SAFE);
        ObjectId id = (ObjectId) object.get("_id");
                
        object.put("md5",md5);
        object.removeField("_id");
        fileCollection.insert(object,WriteConcern.SAFE);
        System.err.println("INSERTED " + object.get("_id"));
        query = new BasicDBObject();
        query.put("md5",md5);
                
        // update self
        DBObject x  =collection.findOne(id);
        x.put("contentRef",object.get("_id"));
        x.removeField("content");
        collection.save(x,WriteConcern.SAFE);
                
        // updatePackage others
        DBObject updatePackage = new BasicDBObject();
        updatePackage.put("$set",new BasicDBObject("contentRef",object.get("_id")));
        updatePackage.put("$unset",new BasicDBObject("content",1));
        WriteResult result = collection.updatePackage(query,updatePackage);
        System.err.println("MD5 " + md5 + " existed  " + result.getN() + " times");
                
        }
        */
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }

}

From source file:org.nuxeo.directory.mongodb.MongoDBConnectionHelper.java

License:Apache License

/**
 * Initialize a connection to the MongoDB server
 *
 * @param server the server url//from   w ww  .j a va 2  s.  c o  m
 * @return the MongoDB client
 */
public static MongoClient newMongoClient(String server) {
    if (StringUtils.isBlank(server)) {
        throw new NuxeoException("Missing <server> in MongoDB repository descriptor");
    }
    MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder()
            // Can help to prevent firewall disconnects
            // inactive connection, option not available from URI
            .socketKeepAlive(true)
            // don't wait for ever by default,
            // can be overridden using URI options
            .connectTimeout(MONGODB_OPTION_CONNECTION_TIMEOUT_MS)
            .socketTimeout(MONGODB_OPTION_SOCKET_TIMEOUT_MS).description("Nuxeo");
    MongoClient client;
    if (server.startsWith("mongodb://")) {
        // allow mongodb:// URI syntax for the server, to pass everything in one string
        client = new MongoClient(new MongoClientURI(server, optionsBuilder));
    } else {
        client = new MongoClient(new ServerAddress(server), optionsBuilder.build());
    }
    if (log.isDebugEnabled()) {
        log.debug("MongoClient initialized with options: " + client.getMongoClientOptions().toString());
    }
    return client;
}

From source file:org.nuxeo.ecm.core.storage.mongodb.GridFSBinaryManager.java

License:Apache License

@Override
public void initialize(String blobProviderId, Map<String, String> properties) throws IOException {
    super.initialize(blobProviderId, properties);
    this.properties = properties;
    String server = properties.get(SERVER_PROPERTY);
    if (StringUtils.isBlank(server)) {
        throw new NuxeoException(
                "Missing server property in GridFS Binary Manager descriptor: " + blobProviderId);
    }//from w  w w.  j  a  va  2s .c  o m
    String dbname = properties.get(DBNAME_PROPERTY);
    if (StringUtils.isBlank(dbname)) {
        throw new NuxeoException(
                "Missing dbname property in GridFS Binary Manager descriptor: " + blobProviderId);
    }
    String bucket = properties.get(BUCKET_PROPERTY);
    if (StringUtils.isBlank(bucket)) {
        bucket = blobProviderId + ".fs";
    }
    if (server.startsWith("mongodb://")) {
        client = new MongoClient(new MongoClientURI(server));
    } else {
        client = new MongoClient(new ServerAddress(server));
    }
    gridFS = new GridFS(client.getDB(dbname), bucket);
    garbageCollector = new GridFSBinaryGarbageCollector();
}

From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBChecker.java

License:Apache License

@Override
public void check(ConfigurationGenerator cg) throws ConfigurationException {
    MongoClient ret = null;//from w  w  w.j a v  a2 s. com
    String serverName = cg.getUserConfig().getProperty(ConfigurationGenerator.PARAM_MONGODB_SERVER);
    String dbName = cg.getUserConfig().getProperty(ConfigurationGenerator.PARAM_MONGODB_NAME);

    MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder()
            .serverSelectionTimeout((int) TimeUnit.SECONDS.toMillis(1)).description("Nuxeo DB Check");
    if (serverName.startsWith("mongodb://")) {
        // allow mongodb:// URI syntax for the server, to pass everything in one string
        ret = new MongoClient(new MongoClientURI(serverName, optionsBuilder));
    } else {
        ret = new MongoClient(new ServerAddress(serverName), optionsBuilder.build());
    }
    try {
        Document ping = new Document("ping", "1");
        ret.getDatabase(dbName).runCommand(ping);
    } catch (MongoTimeoutException e) {
        throw new ConfigurationException(
                String.format("Unable to connect to MongoDB at %s, please check your connection", serverName));
    } finally {
        ret.close();
    }
}

From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBRepository.java

License:Apache License

public static MongoClient newMongoClient(MongoDBRepositoryDescriptor descriptor) throws UnknownHostException {
    MongoClient ret;/*  www.ja  v a  2 s. c  om*/
    String server = descriptor.server;
    if (StringUtils.isBlank(server)) {
        throw new NuxeoException("Missing <server> in MongoDB repository descriptor");
    }
    MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder()
            // Can help to prevent firewall disconnects inactive connection, option not available from URI
            .socketKeepAlive(true)
            // don't wait for ever by default, can be overridden using URI options
            .connectTimeout(MONGODB_OPTION_CONNECTION_TIMEOUT_MS)
            .socketTimeout(MONGODB_OPTION_SOCKET_TIMEOUT_MS).description("Nuxeo");
    if (server.startsWith("mongodb://")) {
        // allow mongodb:// URI syntax for the server, to pass everything in one string
        ret = new MongoClient(new MongoClientURI(server, optionsBuilder));
    } else {
        ret = new MongoClient(new ServerAddress(server), optionsBuilder.build());
    }
    if (log.isDebugEnabled()) {
        log.debug("MongoClient initialized with options: " + ret.getMongoClientOptions().toString());
    }
    return ret;
}