List of usage examples for com.mongodb.client MongoCollection aggregate
AggregateIterable<TDocument> aggregate(List<? extends Bson> pipeline);
From source file:org.nuxeo.tools.esync.db.DbMongo.java
License:Open Source License
@Override public Map<String, Long> getTypeCardinality() { final Map<String, Long> ret = new LinkedHashMap<>(); MongoCollection<org.bson.Document> table = getMongoCollection(); AggregateIterable<org.bson.Document> iterable = table.aggregate(asList( new org.bson.Document("$match", new BasicDBObject("ecm:isProxy", new BasicDBObject("$ne", true))), new org.bson.Document("$group", new org.bson.Document("_id", "$ecm:primaryType").append("count", new org.bson.Document("$sum", 1))))); iterable.forEach(new Block<org.bson.Document>() { @Override//from w ww. j ava 2 s . c o m public void apply(final org.bson.Document document) { String primaryType = document.getString("_id"); long count = document.getInteger("count"); if (!ROOT_TYPE.equals(primaryType)) { ret.put(primaryType, count); } } }); return ret; }
From source file:us.daveread.education.mongo.honeypot.BasicStatistics.java
License:Open Source License
/** * Summarize the count of attacks by country. This method uses MongoDB's * aggregation framework to summarize the data. It then retrieves the * resulting documents and displays them. * <p>//from ww w . j av a2s.co m * Compare this to the countryBreakdownCoded which produces the same report * but reads the raw documents and then uses Java code to produce the * summary. * <p> * Note that this method creates a aggregation pipeline matching the * following JSON (which can be used in the Mongo client directly): * * <pre> * # summarize country * db.honeypotData.aggregate( [ * { "$group": { "_id": "$client_country_code" , "hits": { "$sum": 1 } } }, * { "$sort": { "hits":-1} } * ]) * </pre> * * @see #countryBreakdownCoded() */ private void countryBreakdownAggregation() { MongoCollection<Document> collection = accessCollection(HONEYPOT_COLLECTION); List<Document> aggregationPipeline = new ArrayList<>(); Document operation; /** * Group the data on country code. Count the number of documents in each * group. */ operation = new Document("$group", new Document("_id", "$client_country_code").append("attacks", new Document("$sum", 1))); aggregationPipeline.add(operation); /** * Sort the data on count of attacks, descending. */ operation = new Document("$sort", new Document("attacks", -1)); aggregationPipeline.add(operation); /** * Get the iterable for the pipeline result. */ AggregateIterable<Document> attacks = collection.aggregate(aggregationPipeline); /** * Report the top country codes with their associated attack counts. */ int limit = Math.min(totalAttackingCountries, NUMBER_OF_ITEMS_TO_DISPLAY); printHeader("Top " + limit + " Attack Countries (using aggregation pipeline)"); int count = 0; for (Document attack : attacks) { int numAttacks = attack.getInteger("attacks"); System.out.println(" " + attack.get("_id") + ": " + numAttacks + " (" + (int) ((numAttacks * 100) / (double) totalAttacks) + "%)"); ++count; if (count >= limit) { break; } } }
From source file:us.daveread.education.mongo.honeypot.BasicStatistics.java
License:Open Source License
/** * Summarize the number of attacks recorded by each honeypot server and * channel (sensor). This method uses MongoDB's aggregation framework to * summarize the data. It then retrieves the resulting documents and * displays them./*from w w w.j a v a2 s . co m*/ * <p> * Compare this to the honeypotBreakdownCoded which produces the same report * but reads the raw documents and then uses Java code to produce the * summary. * <p> * Note that this method creates a aggregation pipeline matching the * following JSON (which can be used in the Mongo client directly): * * <pre> * # summarize server and channel * db.honeypotData.aggregate( [ * { "$group": { "_id": { "server_ip_mask" : "$payload.server_ip_mask" , * "channel" : "$channel" }, "attacks": { "$sum": 1 } } }, * { "$project": { "server_ip_mask" : "$_id.server_ip_mask", * "channel" : "$_id.channel", "attacks" : "$attacks" } }, * { "$sort": { "_id.server_ip_mask":1, "_id.channel":1} } * ]) * </pre> * * @see #honeypotBreakdownAggregation() */ private void honeypotBreakdownAggregation() { MongoCollection<Document> collection = accessCollection(HONEYPOT_COLLECTION); List<Document> aggregationPipeline = new ArrayList<>(); Document operation; /** * Group by server IP mask value and channel. Count the number of * documents in each group. */ operation = new Document("$group", new Document("_id", new Document("server_ip_mask", "$payload.server_ip_mask").append("channel", "$channel")) .append("attacks", new Document("$sum", 1))); aggregationPipeline.add(operation); /** * Project the attributes to extract them from the _id. */ operation = new Document("$project", new Document("server_ip_mask", "$_id.server_ip_mask") .append("channel", "$_id.channel").append("attacks", 1)); aggregationPipeline.add(operation); /** * Sort the data based on server IP mask value and channel. */ operation = new Document("$sort", new Document("_id.server_ip_mask", 1).append("_id.channel", 1)); aggregationPipeline.add(operation); /** * Get the iterable for the pipeline result. */ AggregateIterable<Document> attacks = collection.aggregate(aggregationPipeline); /** * Report the attack counts for each server and channel. */ printHeader("Attack Counts for Servers and Channels (using aggregation pipeline)"); for (Document attack : attacks) { int numAttacks = attack.getInteger("attacks"); System.out.println(" Server:" + attack.getInteger("server_ip_mask") + " Channel:" + attack.getString("channel") + " Attack Count:" + numAttacks + " (" + (int) ((numAttacks * 100) / (double) totalAttacks) + "%)"); } }
From source file:us.daveread.education.mongo.honeypot.BasicStatistics.java
License:Open Source License
/** * Summarize the number of attacks from each client. This method uses * MongoDB's aggregation framework to summarize the data. It then retrieves * the resulting documents and displays them. * <p>//from w ww. ja v a 2s . c om * Note that this method creates a aggregation pipeline matching the * following JSON (which can be used in the Mongo client directly): * * <pre> * # summarize most active IPs and their countries * db.honeypotData.aggregate( [ * { "$group": { "_id": { "client_ip_mask" : "$payload.client_ip_mask" , * "client_country_code" : "$client_country_code" }, "attacks": { "$sum": 1 } } }, * { "$project": { "client_ip_mask" : "$_id.client_ip_mask", * "client_country_code" : "$_id.client_country_code", "attacks" : "$attacks" } }, * { "$sort": {"attacks":-1} } * ]) * </pre> */ private void mostActiveIps() { MongoCollection<Document> collection = accessCollection(HONEYPOT_COLLECTION); List<Document> aggregationPipeline = new ArrayList<>(); Document operation; /** * Group by client IP mask value and country code (expect a given IP to * always map the the same country). Count the number of documents in * each group. */ operation = new Document("$group", new Document("_id", new Document("client_ip_mask", "$payload.client_ip_mask") .append("client_country_code", "$client_country_code")).append("attacks", new Document("$sum", 1))); aggregationPipeline.add(operation); /** * Project the attributes to extract them from the _id. */ operation = new Document("$project", new Document("client_ip_mask", "$_id.client_ip_mask") .append("client_country_code", "$_id.client_country_code").append("attacks", "$attacks")); aggregationPipeline.add(operation); /** * Sort the data based on number of attacks, descending. */ operation = new Document("$sort", new Document("attacks", -1)); aggregationPipeline.add(operation); /** * Get the iterable for the pipeline result. */ AggregateIterable<Document> attacks = collection.aggregate(aggregationPipeline); /** * Report the attack counts for top attacking clients. */ int limit = Math.min(totalAttackingCountries, NUMBER_OF_ITEMS_TO_DISPLAY); printHeader("Top " + limit + " Attacking Client IPs (using aggregation pipeline)"); int count = 0; for (Document attack : attacks) { int numAttacks = attack.getInteger("attacks"); System.out.println(" Client:" + attack.getInteger("client_ip_mask") + " Country:" + attack.getString("client_country_code") + " Attack Count:" + numAttacks + " (" + (int) ((numAttacks * 100) / (double) totalAttacks) + "%)"); ++count; if (count >= limit) { break; } } }
From source file:week2.MongoDBSparkFreemarkerStyle.java
License:Apache License
public static void main(String[] args) { final Configuration configuration = new Configuration(); configuration.setClassForTemplateLoading(MongoDBSparkFreemarkerStyle.class, "/week2/freemarker"); MongoClient client = new MongoClient(); MongoDatabase database = client.getDatabase("m101"); final MongoCollection<Document> collection = database.getCollection("funnynumbers"); get("/", (request, response) -> { StringWriter writer = new StringWriter(); try {//from w w w . j a v a 2s .c om 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<>()); int answer = 0; for (Document cur : results) { answer += (Double) cur.get("_id"); } Map<String, String> answerMap = new HashMap<>(); answerMap.put("answer", Integer.toString(answer)); template.process(answerMap, writer); } catch (Exception e) { e.printStackTrace(); halt(500); } return writer; }); }