Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

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

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

From source file:ch.qos.logback.contrib.mongodb.MongoDBAccessEventAppender.java

License:Open Source License

private void addRequest(BasicDBObject parent, IAccessEvent event) {
    final BasicDBObject request = new BasicDBObject();
    final String uri = event.getRequestURI();
    if (requestUri && uri != null && !uri.equals("-")) {
        request.append("uri", uri);
    }/*from  w ww .jav a2s. c o m*/
    final String protocol = event.getProtocol();
    if (requestProtocol && protocol != null)
        request.append("protocol", protocol);
    final String method = event.getMethod();
    if (requestMethod && method != null)
        request.append("method", method);
    final String requestContent = event.getRequestContent();
    if (requestPostContent && requestContent != null && !requestContent.equals("")) {
        request.append("postContent", requestContent);
    }
    final String jSessionId = event.getCookie("JSESSIONID");
    if (requestSessionId && jSessionId != null && !jSessionId.equals("-"))
        request.append("sessionId", jSessionId);
    final String userAgent = event.getRequestHeader("User-Agent");
    if (requestUserAgent && userAgent != null && !userAgent.equals("-"))
        request.append("userAgent", userAgent);
    final String referer = event.getRequestHeader("Referer");
    if (requestReferer && referer != null && !referer.equals("-"))
        request.append("referer", referer);
    if (!request.isEmpty())
        parent.put("request", request);
}

From source file:ch.qos.logback.contrib.mongodb.MongoDBAccessEventAppender.java

License:Open Source License

private void addResponse(BasicDBObject doc, IAccessEvent event) {
    final BasicDBObject response = new BasicDBObject();
    if (responseContentLength)
        response.append("contentLength", event.getContentLength());
    if (responseStatusCode)
        response.append("statusCode", event.getStatusCode());
    if (!response.isEmpty())
        doc.append("response", response);
}

From source file:ch.qos.logback.contrib.mongodb.MongoDBLoggingEventAppender.java

License:Open Source License

@Override
protected BasicDBObject toMongoDocument(ILoggingEvent event) {
    BasicDBObject logEntry = new BasicDBObject();
    logEntry.append("message", event.getFormattedMessage());
    logEntry.append("logger", event.getLoggerName());
    logEntry.append("thread", event.getThreadName());
    logEntry.append("timestamp", new Date(event.getTimeStamp()));
    logEntry.append("level", event.getLevel().toString());
    if (event.getMDCPropertyMap() != null && !event.getMDCPropertyMap().isEmpty()) {
        logEntry.append("mdc", event.getMDCPropertyMap());
    }//from  ww w . j  a  va  2  s  . co  m
    if (includeCallerData) {
        logEntry.append("callerData", toDocument(event.getCallerData()));
    }
    if (event.getArgumentArray() != null && event.getArgumentArray().length > 0) {
        logEntry.append("arguments", event.getArgumentArray());
    }
    appendThrowableIfAvailable(logEntry, event);
    return logEntry;
}

From source file:ch.qos.logback.contrib.mongodb.MongoDBLoggingEventAppender.java

License:Open Source License

private void appendThrowableIfAvailable(BasicDBObject doc, ILoggingEvent event) {
    if (event.getThrowableProxy() != null) {
        final BasicDBObject val = toMongoDocument(event.getThrowableProxy());
        doc.append("throwable", val);
    }//from  ww  w . ja v a 2  s . c o  m
}

From source file:ch.qos.logback.contrib.mongodb.MongoDBLoggingEventAppender.java

License:Open Source License

private BasicDBObject toMongoDocument(IThrowableProxy throwable) {
    final BasicDBObject throwableDoc = new BasicDBObject();
    throwableDoc.append("class", throwable.getClassName());
    throwableDoc.append("message", throwable.getMessage());
    throwableDoc.append("stackTrace", toSteArray(throwable));
    if (throwable.getCause() != null) {
        throwableDoc.append("cause", toMongoDocument(throwable.getCause()));
    }//from  w w w.ja  va  2 s.  c  o  m
    return throwableDoc;
}

