Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2014 Grigory Pykhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.s1.mongodb;
import com.mongodb.*;
import org.s1.cluster.dds.beans.CollectionId;
import org.s1.objects.Objects;
import org.s1.table.AggregationBean;
import org.s1.table.CountGroupBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* MongoDB aggregation helper
* DEBUG - aggregate, countGroup
*/
public class MongoDBAggregationHelper {
private static final Logger LOG = LoggerFactory.getLogger(MongoDBAggregationHelper.class);
/**
* Aggregate by field
*
* @param c
* @param field
* @param search
* @return {max,min,sum,count,avg}
*/
public static AggregationBean aggregate(CollectionId c, String field,
Map search) {
DBCollection coll = MongoDBConnectionHelper.getConnection(c.getDatabase()).getCollection(c.getCollection());
Map gr = Objects.newHashMap(
"_id","$_null",
"min",Objects.newHashMap("$min","$"+field),
"max",Objects.newHashMap("$max","$"+field),
"sum",Objects.newHashMap("$sum","$"+field),
"avg",Objects.newHashMap("$avg","$"+field),
"count",Objects.newHashMap("$sum",1L)
);
//search
if (search == null)
search = Objects.newHashMap();
AggregationOutput out = coll.aggregate(new BasicDBObject("$match", MongoDBFormat.fromMap(search)),
new BasicDBObject("$group", gr));
BasicDBList l = (BasicDBList) out.getCommandResult().get("result");
DBObject res = new BasicDBObject();
if(l.size()>0){
res = (DBObject)l.get(0);
res.removeField("_id");
}
if(LOG.isDebugEnabled())
LOG.debug("MongoDB aggregation result ("+
c+", search: "+search+
") \n\t> "+res);
Map m = MongoDBFormat.toMap(res);
AggregationBean a = new AggregationBean();
a.setMin(Objects.get(m,"min"));
a.setMax(Objects.get(m, "max"));
a.setAvg(Objects.get(m, "avg"));
a.setSum(Objects.get(m, "sum"));
a.setCount(Objects.get(Long.class, m, "count"));
return a;
}
/**
* Average group count in countGroup
* Default = 20
*/
public static int GROUP_COUNT = 20;
/**
* Get groups (by field) with record count.
* Performs auto regroup if group count is larger then GROUP_COUNT
*
* @param cid
* @param field
* @param search
* @return [{value, count},...]
*/
public static List countGroup(CollectionId cid,
String field, Map search){
DBCollection coll = MongoDBConnectionHelper.getConnection(cid.getDatabase()).getCollection(cid.getCollection());
Map group = Objects.newHashMap(
"_id","$"+field,
"count",Objects.newHashMap("$sum",1L)
);
//search
if (search == null)
search = Objects.newHashMap();
AggregationOutput out = coll.aggregate(new BasicDBObject("$match", MongoDBFormat.fromMap(search)),
new BasicDBObject("$group", group));
List