Example usage for com.mongodb MongoClient getDatabase

List of usage examples for com.mongodb MongoClient getDatabase

Introduction

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

Prototype

public MongoDatabase getDatabase(final String databaseName) 

Source Link

Usage

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

License:Open Source License

@Override
public void visit(Page page, CrawlDatums next) {
    int pageNum = Integer.valueOf(page.getMetaData("pageNum"));
    /*??*//*w w  w. j  av  a 2  s  . c o  m*/
    Elements weibos = page.select("div.c");
    try {
        MongoClient mongoClient = new MongoClient("localhost", 27017);

        // ?
        MongoDatabase mongoDatabase = mongoClient.getDatabase("weibo_crawler");
        System.out.println("Connect to database successfully");
        MongoCollection<Document> collection = mongoDatabase.getCollection("webpage");
        //?  
        /** 
        * 1.  org.bson.Document ?key-value? 
        * 2. ?List<Document> 
        * 3. ???? mongoCollection.insertMany(List<Document>) ??? mongoCollection.insertOne(Document) 
        * */
        for (Element weibo : weibos) {
            Document document = new Document("content", "" + pageNum + "" + ":" + weibo.text());
            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

@Override
public void visit(Page page, CrawlDatums next) {
    String inlink = page.meta("inlink");
    String title = page.select("title").text();
    String url = page.getUrl();/*from   w  ww.j a va 2  s . c  o  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:co.aurasphere.mongodb.university.classes.m101j.homework23.MainClass.java

License:Open Source License

/**
 * The main method./*  ww w  .ja va  2 s .  com*/
 *
 * @param args
 *            the arguments
 */
public static void main(String[] args) {

    MongoClient client = new MongoClient();
    MongoDatabase numbersDB = client.getDatabase("students");
    MongoCollection<Document> grades = numbersDB.getCollection("grades");

    // Gets all the grades.
    MongoCursor<Document> cursor = grades.find(Filters.eq("type", "homework"))
            .sort(Sorts.ascending("student_id", "score")).iterator();

    Object previousStudentId = null;
    try {
        // Finds the lowest homework score.
        while (cursor.hasNext()) {
            Document entry = cursor.next();

            // If the student_id is different from the previous one, this 
            // means that this is the student's lowest graded homework 
            // (they are sorted by score for each student).
            if (!entry.get("student_id").equals(previousStudentId)) {
                Object id = entry.get("_id");
                grades.deleteOne(Filters.eq("_id", id));

            }

            // The current document ID becomes the new previous one.   
            previousStudentId = entry.get("student_id");
        }

        // Gets the student with the highest average in the class.
        AggregateIterable<Document> results = grades
                .aggregate(Arrays.asList(Aggregates.group("$student_id", Accumulators.avg("average", "$score")),
                        Aggregates.sort(Sorts.descending("average")), Aggregates.limit(1)));

        // There should be only one result. Prints it.
        System.out.println("Solution : " + results.iterator().next().toJson());

    } finally {
        cursor.close();
    }

    client.close();
}

From source file:co.aurasphere.mongodb.university.classes.m101j.homework31.MainClass.java

License:Open Source License

/**
 * The main method./*from w ww .  j  a  v  a  2  s.c o m*/
 *
 * @param args
 *            the arguments
 */
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    MongoClient client = new MongoClient();
    MongoDatabase db = client.getDatabase("school");
    MongoCollection<Document> students = db.getCollection("students");

    // Gets all the students.
    MongoCursor<Document> cursor = students.find().iterator();

    try {
        while (cursor.hasNext()) {
            Document student = cursor.next();
            List<Document> scores = (List<Document>) student.get("scores");

            // Finds the lowest homework score.
            Document minScoreObj = null;
            double minScore = Double.MAX_VALUE;

            for (Document scoreDocument : scores) {
                double score = scoreDocument.getDouble("score");
                String type = scoreDocument.getString("type");

                // Swaps the scores.
                if (type.equals("homework") && score < minScore) {
                    minScore = score;
                    minScoreObj = scoreDocument;
                }
            }

            // Removes the lowest score.
            if (minScoreObj != null) {
                scores.remove(minScoreObj);
            }

            // Updates the record.
            students.updateOne(Filters.eq("_id", student.get("_id")),
                    new Document("$set", new Document("scores", scores)));
        }

        // Gets the student with the highest average in the class.
        AggregateIterable<Document> results = students.aggregate(Arrays.asList(Aggregates.unwind("$scores"),
                Aggregates.group("$_id", Accumulators.avg("average", "$scores.score")),
                Aggregates.sort(Sorts.descending("average")), Aggregates.limit(1)));

        // There should be only one result. Prints it.
        System.out.println("Solution : " + results.iterator().next().toJson());

    } finally {
        cursor.close();
    }

    client.close();
}

From source file:com.abhishek.mongodb.homework.week2.MongoDBSparkFreemarkerStyle.java

License:Apache License

public static void main(String[] args) {
    final Configuration configuration = new Configuration();
    configuration.setClassForTemplateLoading(MongoDBSparkFreemarkerStyle.class, "/week2freemarker");

    MongoClient client = new MongoClient();

    MongoDatabase database = client.getDatabase("m101");
    final MongoCollection<Document> collection = database.getCollection("funnynumbers");

    Spark.get(new Route("/") {
        @Override//  w w  w.  j  a  va  2 s  .c om
        public Object handle(final Request request, final Response response) {
            StringWriter writer = new StringWriter();
            try {
                Template template = configuration.getTemplate("answer.ftl");

                // Not necessary yet to understand this.  It's just to prove that you
                // are able to run a command on a mongod server
                List<Document> results = collection.aggregate(asList(
                        new Document("$group",
                                new Document("_id", "$value").append("count", new Document("$sum", 1))),
                        new Document("$match", new Document("count", new Document("$lte", 2))),
                        new Document("$sort", new Document("_id", 1)))).into(new ArrayList<Document>());

                int answer = 0;
                for (Document cur : results) {
                    answer += (Double) cur.get("_id");
                }

                Map<String, String> answerMap = new HashMap<String, String>();
                answerMap.put("answer", Integer.toString(answer));

                template.process(answerMap, writer);
            } catch (Exception e) {
                e.printStackTrace();
                halt(500);
            }
            return writer;
        }
    });
}

From source file:com.AlertMailerWebPage.servlet.GetDaily.java

public AggregateIterable<Document> getCounts(String database, String collection, String serverAddress) {

    Calendar current = Calendar.getInstance();

    int currYear = current.get(Calendar.YEAR);
    int currMonth = current.get(Calendar.MONTH);
    int currDay = current.get(Calendar.DAY_OF_MONTH);
    currMonth = 6;//from w ww  .j  av  a  2 s . co  m
    currDay = 4;

    Calendar curr_6AM = Calendar.getInstance();
    curr_6AM.set(currYear, currMonth, currDay, 0, 0, 0);
    curr_6AM.set(Calendar.MILLISECOND, 0);
    // out.println(curr_6AM.getTimeInMillis());

    //MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017));

    MongoClient mongoClient = new MongoClient();

    //MongoClient mongoClient = new MongoClient();
    AggregateIterable<Document> iterable_6_9_p = mongoClient.getDatabase(database).getCollection(collection)
            .aggregate(asList(new Document("$match", new Document("sentDate", curr_6AM.getTimeInMillis())
                    //.append("alertType", alertType)
                    .append("isMailSent", "Y")
    //.append("recordTime", new Document("$gte",curr_6AM.getTimeInMillis()).append("$lte", curr_9AM.getTimeInMillis()))
    ), new Document("$group",
            new Document("_id", new Document("hour", "$sentHour").append("type", "$alertType")).append("count",
                    new Document("$sum", 1)))));
    //mongoClient.close();
    return iterable_6_9_p;
}

From source file:com.AlertMailerWebPage.servlet.GetDailyOpenMail.java

public AggregateIterable<Document> getCounts(String database, String collection, String serverAddress) {

    Calendar current = Calendar.getInstance();

    int currYear = current.get(Calendar.YEAR);
    int currMonth = current.get(Calendar.MONTH);
    currMonth = 4;/*from  w  w  w .  ja va2 s  .c  o m*/
    currYear = 2015;
    int currDay = current.get(Calendar.DAY_OF_MONTH);

    Calendar curr_6AM = Calendar.getInstance();
    curr_6AM.set(currYear, currMonth, 18, 0, 0, 0);
    curr_6AM.set(Calendar.MILLISECOND, 0);
    // out.println(curr_6AM.getTimeInMillis());

    // MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017));
    MongoClient mongoClient = new MongoClient();
    // MongoClient mongoClient = new MongoClient();
    AggregateIterable<Document> iterable_6_9_p = mongoClient.getDatabase(database).getCollection(collection)
            .aggregate(asList(new Document("$match", new Document("openDate", curr_6AM.getTimeInMillis())
    //.append("openDate",curr_6AM.getTimeInMillis())                

    //.append("recordTime", new Document("$gte",curr_6AM.getTimeInMillis()).append("$lt", curr_9AM.getTimeInMillis()))
    ), new Document("$group",
            new Document("_id", new Document("hour", "$openHour").append("type", "$alertType")).append("count",
                    new Document("$sum", 1)))));

    return iterable_6_9_p;
}

From source file:com.AlertMailerWebPage.servlet.GetDailyPRSProcessStats.java

public AggregateIterable<Document> getCounts(String database, String collection, String serverAddress,
        String time) {/*www  .j a v a  2 s . co m*/
    Calendar current = Calendar.getInstance();
    //out.println( current.getTimeInMillis());
    int currYear = current.get(Calendar.YEAR);
    int currMonth = current.get(Calendar.MONTH);
    //currMonth=6;
    int currDay = current.get(Calendar.DAY_OF_MONTH);

    list.clear();

    Calendar curr_6AM = Calendar.getInstance();
    Calendar curr_24PM = Calendar.getInstance();
    if (time.equals("daily")) {
        //Calendar curr_6AM=Calendar.getInstance();
        curr_6AM.set(currYear, 6, 7, 0, 0, 1);
        // out.println(curr_6AM.getTimeInMillis());

        //Calendar curr_24PM=Calendar.getInstance();
        curr_24PM.set(currYear, 6, 7, 23, 59, 59);
        //out.println(curr_9AM.getTimeInMillis());
    } else if (time.equals("weekly")) {

        curr_6AM.set(currYear, 6, 7, 0, 0, 1);
        curr_6AM.add(Calendar.DATE, -6);

        curr_24PM.set(currYear, 6, 7, 23, 59, 59);
    } else if (time.equals("monthly")) {

        curr_6AM.set(currYear, 6, 7, 0, 0, 1);
        curr_6AM.add(Calendar.DATE, -29);

        curr_24PM.set(currYear, 6, 7, 23, 59, 59);
    }

    //MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017));
    MongoClient mongoClient = new MongoClient();
    //MongoClient mongoClient = new MongoClient();
    AggregateIterable<Document> iterable_6_9_p = mongoClient.getDatabase(database).getCollection(collection)
            .aggregate(asList(new Document("$match",
                    new Document("date", new Document("$gte", Long.toString(curr_6AM.getTimeInMillis()))
                            .append("$lte", Long.toString(curr_24PM.getTimeInMillis())))
    //.append("alertType", alertType)                
    //.append("date", new Document("$gte",Long.toString(curr_6AM.getTimeInMillis())).append("$lt", Long.toString(curr_24PM.getTimeInMillis())))
    ),

                    new Document("$group",
                            new Document("_id", "$errorCode").append("count", new Document("$sum", 1)))));

    return iterable_6_9_p;
}

From source file:com.AlertMailerWebPage.servlet.GetMonthlyOpenMailServlet.java

public AggregateIterable<Document> getCounts(String database, String collection, String serverAddress) {

    Calendar current = Calendar.getInstance();
    Calendar min_13 = Calendar.getInstance();

    min_13.set(2015, 4, 1, 0, 0, 0);/*w w  w  . j  a  v  a2  s  .  c  o  m*/
    min_13.set(Calendar.MILLISECOND, 0);
    current.set(2015, 4, 1, 0, 0, 0);
    current.add(Calendar.DATE, 29);
    current.set(Calendar.MILLISECOND, 0);
    /* min_13.add(Calendar.DATE,-29);
    int y=min_13.get(Calendar.YEAR);
            int m=min_13.get(Calendar.MONTH);
            int d=min_13.get(Calendar.DAY_OF_MONTH);
       min_13.set(y,m,d,0,0,0);
    min_13.set(Calendar.MILLISECOND,0);*/

    // MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017));
    MongoClient mongoClient = new MongoClient();
    //MongoClient mongoClient = new MongoClient();
    AggregateIterable<Document> iterable_6_9_p = mongoClient.getDatabase(database).getCollection(collection)
            .aggregate(asList(new Document("$match", new Document("openDate",
                    new Document("$gte", min_13.getTimeInMillis()).append("$lte", current.getTimeInMillis()))

    ), new Document("$group",
            new Document("_id", new Document("date", "$openDate").append("type", "$alertType")).append("count",
                    new Document("$sum", 1)))));

    return iterable_6_9_p;
}

From source file:com.AlertMailerWebPage.servlet.GetMonthlyServlet.java

public AggregateIterable<Document> getCounts(String alertType, String database, String collection,
        String serverAddress) {/*  w  w w  .  j  a  v  a2 s.com*/

    Calendar current = Calendar.getInstance();
    Calendar min_13 = Calendar.getInstance();

    min_13.set(2016, 6, 1, 0, 0, 0);
    current.set(2016, 6, 1, 23, 59, 59);
    current.add(Calendar.DATE, 29);
    //  min_13.add(Calendar.DATE,-29);
    int y = min_13.get(Calendar.YEAR);
    int m = min_13.get(Calendar.MONTH);
    int d = min_13.get(Calendar.DAY_OF_MONTH);
    min_13.set(y, m, d, 0, 0, 0);
    min_13.set(Calendar.MILLISECOND, 0);
    //MongoClient mongoClient = new MongoClient(new ServerAddress(serverAddress,27017));
    MongoClient mongoClient = new MongoClient();
    AggregateIterable<Document> iterable_6_9_p = mongoClient.getDatabase(database).getCollection(collection)
            .aggregate(asList(
                    new Document("$match",
                            new Document("sentDate",
                                    new Document("$gte", min_13.getTimeInMillis()).append("$lte",
                                            current.getTimeInMillis()))
                                                    //.append("isMailSent","Y")                
                                                    .append("isMailSent", "Y")),
                    new Document("$group",
                            new Document("_id", new Document("date", "$sentDate").append("type", "$alertType"))
                                    .append("count", new Document("$sum", 1)))));

    return iterable_6_9_p;
}