From source file:cn.edu.hfut.dmic.webcollector.example.DemoSelenium.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Executor executor = new Executor() {
        @Override/*ww w.  java2s. c  o  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 click_view = driver.findElement(By.xpath("//div[@id='seatContent']//span[1]"));
            click_view.click();
            String gold_seat = driver.getWindowHandle();
            driver.switchTo().window(gold_seat);
            System.out.println(driver.getPageSource());
            WebElement city_name = driver.findElement(By.xpath("//*[@id='all-citys']/div[1]/ul/li[1]/a"));
            System.out.println(city_name.getText());
            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("crawl");
    //Crawler?DBManagerExecutor
    Crawler crawler = new Crawler(manager, executor);
    crawler.addSeed("http://pf.maoyan.com/attend/rate");
    crawler.start(1);
}

From source file:cn.edu.hfut.dmic.webcollector.example.FirefoxSelenium3.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Executor executor = new Executor() {
        @Override/*from  www  .  j  av  a 2  s  .  c o  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");
            ProfilesIni pi = new ProfilesIni();
            FirefoxProfile profile = pi.getProfile("default");
            WebDriver driver = new FirefoxDriver(profile);
            driver.manage().window().maximize();
            driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS);
            //                driver.setJavascriptEnabled(false);
            driver.get(datum.getUrl());
            //                System.out.println(driver.getPageSource());
            driver.findElement(By.xpath("//*[@id='seat_city']")).click();
            driver.switchTo().window(driver.getWindowHandle());

            int city_num = driver.findElements(By.xpath("//div[@id='all-citys']/div/ul/li/a")).size();
            for (int i = 0; i < city_num; i++) {
                System.out.println("A city chosen" + i);
                System.out.println(
                        driver.findElements(By.xpath("//div[@id='all-citys']/div/ul/li/a")).get(i).getText());
                String city = driver.findElements(By.xpath("//div[@id='all-citys']/div/ul/li/a")).get(i)
                        .getText();
                ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);",
                        driver.findElements(By.xpath("//div[@id='all-citys']/div/ul/li/a")).get(i));
                ((JavascriptExecutor) driver).executeScript("window.scrollBy(0, -250)", "");
                Thread.sleep(1000);
                new Actions(driver)
                        .moveToElement(
                                driver.findElements(By.xpath("//div[@id='all-citys']/div/ul/li/a")).get(i))
                        .click().perform();
                driver.switchTo().window(driver.getWindowHandle());
                //                System.out.println(driver.findElement(By.xpath("//span[@class='today']/em")).getText());
                System.out.println(driver.findElement(By.xpath("//span[@class='today']")).getText());
                for (int j = 0; j < driver
                        .findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c1 lineDot']"))
                        .size(); j++) {
                    System.out.println(driver
                            .findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c1 lineDot']"))
                            .get(j).getText());
                    System.out.println(
                            driver.findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c2 red']"))
                                    .get(j).getText());
                    System.out.println(
                            driver.findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c3 gray']"))
                                    .get(j).getText());
                    BasicDBObject dbObject = new BasicDBObject();
                    dbObject.append("title", driver.findElement(By.xpath("//span[@class='today']")).getText())
                            .append("city", city)
                            .append("mov_cnname",
                                    driver.findElements(
                                            By.xpath("//div[@id='seat_table']//ul//li[@class='c1 lineDot']"))
                                            .get(j).getText())
                            .append("boxoffice_rate", driver
                                    .findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c2 red']"))
                                    .get(j).getText())
                            .append("visit_pershow", driver
                                    .findElements(By.xpath("//div[@id='seat_table']//ul//li[@class='c3 gray']"))
                                    .get(j).getText());
                    dbCollection.insert(dbObject);
                }
                System.out.println("new city list to choose");
                new Actions(driver).moveToElement(driver.findElement(By.xpath("//*[@id='seat_city']"))).click()
                        .perform();
                driver.switchTo().window(driver.getWindowHandle());
                Thread.sleep(500);
            }
            driver.close();
            driver.quit();
            mongoClient.close();
        }
    };

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

From source file:cn.edu.hfut.dmic.webcollector.example.TutorialCrawler.java

License:Open Source License

@Override
public void visit(Page page, CrawlDatums next) {
    if (page.matchUrl("http://pf.maoyan.com/rankings/america.*")) {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        // ?//from   w  w w.j a  v a  2  s . c om
        // 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("rankings_am")) {
                db.getCollection(s).drop();
            }
        }
        DBCollection dbCollection = db.getCollection("rankings_am");
        String title = page.select("span[id=year-box]").text();
        Elements table = page.select("table[id=na-list]");
        Elements data_set = table.select("tr");
        List amList = new ArrayList();
        for (Element id : data_set) {
            Elements tds = id.select("td");
            for (Element td : tds) {
                amList.add(td.text());
            }
            String en_name = tds.select("p[class=first-line]").text();
            String cn_name = tds.select("p[class=second-line]").text();
            if (amList.size() > 0) {
                System.out.println(amList);
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title).append("rank", amList.get(0)).append("mov_cnname", cn_name)
                        .append("mov_enname", en_name).append("toweek_rev", amList.get(2))
                        .append("total_rev", amList.get(3)).append("val_week", amList.get(4));
                amList.removeAll(amList);
                dbCollection.insert(dbObject);
            }
        }
        mongoClient.close();
    } else if (page.matchUrl("http://pf.maoyan.com/rankings/day.*")) {
        MongoClient mongoClient2 = new MongoClient("localhost", 27017);
        // ?
        DB db = mongoClient2.getDB("maoyan_crawler");
        // ?????
        Set<String> colls = db.getCollectionNames();
        for (String s : colls) {
            // Collection(?"")
            if (s.equals("rankings_day")) {
                db.getCollection(s).drop();
            }
        }
        DBCollection dbCollection2 = db.getCollection("rankings_day");
        String title = page.select("span[id=year-box]").text();
        String update_time = page.select("span[id=update-time]").text();
        title = title + update_time;
        System.out.println(title);
        Elements data_set = page.select("tr");
        List dayList = new ArrayList();
        for (Element id : data_set) {
            Elements tds = id.select("td");
            for (Element td : tds) {
                dayList.add(td.text());
            }
            if (dayList.size() > 0) {
                System.out.println(dayList);
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title).append("rank", dayList.get(0))
                        .append("mov_name", dayList.get(1)).append("today_rev", dayList.get(2))
                        .append("date", dayList.get(3)).append("val_week", dayList.get(4));
                dayList.removeAll(dayList);
                dbCollection2.insert(dbObject);
            }
        }
        mongoClient2.close();
    } else if (page.matchUrl("http://pf.maoyan.com/rankings/market.*")) {
        MongoClient mongoClient3 = new MongoClient("localhost", 27017);
        // ?
        DB db = mongoClient3.getDB("maoyan_crawler");
        // ?????
        Set<String> colls = db.getCollectionNames();
        for (String s : colls) {
            // Collection(?"")
            if (s.equals("rankings_market")) {
                db.getCollection(s).drop();
            }
        }
        DBCollection dbCollection3 = db.getCollection("rankings_market");
        String title = page.select("span[id=year-box]").text();
        String update_time = page.select("span[id=update-time]").text();
        title = title + update_time;
        System.out.println(title);
        Elements data_set = page.select("tr");
        List dayList = new ArrayList();
        for (Element id : data_set) {
            Elements tds = id.select("td");
            for (Element td : tds) {
                dayList.add(td.text());
            }
            if (dayList.size() > 0) {
                System.out.println(dayList);
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title).append("rank", dayList.get(0)).append("date", dayList.get(1))
                        .append("today_rev", dayList.get(2)).append("total_sessions", dayList.get(3))
                        .append("total_visit_count", dayList.get(4));
                dayList.removeAll(dayList);
                dbCollection3.insert(dbObject);
            }
        }
        mongoClient3.close();
    } else if (page.matchUrl("http://pf.maoyan.com/rankings/year.*")) {
        MongoClient mongoClient4 = new MongoClient("localhost", 27017);
        // ?
        DB db = mongoClient4.getDB("maoyan_crawler");
        // ?????
        Set<String> colls = db.getCollectionNames();
        for (String s : colls) {
            // Collection(?"")
            if (s.equals("rankings_year")) {
                db.getCollection(s).drop();
            }
        }
        DBCollection dbCollection4 = db.getCollection("rankings_year");
        String title = page.select("span[id=year-box]").text();
        String update_time = page.select("span[id=update-time]").text();
        title = title + update_time;
        System.out.println(title);
        Elements table = page.select("div[id=ranks-list]");
        //             System.out.println(table);
        Elements data_set = table.select("ul[class=row]");
        //             System.out.println(data_set);
        List dayList = new ArrayList();
        for (Element id : data_set) {
            Elements lis = id.select("li");
            for (Element li : lis) {
                dayList.add(li.text());
            }
            String cn_name = lis.select("p[class=first-line]").text();
            String release_date = lis.select("p[class=second-line]").text();
            if (dayList.size() > 0) {
                System.out.println(dayList);
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title).append("rank", dayList.get(0)).append("name", cn_name)
                        .append("release date", release_date).append("year_rev", dayList.get(2))
                        .append("avg_price", dayList.get(3)).append("avg_visit_count", dayList.get(4));
                dayList.removeAll(dayList);
                dbCollection4.insert(dbObject);
            }
        }
        mongoClient4.close();
    } else if (page.matchUrl("http://pf.maoyan.com/")) {
        MongoClient mongoClient5 = new MongoClient("localhost", 27017);
        // ?
        DB db = mongoClient5.getDB("maoyan_crawler");
        // ?????
        Set<String> colls = db.getCollectionNames();
        for (String s : colls) {
            // Collection(?"")
            if (s.equals("main_page")) {
                db.getCollection(s).drop();
            }
        }
        DBCollection dbCollection5 = db.getCollection("main_page");
        String title = page.select("span[id=dayStr]").text();
        String box_type = page.select("span[id=box-type]").text();
        String ticket_count = page.select("span[id=ticket_count]").text();
        box_type = box_type + ticket_count;
        System.out.println(title + "\n" + box_type);
        Elements table = page.select("div[id=ticket_tbody]");
        //System.out.println(table);
        Elements data_set = table.select("ul");
        //System.out.println(data_set);
        List dayList = new ArrayList();
        for (Element id : data_set) {
            Elements lis = id.select("li");
            for (Element li : lis) {
                dayList.add(li.text());
            }
            //           System.out.println(lis);
            String cn_name = lis.select("b").first().text();
            String comment = lis.select("em").text();
            //         System.out.println(cn_name+ "\n" +comment);
            if (dayList.size() > 0) {
                System.out.println(dayList);
                BasicDBObject dbObject = new BasicDBObject();
                dbObject.append("title", title).append("box_type", box_type).append("name", cn_name)
                        .append("comment", comment).append("realtime_rev", dayList.get(1))
                        .append("rev_percent", dayList.get(2)).append("schedule_percent", dayList.get(3))
                        .append("total_rev", dayList.get(4));
                dayList.removeAll(dayList);
                dbCollection5.insert(dbObject);
            }
        }
        mongoClient5.close();
    }
}

From source file:co.dewald.log.MongoDBAppender.java

License:Open Source License

/**
 * @param event/*from w  ww .  ja v  a  2s. c o m*/
 */
protected void append(LoggingEvent event) {
    try {
        if (!init())
            return;

        BasicDBObject log = new BasicDBObject("timestamp", new Date(event.timeStamp));
        log.append("logger", event.getLoggerName());
        log.append("level", event.getLevel().toString());
        log.append("thread", event.getThreadName());
        log.append("message", event.getRenderedMessage());

        if (event.getLocationInformation() != null) {
            LocationInfo info = event.getLocationInformation();

            log.append("file", info.getFileName());
            log.append("line", info.getLineNumber());
            log.append("class", info.getClassName());
            log.append("method", info.getMethodName());
        }

        if (event.getThrowableInformation() != null) {
            ThrowableInformation info = event.getThrowableInformation();
            BasicDBList throwable = new BasicDBList();

            throwable.addAll(Arrays.asList(info.getThrowableStrRep()));
            log.append("throwable", throwable);
        }

        if (event.getNDC() != null)
            log.append("ndc", event.getNDC());

        try {
            event.getMDCCopy();
            @SuppressWarnings("rawtypes")
            Map properties = event.getProperties();

            if (properties != null && !properties.isEmpty()) {
                log.append("mdc", new BasicDBObject(properties));
            }
        } catch (NoSuchMethodError backwardsCompatible) {
            backwardsCompatible.printStackTrace();
        }

        this.logs.insert(log);
    } catch (Throwable irony) {
        irony.printStackTrace();
    }
}

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

License:MIT License

public UsuarioDTO createUsuario(UsuarioDTO user) {

    //      UsuarioEntity entity=UsuarioConverter.persistenceDTO2Entity(usuario);
    //      entityManager.persist(entity);
    //      return UsuarioConverter.entity2PersistenceDTO(entity);

    DBCollection coll = null;// w  w w  .j a  va2s . c  o m
    BasicDBObject doc = null;

    UsuarioDTO usuario = (UsuarioDTO) user;
    coll = db.getCollection("UsuarioDTO");
    doc = new BasicDBObject();
    doc.append("name", usuario.getName());
    doc.append("contrasena", usuario.getContrasena());
    doc.append("email", usuario.getEmail());
    doc.append("facebookId", usuario.getFacebookId());

    coll.insert(doc);
    return user;
}