本文整理汇总了Java中com.mongodb.MapReduceOutput.results方法的典型用法代码示例。如果您正苦于以下问题:Java MapReduceOutput.results方法的具体用法?Java MapReduceOutput.results怎么用?Java MapReduceOutput.results使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.MapReduceOutput
的用法示例。
在下文中一共展示了MapReduceOutput.results方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calcularLocalizaciones
import com.mongodb.MapReduceOutput; //导入方法依赖的package包/类
/**
* Map reduce.
*
* @param mongoOperation
* the mongo operation
* @param a
* the a
* @param b
* the b
* @param c
* the c
* @param d
* the d
* @throws UnknownHostException
*/
static void calcularLocalizaciones() throws UnknownHostException {
String map = "function () { emit(this.localizacion, {count: 1}); }";
String reduce = " function(key, values) { var result = 0; values.forEach(function(value){ result++ }); "
+ "return result; }";
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("craulerdb");
DBCollection ofertas = db.getCollection("ofertas");
MapReduceCommand cmd = new MapReduceCommand(ofertas, map, reduce, null, MapReduceCommand.OutputType.INLINE,
null);
MapReduceOutput out = ofertas.mapReduce(cmd);
for (DBObject o : out.results()) {
System.out.println(o.toString());
}
}
示例2: testMapReduce
import com.mongodb.MapReduceOutput; //导入方法依赖的package包/类
@Test
public void testMapReduce() throws Exception{
MongoClient client = new MongoClient("localhost", 27017);
DBCollection articles = client.getDB("ensim").getCollection("articles");
// Write map function as Javascript.
String map = "function() { " +
" emit('note', this.note);" +
"}";
// Write reduc function as Javascript.
String reduce = "function(key, values) { " +
" return Array.sum(values) / values.length; " +
"}";
// Execute map/reduce job without finalize function and filter query.
MapReduceOutput out = articles.mapReduce(map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
try{
for (DBObject o : out.results()){
System.err.println(o.toString());
}
} catch (Exception e){
e.printStackTrace();
}
}
示例3: getMovies
import com.mongodb.MapReduceOutput; //导入方法依赖的package包/类
@GET
@Path("movies/{id}")
@Produces(MediaType.APPLICATION_JSON)
public static String getMovies(@PathParam("id") String userId) {
Logger.getLogger(XPostWS.class.getName()).log(Level.INFO, "retrieving movies for " + userId);
final JsonObject result = new JsonObject();
Mongo mongo = getActionMongo();
final DB db = mongo.getDB("mydb");
final DBCollection moviesCol = db.getCollection("movies");
String map = "function () {" + "if (this.userId != this.friendId)" + "{emit(this.id, this);}" + "};";
String reduce = "function (key, values) {" +
"var res = {};" +
"res.name = values[0].name;" +
"res.id = key;" +
"res.userId = values[0].userId;" +
"res.poster = values[0].poster;" +
"res.rating = values[0].rating;" +
"res.imdbId = values[0].imdbId;" +
"res.friendId = [];" +
"for (var i = 0; i<values.length; ++i) {" +
"res.friendId.push(String(values[i].friendId));" +
"}" +
"return res;}";
final BasicDBObject searchObject = new BasicDBObject("userId", userId);
MapReduceCommand mapReduceComand = new MapReduceCommand(moviesCol, map, reduce, null,
MapReduceCommand.OutputType.INLINE, searchObject);
final MapReduceOutput moviesReduced = moviesCol.mapReduce(mapReduceComand);
JsonArray moviesInfo = new JsonArray();
for (DBObject movie : moviesReduced.results()) {
moviesInfo.put(new JsonObject(movie.toString()));
}
Logger.getLogger(XPostWS.class.getName()).log(Level.INFO,
"retrieved " + moviesInfo.length() + " movies for user " + userId);
if (moviesInfo.length() == 0) {
result.put("movies", new JsonArray().toString());
} else {
sortMovies(moviesInfo, userId);
result.put("movies", moviesInfo);
}
try {
return new String(result.toString().getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
Logger.getLogger(TAG).severe(e.getMessage());
return result.toString();
}
}
示例4: getComments
import com.mongodb.MapReduceOutput; //导入方法依赖的package包/类
public List<JSONObject> getComments(String word) throws UnknownHostException, MongoException, SQLException, ClassNotFoundException, JSONException {
// TODO Auto-generated method stub
List<JSONObject> listeComments = new ArrayList<JSONObject>();
Mongo mongoClient = new Mongo();
DB db = mongoClient.getDB("social");
DBCollection coll = db.getCollection("comments");
String m="function wordMap(){"+
"var text = this.comment;"+
"var words = text.match(/\\w+/g);"+
"var tf = new Array();"+
"for( var i=0; i< words.length ; i++ ){"+
"if( tf[words[i]] == null){"+
"tf[words[i]]=1;"+
"}"+
"else{"+
"tf[words[i]]++;"+
"}"+
"}"+
"for( var i=0; i< words.length ; i++ ){"+
"emit(this._id, { word : words[i], tf : tf[words[i]] } )}};";
String r="function wordReduce(key, values){"+
"return ( { \"tfs\" : values } )};";
MapReduceOutput out = coll.mapReduce(m,r,null,MapReduceCommand.OutputType.INLINE,null);
String[] tabWords = word.split(" ");
SortedMap<String,String> map = new TreeMap<String,String>();
for ( DBObject obj : out.results()){
JSONObject jsonObj = new JSONObject(obj.toMap());
String idObj = jsonObj.get("_id").toString();
JSONObject jsonValue = new JSONObject(jsonObj.get("value").toString());
JSONArray tfsTab = new JSONArray(jsonValue.get("tfs").toString());
for(int i=0; i<tfsTab.length();i++){
JSONObject tfsObj = tfsTab.getJSONObject(i);
for(String w : tabWords){
if(tfsObj.get("word").equals(w)){
String nbtf = tfsObj.get("tf").toString();
map.put(idObj,nbtf);
}
}
}
}
mongoClient.close();
Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
JSONObject objToAdd = new JSONObject();
objToAdd.put("_id",key.toString());
listeComments.add(objToAdd);
}
return listeComments;
}
示例5: mapReduce
import com.mongodb.MapReduceOutput; //导入方法依赖的package包/类
public Iterable<DBObject> mapReduce(MapReduceCommand cmd) {
MapReduceOutput output = getCollection().mapReduce(cmd);
return output.results();
}