Example usage for com.mongodb MongoClient getDB

List of usage examples for com.mongodb MongoClient getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:com.khubla.cbean.mongokv.MongoKVService.java

License:Open Source License

@Override
public byte[] load(CBeanKey cBeanKey) throws KVServiceException {
    MongoClient mongoClient = null;
    try {//from   ww w .j  a va  2 s . c  o  m
        /*
         * get a pooled mongo connection
         */
        mongoClient = mongoClientPool.borrowObject();
        final DB db = mongoClient.getDB(cBeanUrl.getClusterName());
        new GridFS(db, "");
        // List<> gfs.find(url);
        // final Location location = new Location(new Namespace(clusterName), url);
        // final FetchValue fv = new FetchValue.Builder(location).build();
        // final FetchValue.Response response = riakClient.execute(fv);
        /*
         * response
         */
        // if (null != response) {
        // final RiakObject riakObject = response.getValue(RiakObject.class);
        // if (null != riakObject) {
        // final BinaryValue binaryValue = riakObject.getValue();
        // if (null != binaryValue) {
        // logger.info("Loaded asset '" + url + "'");
        // return binaryValue.getValue();
        // }
        // }
        // }
        logger.info("Unable to load asset '" + cBeanKey.getKey() + "'");
        return null;
    } catch (final Exception e) {
        throw new KVServiceException(e);
    } finally {
        try {
            if (null != mongoClient) {
                mongoClientPool.returnObject(mongoClient);
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.korpacz.testproject.HelloWorldMongoDBSparkFreemarkerStyle.java

public static void main(String... args) throws UnknownHostException {
    //ssssss/* www  . j  av  a2  s . c o  m*/

    final Configuration config = new Configuration();
    config.setClassForTemplateLoading(HelloWorldSparkFreemarkerStyle.class, "/");

    MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));
    DB database = client.getDB("test");
    final DBCollection collection = database.getCollection("names");

    Spark.get("/", new Route() {

        @Override
        public Object handle(Request rqst, Response rspns) throws Exception {

            Template helloTemplete = config.getTemplate("hello.ftl");
            StringWriter writter = new StringWriter();

            DBObject object = collection.findOne();

            helloTemplete.process(object, writter);

            System.out.println(writter);

            return writter;
        }
    });

}

From source file:com.linuxbox.enkive.statistics.gathering.mongodb.MongoStatsFileAttachmentsGatherer.java

License:Open Source License

public MongoStatsFileAttachmentsGatherer(MongoClient m, String dbName, String attachmentsColl,
        String gathererName, String humanName, List<String> keys) throws GathererException {
    this(gathererName, humanName, keys, m.getDB(dbName).getCollection(attachmentsColl));
}

From source file:com.linuxbox.enkive.statistics.gathering.mongodb.MongoStatsGridAttachmentsGatherer.java

License:Open Source License

public MongoStatsGridAttachmentsGatherer(MongoClient m, String dbName, String attachmentsColl,
        String gathererName, String humanName, List<String> keys) throws GathererException {
    this(gathererName, humanName, keys,
            m.getDB(dbName).getCollection(attachmentsColl + GRID_FS_FILES_COLLECTION_SUFFIX));
}

From source file:com.linuxbox.enkive.workspace.searchQuery.mongo.MongoSearchQueryBuilder.java

License:Open Source License

public MongoSearchQueryBuilder(MongoClient m, String searchQueryDBName, String searchQueryCollName) {
    this(m.getDB(searchQueryDBName).getCollection(searchQueryCollName));
}

From source file:com.linuxbox.enkive.workspace.searchResult.mongo.MongoSearchResultBuilder.java

License:Open Source License

public MongoSearchResultBuilder(MongoClient m, String searchResultsDBName, String searchResultsCollName) {
    this(m.getDB(searchResultsDBName).getCollection(searchResultsCollName));
}

From source file:com.linuxbox.enkive.workspace.searchResult.mongo.MongoSearchResultUtils.java

License:Open Source License

public MongoSearchResultUtils(MongoClient m, String messageDB, String messageCollName,
        String searchResultCollName) {
    DB messageDb = m.getDB(messageDB);
    this.messageColl = messageDb.getCollection(messageCollName);
    this.searchResultColl = messageDb.getCollection(searchResultCollName);
}

From source file:com.malsolo.mongodb.humongous.driver.Main.java

public static void main(String... args) throws UnknownHostException {

    MongoClient mongoClient = new MongoClient();
    DB db = mongoClient.getDB("news");

    Set<String> colls = db.getCollectionNames();

    for (String s : colls) {
        System.out.println(s);//from w  ww. j  a  va 2  s .c  om
    }

    DBCollection coll = db.getCollection("article");

    BasicDBObject doc = new BasicDBObject("authorId", UUID.randomUUID()).append("author", "Driver")
            .append("date", new Date()).append("title", "Title");
    coll.insert(doc);

    DBObject myDoc = coll.findOne();
    System.out.println(myDoc);

    System.out.println(coll.getCount());

    try (DBCursor cursor = coll.find()) {
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    }

}

From source file:com.maoyan.pf.webcollector.spider.AttendrateCrawler.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Executor executor = new Executor() {
        @Override//w w w . ja v  a2  s  . co  m
        public void execute(CrawlDatum datum, CrawlDatums next) throws Exception {
            MongoClient mongoClient = new MongoClient("localhost", 27017);
            // ?
            // DBCollection dbCollection = mongoClient.getDB("maoyan_crawler").getCollection("rankings_am"); 
            DB db = mongoClient.getDB("maoyan_crawler");
            // ?????
            Set<String> colls = db.getCollectionNames();
            for (String s : colls) {
                // Collection(?"")
                if (s.equals("attend_rate")) {
                    db.getCollection(s).drop();
                }
            }
            DBCollection dbCollection = db.getCollection("attend_rate");
            HtmlUnitDriver driver = new HtmlUnitDriver();
            driver.setJavascriptEnabled(false);
            driver.get(datum.getUrl());
            //                System.out.println(driver.getPageSource());
            WebElement element = driver.findElementByCssSelector("div#seat_table");
            List<WebElement> movie_name = element.findElements(By.className("c1 lineDot"));
            List<WebElement> boxoffice_rate = element.findElements(By.className("c2 red"));
            List<WebElement> visit_pershow = element.findElements(By.className("c3 gray"));
            WebElement cityarea = driver.findElementByCssSelector("span[class='today']");
            System.out.println(cityarea.getText());
            for (int i = 0; i < movie_name.size(); i++) {
                System.out.println(movie_name.get(i).getText());
                System.out.println(boxoffice_rate.get(i).getText());
                System.out.println(visit_pershow.get(i).getText());
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", cityarea.getText()).append("movie_name", movie_name.get(i).getText())
                        .append("boxoffice_rate", boxoffice_rate.get(i).getText())
                        .append("visit_pershow", visit_pershow.get(i).getText());
                dbCollection.insert(dbObject);
            }
            mongoClient.close();
        }
    };

    //DBDBManager
    DBManager manager = new BerkeleyDBManager("maoyan");
    //Crawler?DBManagerExecutor
    Crawler crawler = new Crawler(manager, executor);
    crawler.addSeed("http://pf.maoyan.com/attend/rate");
    crawler.start(1);
}

From source file:com.maoyan.pf.webcollector.spider.MoviestoreCrawler.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Executor executor = new Executor() {
        @Override/* w  ww .  j ava  2 s. c  o  m*/
        public void execute(CrawlDatum datum, CrawlDatums next) throws Exception {
            MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
            Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
            mongoLogger.setLevel(Level.ERROR);
            // ?
            // DBCollection dbCollection =
            // mongoClient.getDB("maoyan_crawler").getCollection("rankings_am");
            DB db = mongoClient.getDB("maoyan_crawler");
            // ?????
            DBCollection dbCollection = db.getCollection("movie_store");
            HtmlUnitDriver driver = new HtmlUnitDriver();
            driver.setJavascriptEnabled(false);
            driver.get(datum.getUrl());
            // System.out.println(driver.getPageSource());
            System.out.println(driver.findElement(By.xpath("//html/body/header/h1")).getText());
            // ProfilesIni pi = new ProfilesIni();
            // FirefoxProfile profile = pi.getProfile("default");
            // WebClient webClient = new
            // WebClient(BrowserVersion.FIREFOX_38);
            // driver.setJavascriptEnabled(false);
            // webClient.getOptions().setCssEnabled(true);
            // HtmlPage page = webClient.getPage(datum.getUrl());
            // System.out.println(page.asXml());
            // List<?> title =
            // page.getByXPath("//html/body/header/h1/text()");
            // List<?> score =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/hgroup/article[1]/i/text()");
            // List<?> score_num =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/hgroup/article[1]/span/text()");
            // List<?> want_num =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/hgroup/article[2]/i/text()");
            // List<?> type =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/p[1]/text()");
            // List<?> timing =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/p[2]/text()");
            // List<?> system =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/p[3]/text()");
            // List<?> onscreendate =
            // page.getByXPath("//html/body/section[1]/article[1]/aside/p[4]/text()");
            // List<?> total_rev =
            // page.getByXPath("//html/body/section[1]/article[2]/span[1]/text()");
            // List<?> firstweek_rev =
            // page.getByXPath("//html/body/section[1]/article[2]/span[2]/text()");
            // List<?> ahead_rev =
            // page.getByXPath("//html/body/section[1]/article[2]/span[3]/text()");
            // List<?> director =
            // page.getByXPath("//*[@id='infoContent']/article[1]/div/div[1]/div/div[2]/div[1]/div[2]/text()");
            // List<?> actor =
            // page.getByXPath("//*[@id='infoContent']/article[1]/div/div[1]/div/div[2]/div[2]/div[2]/text()");
            // List<?> production =
            // page.getByXPath("//*[@id='infoContent']/article[2]/div/div[1]/div/div[2]/text()");
            // List<?> distribution =
            // page.getByXPath("//*[@id='infoContent']/article[4]/div/div[1]/div/div[2]/text()");
            // List<?> technique =
            // page.getByXPath("//*[@id='infoContent']/article[5]/div/div[1]/div/div[2]/text()");
            // List<?> desc =
            // page.getByXPath("//*[@id='infoContent']/article[6]/div/div[2]/text()");
            // List<?> want_by_city =
            // page.getByXPath("//*[@id='wantCity']/ul/li[2]/b/text()");
            // List<?> want_val=
            // page.getByXPath("//*[@id='wantCity']/ul/li[3]/div/@style");
            // System.out.println(title);
            //// System.out.println(page.getByXPath("//span[@class='today']/em/text()"));
            //// System.out.println(page.getByXPath("//span[@class='today']/text()"));
            //// List<?> movie_name =
            // page.getByXPath("//div[@id='seat_table']//ul//li[@class='c1
            // lineDot']/text()");
            //// List<?> boxoffice_rate =
            // page.getByXPath("//div[@id='seat_table']//ul//li[@class='c2
            // red']/text()");
            //// List<?> visit_pershow =
            // page.getByXPath("//div[@id='seat_table']//ul//li[@class='c3
            // gray']/text()");
            //// for(int i = 0;i<movie_name.size();i++){
            //// System.out.println(movie_name.get(i));
            //// System.out.println(boxoffice_rate.get(i));
            //// System.out.println(visit_pershow.get(i));
            //// }
            // List<?> hbList = page.getByXPath("//div");
            // HtmlDivision hb = (HtmlDivision)hbList.get(0);
            // System.out.println(hb.toString());
            // System.out.println(ReturnString(driver,By.xpath("//*[@id='infoContent']/article[4]/div/div[1]/div/div[2]")));
            if (ReturnString(driver, (By.xpath("//html/body/header/h1"))).length() != 0) {
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", ReturnString(driver, (By.xpath("//html/body/header/h1"))))
                        .append("url", driver.getCurrentUrl())
                        .append("score",
                                ReturnString(driver,
                                        By.xpath(
                                                "//html/body/section[1]/article[1]/aside/hgroup/article[1]/i")))
                        .append("score_num",
                                ReturnString(driver, By.xpath(
                                        "//html/body/section[1]/article[1]/aside/hgroup/article[1]/span")))
                        .append("want_num",
                                ReturnString(driver,
                                        By.xpath(
                                                "//html/body/section[1]/article[1]/aside/hgroup/article[2]/i")))
                        .append("type",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[1]")))
                        .append("timing",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[2]")))
                        .append("system",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[3]")))
                        .append("onscreendate",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[1]/aside/p[4]")))
                        .append("total_rev",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[2]/span[1]")))
                        .append("firstweek_rev",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[2]/span[2]")))
                        .append("ahead_rev",
                                ReturnString(driver, By.xpath("//html/body/section[1]/article[2]/span[3]")))
                        .append("director", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[@class='m-info-crews m-info-section']/div/div[1]/div/div[2]/div[1]/div[2]")))
                        .append("actor", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[@class='m-info-crews m-info-section']/div/div[1]/div/div[2]/div[2]/div[2]")))
                        .append("production", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[@class='production-companies m-info-section']/div/div[1]/div/div[2]")))
                        .append("jointproduction", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[@class='joint-production-companies m-info-section']/div/div[1]/div/div[2]")))
                        .append("distribution", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[@class='distribution-firm m-info-section']/div/div[1]/div/div[2]")))
                        .append("techpara", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[tech-params m-info-section]/div/div[1]/div/div[2]")))
                        .append("infodrama", ReturnString(driver, By.xpath(
                                "//*[@id='infoContent']/article[m-info-drama m-info-section]/div/div[2]")))
                        .append("want_by_city", ReturnString(driver, By.xpath("//*[@id='wantCity']")));
                dbCollection.insert(dbObject);
                driver.close();
                driver.quit();
                mongoClient.close();
            }
        }
    };
    MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
    // ?
    // DBCollection dbCollection =
    // mongoClient.getDB("maoyan_crawler").getCollection("rankings_am");
    DB db = mongoClient.getDB("maoyan_crawler");
    // ?????
    Set<String> colls = db.getCollectionNames();
    for (String s : colls) {
        // Collection(?"")
        if (s.equals("movie_store")) {
            db.getCollection(s).drop();
        }
    }

    // DBDBManager
    DBManager manager = new BerkeleyDBManager("maoyan");
    // Crawler?DBManagerExecutor
    for (int round = 200; round < 300; round++) {
        System.out.println("Round " + round + " crawling.../n");
        Crawler crawler = new Crawler(manager, executor);
        for (int i = 1 + 100 * round; i <= 100 + 100 * round; i++) {
            crawler.addSeed("http://pf.maoyan.com/movie/" + i + "?_v_=yes");
        }
        /* ??visit */
        crawler.setVisitInterval(50);
        /* ?http? */
        crawler.setRetryInterval(100);
        crawler.setThreads(10);
        crawler.start(1);
    }
}