本篇文章为大家展示了Java中怎么实现一个Group功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
接口方法如下:
/** * keyColumn : new String[]{"xxxName","xxxType"} <br> * condition : 查询条件 ,可为空<br> * initial : 分组统计初始变量,为空时自动为每列提供初始变量<br> * reduce : 记录处理function<br> * finalize : finalize函数,可为空 <br> */ public BasicDBList group(String[] keyColumn, DBObject condition, DBObject initial, String reduce, String finalize) { DBCollection coll = getCollection(); DBObject key = new BasicDBObject(); for (int i = 0; i < keyColumn.length; i++) { key.put(keyColumn[i], true); } condition = (condition == null) ? new BasicDBObject() : condition; if (StringUtils.isEmpty(finalize)) { finalize = null; } if (initial == null) { //定义一些初始变量 initial = new BasicDBObject(); for (int i = 0; i < keyColumn.length; i++) { DBObject index = new BasicDBObject(); index.put("count", 0); index.put("sum", 0); index.put("max", 0); index.put("min", 0); index.put("avg", 0); index.put("self", ""); initial.put(keyColumn[i], index); } } BasicDBList resultList = (BasicDBList) coll.group(key, condition, initial, reduce, finalize); return resultList; }
实现案例:
//任务统计 @Action(value = "getTaskStatistic", results = { @Result(name = "success", type = "json", params = { "includeProperties", "jsonResult" }) }) public String getTaskStatistic() { DBObject initial = new BasicDBObject(); DBObject index = new BasicDBObject(); index.put("count", 0); index.put("taskStatus", ""); initial.put("taskStatus", index); String reduce = "function (doc, out) { " + " out.taskStatus.count = out.taskStatus.count+=1; " + " out.taskStatus.inspectStatus = doc.taskStatus;" + "}"; BasicDBList group = (BasicDBList) taskStatusService.group(new String[] { "taskStatus"}, null, initial, reduce, null); this.jsonResult = group.toString(); return SUCCESS; }
返回数据:
[ { "taskStatus" : { "count" : 4.0 , "taskStatus" : "Finished"}} , { "taskStatus" : { "count" : 3.0 , "taskStatus" : "Received"}} , { "taskStatus" : { "count" : 2.0 , "taskStatus" : "UnReceive"}}]
上述内容就是Java中怎么实现一个Group功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。