public List<Double> getTimeStatsForUser(User u) { DBObject queryObject = new BasicDBObject("authorName", u.getUsername()); String map = "function(){" + "var hours = this.datePublished.getHours();" + "var zone;" + "if(hours >= 0 && hours < 4){" + "zone = 0;" + "}" + "else if(hours >= 4 && hours < 8){" + "zone = 1;" + "}" + "else if(hours >= 8 && hours < 12){" + "zone = 2;" + "}" + "else if(hours >= 12 && hours < 16){" + "zone = 3;" + "}" + "else if(hours >= 16 && hours < 20){" + "zone = 4;" + "}" + "else {" + "zone = 5;" + "}" + "emit(zone, 1);" + "}"; String reduce = "function(k, v){" + "return Array.sum(v);" + "}"; DBCollection collection = datastore.getCollection(Message.class); MapReduceCommand mrc = new MapReduceCommand( collection, map, reduce, null, MapReduceCommand.OutputType.INLINE, queryObject); MapReduceOutput output = collection.mapReduce(mrc); Integer count = collection.find(queryObject).count(); List<Double> result = new ArrayList<>(); for (int i = 0; i <= 5; i++) { result.add(0.0); } result.add(count.doubleValue()); output .results() .forEach( (DBObject o) -> result.set(((Double) o.get("_id")).intValue(), (Double) o.get("value"))); return result; }
public Map<String, Double> getPlaceStatsForUser(User u) { DBObject queryObject = new BasicDBObject("authorName", u.getUsername()); String map = "function(){" + "var place = this.place;" + "if(place != null){" + "if(place === ''){" + "place = 'Empty';" + "}" + "else if(place !== 'Sofia' && place !== 'Plovdiv' && place !== 'Varna' && place !== 'Burgas'){" + "place = 'Other';" + "}" + "}" + "else {" + "place = 'Empty';" + "}" + "emit(place, 1);" + "}"; String reduce = "function(k, v){ return Array.sum(v); }"; DBCollection collection = datastore.getCollection(Message.class); MapReduceCommand mrc = new MapReduceCommand( collection, map, reduce, null, MapReduceCommand.OutputType.INLINE, queryObject); MapReduceOutput output = collection.mapReduce(mrc); Integer count = collection.find(queryObject).count(); Map<String, Double> result = new HashMap<>(); result.put("Total", count.doubleValue()); result.put("Burgas", 0.0); result.put("Varna", 0.0); result.put("Sofia", 0.0); result.put("Plovdiv", 0.0); result.put("Other", 0.0); result.put("Empty", 0.0); output .results() .forEach((DBObject o) -> result.replace((String) o.get("_id"), (Double) o.get("value"))); return result; }
public static void main(String[] args) { // TODO Auto-generated method stub try { MongoClient mc = new MongoClient("localhost"); DB db = mc.getDB("mydb"); DBCollection dbc = db.getCollection("books"); /*BasicDBObject book= new BasicDBObject(); book.put("name", "자바의 정석"); book.put("pages", 600); dbc.insert(book); book= new BasicDBObject(); book.put("name", "스프링4"); book.put("pages", 250); dbc.insert(book); book= new BasicDBObject(); book.put("name", "JQuery"); book.put("pages", 150); dbc.insert(book); book= new BasicDBObject(); book.put("name", "오라클 프로그램"); book.put("pages", 800); dbc.insert(book); book= new BasicDBObject(); book.put("name", "AngularJS"); book.put("pages", 130); dbc.insert(book); book= new BasicDBObject(); book.put("name", "MongoDB"); book.put("pages", 650); dbc.insert(book); book= new BasicDBObject(); book.put("name", "Hadoop"); book.put("pages", 300); dbc.insert(book); book= new BasicDBObject(); book.put("name", "HIVE"); book.put("pages", 350); dbc.insert(book); book= new BasicDBObject(); book.put("name", "PIG"); book.put("pages", 650); dbc.insert(book); book= new BasicDBObject(); book.put("name", "HTML5"); book.put("pages", 360); dbc.insert(book);*/ String map = "function(){" + "var category;" + "if(this.pages>=300){" + "category='Big Books';" + "}else{" + "category='Small Books';}" + "emit(category,{name:this.name});}"; String reduce = "function(key,values){" + "var sum=0;" + "values.forEach(function(doc){" + "sum+=1;" + "});" + "return {books:sum};}"; MapReduceCommand cmd = new MapReduceCommand(dbc, map, reduce, null, MapReduceCommand.OutputType.INLINE, null); MapReduceOutput out = dbc.mapReduce(cmd); for (DBObject o : out.results()) { System.out.println(o.toString()); } } catch (Exception ex) { System.out.println(ex.getMessage()); } }