Example usage for com.mongodb BasicDBObject BasicDBObject

List of usage examples for com.mongodb BasicDBObject BasicDBObject

Introduction

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

Prototype

public BasicDBObject() 

Source Link

Document

Creates an empty object.

Usage

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());
    }//  w w w  .  j a va  2  s.c  o  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 BasicDBList toDocument(StackTraceElement[] callerData) {
    final BasicDBList dbList = new BasicDBList();
    for (final StackTraceElement ste : callerData) {
        dbList.add(new BasicDBObject().append("file", ste.getFileName()).append("class", ste.getClassName())
                .append("method", ste.getMethodName()).append("line", ste.getLineNumber())
                .append("native", ste.isNativeMethod()));
    }/*from w  ww . ja v a 2s .  co  m*/
    return dbList;
}

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()));
    }//  w  w w  . j a  v a 2s . c o  m
    return throwableDoc;
}

From source file:ch.windmobile.server.social.mongodb.ChatServiceImpl.java

License:Open Source License

@Override
public Message postMessage(String chatRoomId, String pseudo, String text, String emailHash) {
    String collectionName = computeCollectionName(chatRoomId);
    DBCollection col = getOrCreateCappedCollection(collectionName);

    DBObject chatItem = new BasicDBObject();
    Double id = (Double) db.eval(counter, chatRoomId);
    chatItem.put("_id", id.longValue());
    chatItem.put(MongoDBConstants.CHAT_PROP_TEXT, text);
    chatItem.put(MongoDBConstants.CHAT_PROP_USER, pseudo);
    DateTime date = new DateTime().toDateTimeISO();
    chatItem.put(MongoDBConstants.CHAT_PROP_TIME, date.toString());
    chatItem.put(MongoDBConstants.CHAT_PROP_EMAIL_HASH, emailHash);
    col.insert(chatItem);// www. j av  a2s .c o  m

    Message message = new Message();
    message.setId(id.toString());
    message.setDate(date);
    message.setPseudo(pseudo);
    message.setText(text);
    message.setEmailHash(emailHash);

    return message;
}

From source file:cn.cnic.bigdatalab.flume.sink.mongodb.EventParser.java

License:Apache License

public DBObject parse(Event event) {

    DBObject dbObject = new BasicDBObject();
    if (definition.getBodyType() != MongoDataType.NULL) {
        Object obj = null;/*from  w w  w .j ava2s . c o  m*/
        if (definition.getBodyType() == MongoDataType.BINARY && definition.getBodyEncoding().equals("raw")) {
            obj = event.getBody();
        } else if (definition.getBodyType() == MongoDataType.STRING) {
            Charset charset = Charset.forName(definition.getBodyEncoding());
            obj = new String(event.getBody(), charset);
        } else {
            SimpleFieldDefinition fd = new SimpleFieldDefinition();
            fd.setType(definition.getBodyType());
            fd.setEncoding(definition.getBodyEncoding());
            obj = parseValue(fd, new String(event.getBody(), Charsets.UTF_8));
        }

        if (!"".equals(definition.getBodyField())) {
            dbObject.put(definition.getBodyField(), obj);
        } else if (obj instanceof DBObject) {
            dbObject = (DBObject) obj;
        } else {
            log.warn("Could not map body to JSON document: {}", obj);
        }
    }

    final Map<String, String> eventHeaders = event.getHeaders();
    if (definition.allowsAdditionalProperties()) {
        for (final Map.Entry<String, String> headerEntry : eventHeaders.entrySet()) {
            final String fieldName = headerEntry.getKey();
            final String fieldValue = headerEntry.getValue();
            FieldDefinition def = definition.getFieldDefinitionByName(fieldName);
            if (def == null) {
                dbObject.put(fieldName, parseValue(null, fieldValue));
            } else {
                final String mappedName = (def.getMappedName() == null) ? def.getFieldName()
                        : def.getMappedName();
                if (eventHeaders.containsKey(fieldName)) {
                    dbObject.put(mappedName, parseValue(def, fieldValue));
                }
            }
        }
    } else {
        for (FieldDefinition def : definition.getFields()) {
            final String fieldName = def.getFieldName();
            final String mappedName = (def.getMappedName() == null) ? def.getFieldName() : def.getMappedName();
            if (containsKey(eventHeaders, fieldName)) {
                dbObject.put(mappedName, parseValue(def, getFieldName(eventHeaders, fieldName)));
            }
        }
    }

    return dbObject;
}

