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:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java

License:Apache License

public MongoStorageService(String host, int port, String dbName) {
    try {/*from   w w w  . j a v a 2  s.  co  m*/
        client = new MongoClient(host, port);
    } catch (UnknownHostException | MongoException e) {
        e.printStackTrace();
    }
    if (client != null) {
        this.options = new MongoTemplate(client, dbName);
    }
}

From source file:cn.weibo.webcollector.spider.WeiboCrawler.java

License:Open Source License

@Override
public void visit(Page page, CrawlDatums next) {
    String inlink = page.meta("inlink");
    String title = page.select("title").text();
    String url = page.getUrl();/*  ww w . jav  a  2s .co  m*/
    /*??*/
    Elements weibos = page.select("div[id].c");
    try {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
        mongoLogger.setLevel(Level.ERROR);

        // ?
        MongoDatabase mongoDatabase = mongoClient.getDatabase("weibo_crawler");
        System.out.println("Connect to database successfully");
        MongoCollection<Document> collection = mongoDatabase.getCollection("weibo_page");
        //?  
        /** 
        * 1.  org.bson.Document ?key-value? 
        * 2. ?List<Document> 
        * 3. ???? mongoCollection.insertMany(List<Document>) ??? mongoCollection.insertOne(Document) 
        * */
        for (Element weibo : weibos) {
            if (weibo.text().length() != 0) {
                Document document = new Document("url", url).append("title", title)
                        .append("content", weibo.text()).append("inlink", inlink);
                List<Document> documents = new ArrayList<Document>();
                documents.add(document);
                collection.insertMany(documents);
            }
        }
        System.out.println("??");
        mongoClient.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:cn.weibo.webcollector.spider.WeiboCrawler.java

License:Open Source License

public static void main(String[] args) throws Exception {
    MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
    Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
    mongoLogger.setLevel(Level.ERROR);
    // ?//ww  w. j  a  v  a 2s  . c  o m
    // DBCollection dbCollection =
    // mongoClient.getDB("maoyan_crawler").getCollection("rankings_am");
    DB db = mongoClient.getDB("weibo_crawler");
    // ?????
    Set<String> colls = db.getCollectionNames();
    for (String s : colls) {
        // Collection(?"")
        if (s.equals("weibo_page")) {
            db.getCollection(s).drop();
        }
    }
    WeiboCrawler crawler = new WeiboCrawler("weibo_crawler", true);
    crawler.setThreads(3);
    /*???10?*/
    for (int i = 1; i <= 10; i++) {
        crawler.addSeed(new CrawlDatum("http://weibo.cn/entpaparazzi?vt=4&page=" + i) //?
                .meta("inlink", "seed").meta("depth", "1"));
    }
    for (int i = 1; i <= 10; i++) {
        crawler.addSeed(new CrawlDatum("http://weibo.cn/dianyingpiaofangba?vt=4&page=" + i) //?
                .meta("inlink", "seed").meta("depth", "1"));
    }
    for (int i = 1; i <= 10; i++) {
        crawler.addSeed(new CrawlDatum("http://weibo.cn/houson100037?vt=4&page=" + i) //Houson
                .meta("inlink", "seed").meta("depth", "1"));
    }
    for (int i = 1; i <= 10; i++) {
        crawler.addSeed(new CrawlDatum("http://weibo.cn/kaopuyingping?vt=4&page=" + i) //?
                .meta("inlink", "seed").meta("depth", "1"));
    }
    for (int i = 1; i <= 10; i++) {
        crawler.addSeed(new CrawlDatum("http://weibo.cn/rottentomato?vt=4&page=" + i) //
                .meta("inlink", "seed").meta("depth", "1"));
    }
    for (int i = 1; i <= 10; i++) {
        crawler.addSeed(new CrawlDatum("http://weibo.cn/cfcu?vt=4&page=" + i) //?
                .meta("inlink", "seed").meta("depth", "1"));
    }
    for (int i = 1; i <= 10; i++) {
        crawler.addSeed(new CrawlDatum("http://weibo.cn/moviefactory?vt=4&page=" + i) //
                .meta("inlink", "seed").meta("depth", "1"));
    }
    for (int i = 1; i <= 10; i++) {
        crawler.addSeed(new CrawlDatum("http://weibo.cn/wodianying?vt=4&page=" + i) //Mr
                .meta("inlink", "seed").meta("depth", "1"));
    }
    for (int i = 1; i <= 10; i++) {
        crawler.addSeed(new CrawlDatum("http://weibo.cn/movietheworld?vt=4&page=" + i) //?
                .meta("inlink", "seed").meta("depth", "1"));
    }
    for (int i = 1; i <= 10; i++) {
        crawler.addSeed(new CrawlDatum("http://weibo.cn/badmovie?vt=4&page=" + i) //
                .meta("inlink", "seed").meta("depth", "1"));
    }
    crawler.addRegex("-.*\\.(jpg|png|gif).*");
    crawler.addRegex("-.*top.*");
    crawler.addRegex("http://weibo.cn/badmovie.*");
    crawler.addRegex("http://weibo.cn/movietheworld.*");
    crawler.addRegex("http://weibo.cn/wodianying.*");
    crawler.addRegex("http://weibo.cn/moviefactory.*");
    crawler.addRegex("http://weibo.cn/cfcu.*");
    crawler.addRegex("http://weibo.cn/rottentomato.*");
    crawler.addRegex("http://weibo.cn/kaopuyingping.*");
    crawler.addRegex("http://weibo.cn/houson100037.*");
    crawler.addRegex("http://weibo.cn/dianyingpiaofangba.*");
    crawler.addRegex("http://weibo.cn/entpaparazzi.*");
    crawler.addRegex("http://weibo.cn/comment/.*");
    crawler.start(1);
}

From source file:co.edu.uniandes.bigdata.ProyectoBigData.logica.TwitterColombiaLogica.java

public TwitterColombiaLogica() {

    // Mongo connection
    mongoDB = new MongoClient(DB_SERVER, DB_PORT).getDatabase(DB_NAME);
    if (mongoDB.getCollection(COLLECTION_NAME) == null) {
        mongoDB.createCollection(COLLECTION_NAME);
    }//  www. j a va2 s  . co  m

}

From source file:co.edu.uniandes.csw.Arquidalgos.usuario.persistence.MongoConfig.java

public MongoConfig() {
    try {/*from w  w  w.j a  va2  s .  c  o  m*/

        System.out.println("MONGO DB");
        mongoClient = new MongoClient("172.24.98.137", 27017);
    } catch (UnknownHostException ex) {
        Logger.getLogger(MongoConfig.class.getName()).log(Level.SEVERE, null, ex);
    }
    db = mongoClient.getDB("test");
    //    

    //        MongoClientURI uri = new MongoClientURI("mongodb://admin:admin@localhost:27017/tumejoropcionusuario");
    //    mongoClient = new MongoClient(uri); 
    //        } catch (UnknownHostException ex) { 
    //    Logger.getLogger(MongoConfig.class.getName()).log(Level.SEVERE, null, ex); 
    //    } 
    //    db = mongoClient.getDB("test"); 
}

From source file:co.mcme.statistician.database.MongoDBUtil.java

License:Open Source License

public MongoDBUtil(String hostname, int port, String username, char[] password, String database,
        boolean tryAuth, String collection) throws UnknownHostException {
    if (tryAuth) {
        this.client = new MongoClient(Arrays.asList(new ServerAddress(hostname, port)),
                Arrays.asList(MongoCredential.createPlainCredential(username, database, password)));
    } else {/*from   w w  w. j av a2  s .c om*/
        this.client = new MongoClient(Arrays.asList(new ServerAddress(hostname, port)));
    }
    this.database = client.getDB(database);
    if (client != null) {
        if (!this.database.collectionExists(collection)) {
            statsCollection = this.database.createCollection(collection, new BasicDBObject());
        } else {
            statsCollection = this.database.getCollection(collection);
        }
    } else {
        StatisticianLogger.severe(
                "Could not authenticate to '" + hostname + ":" + port + "/" + database + "' disabling plugin.");
        Statistician.getServerInstance().getPluginManager().disablePlugin(Statistician.getPluginInstance());
    }
}

From source file:co.mcme.themedbuilds.database.MongoDBUtil.java

License:Open Source License

public MongoDBUtil(String hostname, int port, String username, char[] password, String database)
        throws UnknownHostException {
    client = new MongoClient(hostname, port);
    this.database = client.getDB(database);
    boolean auth = this.database.authenticate(username, password);
    if (!auth) {//from w w w  .ja  v a  2  s. c  o m
        ThemedLogger.severe(
                "Could not authenticate to '" + hostname + ":" + port + "/" + database + "' disabling plugin.");
        ThemedBuildPlugin.getServerInstance().getPluginManager()
                .disablePlugin(ThemedBuildPlugin.getPluginInstance());
    }
    loadCollections();
}

From source file:co.upet.extensions.glacierbkuploader.metadata.BackupMetadataStore.java

@Inject
public BackupMetadataStore(Configuration conf) {
    try {/*from w  ww .  ja v  a2 s . c o m*/
        mongoClient = new MongoClient(conf.mongoHost(), conf.mongoPort());
        morphia.map(BackupMetadata.class);
        dataStore = morphia.createDatastore(mongoClient, conf.dataStore());
        dataStore.ensureIndexes();
    } catch (UnknownHostException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.abuabdul.mytravelpal.config.MyTravelPalMongoConfig.java

License:Apache License

@Bean
public MongoClient mongoClient() throws Exception {
    return new MongoClient(mongohost, mongoport);
}

From source file:com.ait.tooling.server.mongodb.MongoDB.java

License:Open Source License

public MongoDB(final List<ServerAddress> addr, final List<MongoCredential> auth, final MongoClientOptions opts,
        final boolean repl, final String usedb, final boolean useid, final Map<String, IMongoDBOptions> dbops) {
    m_useid = useid;//from  ww w .  jav  a2 s.c  o m

    m_dbops = Objects.requireNonNull(dbops);

    m_usedb = StringOps.requireTrimOrNull(usedb);

    BSON.addEncodingHook(BigDecimal.class, new Transformer() {
        @Override
        public Object transform(final Object object) {
            if (null == object) {
                return null;
            }
            return JSONUtils.asDouble(object);
        }
    });
    BSON.addEncodingHook(BigInteger.class, new Transformer() {
        @Override
        public Object transform(Object object) {
            if (null == object) {
                return null;
            }
            Long lval = JSONUtils.asLong(object);

            if (null != lval) {
                return lval;
            }
            return JSONUtils.asInteger(object);
        }
    });
    if (addr.isEmpty()) {
        throw new IllegalArgumentException("no ServerAddress");
    }
    if ((addr.size() == 1) && (false == repl)) {
        final ServerAddress main = addr.get(0);

        if (null == main) {
            throw new IllegalArgumentException("null ServerAddress");
        }
        if ((null == auth) || (auth.isEmpty())) {
            m_mongo = new MongoClient(main, Objects.requireNonNull(opts));
        } else {
            m_mongo = new MongoClient(main, auth, Objects.requireNonNull(opts));
        }
    } else {
        if ((null == auth) || (auth.isEmpty())) {
            m_mongo = new MongoClient(addr, Objects.requireNonNull(opts));
        } else {
            m_mongo = new MongoClient(addr, auth, Objects.requireNonNull(opts));
        }
    }
}