From source file:cn.cnic.bigdatalab.flume.sink.mongodb.EventParser.java

License:Apache License

private DBObject populateDocument(DocumentFieldDefinition fd, String document) {
    DBObject dbObject = null;/*ww  w  . j a  v  a 2s  .  c  om*/
    final String delimiter = fd.getDelimiter();
    if (!StringUtils.isEmpty(delimiter)) {
        String[] documentAsArrray = document.split(Pattern.quote(delimiter));
        dbObject = new BasicDBObject();
        Map<String, FieldDefinition> documentMapping = new LinkedHashMap<String, FieldDefinition>(
                fd.getDocumentMapping());
        int i = 0;
        for (Map.Entry<String, FieldDefinition> documentField : documentMapping.entrySet()) {
            if (DOCUMENT_TYPE.equalsIgnoreCase(documentField.getValue().getType().name())) {
                dbObject.put(documentField.getKey(), parseValue(documentField.getValue(), StringUtils.join(
                        Arrays.copyOfRange(documentAsArrray, i, documentAsArrray.length), fd.getDelimiter())));
                i += ((DocumentFieldDefinition) documentField.getValue()).getDocumentMapping().size();
            } else {
                dbObject.put(documentField.getKey(),
                        parseValue(documentField.getValue(), documentAsArrray[i++]));
            }
        }
    } else {
        throw new MongoSinkException("Delimiter char must be set");
    }

    return dbObject;
}

From source file:cn.cnic.bigdatalab.flume.sink.mongodb.MongoSink.java

License:Apache License

/**
 * {@inheritDoc}/*w w w  .  j a  v  a  2 s  .  c  om*/
 */
@Override
public Status process() throws EventDeliveryException {
    log.debug("Executing MongoSink.process()...");
    Status status = Status.READY;
    Channel channel = getChannel();
    Transaction txn = channel.getTransaction();

    try {
        txn.begin();
        int count;
        List<Event> eventList = new ArrayList<Event>();
        for (count = 0; count < batchSize; ++count) {
            Event event = channel.take();

            if (event == null) {
                break;
            }
            eventList.add(event);
        }

        if (count <= 0) {
            sinkCounter.incrementBatchEmptyCount();
            counterGroup.incrementAndGet("channel.underflow");
            status = Status.BACKOFF;
        } else {
            if (count < batchSize) {
                sinkCounter.incrementBatchUnderflowCount();
                status = Status.BACKOFF;
            } else {
                sinkCounter.incrementBatchCompleteCount();
            }

            for (Event event : eventList) {
                final DBObject document = this.eventParser.parse(event);

                if (this.updateInsteadReplace && document.get("_id") != null) {
                    // update requires '_id' field to match document
                    BasicDBObject searchQuery = new BasicDBObject().append("_id", document.get("_id")); // update by _id
                    BasicDBObject updatedDocument = new BasicDBObject().append("$set", document);
                    getDBCollection(event).update(searchQuery, updatedDocument, true, false);
                } else {
                    getDBCollection(event).save(document);
                }
            }

            sinkCounter.addToEventDrainAttemptCount(eventList.size());
        }
        txn.commit();
        sinkCounter.addToEventDrainSuccessCount(count);
        counterGroup.incrementAndGet("transaction.success");
    } catch (ChannelException e) {
        log.error("Unexpected error while executing MongoSink.process", e);
        txn.rollback();
        status = Status.BACKOFF;
        this.sinkCounter.incrementConnectionFailedCount();
    } catch (Throwable t) {
        log.error("Unexpected error while executing MongoSink.process", t);
        txn.rollback();
        status = Status.BACKOFF;
        if (t instanceof Error) {
            throw new MongoSinkException(t);
        }
    } finally {
        txn.close();
    }
    return status;
}

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/*from   w ww .  j  a  va  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");
            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  w  w  w  . j  a v a  2s. c om
        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);
}