package jp.chakra.lotus.luence;
import com.mongodb.*;
import org.apache.commons.lang3.RandomStringUtils;
/**
* Created with IntelliJ IDEA.
* Date: 5/1/14
* Time: 2:46 PM
* To change this template use File | Settings | File Templates.
*/
public class MongoMain {
public static void main(String[] ags){
try{
Mongo mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("search");
DBCollection dbCollection = db.getCollection("mapIndex");
//如果name不进行index,后面无法对其sort的
dbCollection.ensureIndex(new BasicDBObject("name",1));
//保存数据
for(int i =0 ; i< 10; i++){
DBObject object = new BasicDBObject();
object.put("name","li" + i);
object.put("age", Long.valueOf(RandomStringUtils.randomNumeric(2)));
object.put("uid", Long.valueOf(RandomStringUtils.randomNumeric(2)));
dbCollection.save(object);
}
//mr
mapreducd(dbCollection);
}catch (Exception e){
e.printStackTrace();
}
}
private static void mapreducd(DBCollection dbCollection){
String map = "function() { "+
"emit(this.name, {sum1:1,sum2:this.age,sum3:this.uid});}";
String reduce = "function(key, values) { " +
" var result = {sum1:0, sum2:0,sum3:0}\n" +
" values.forEach(function(value) {\n" +
" result.sum1 += value.sum1;\n" +
" result.sum2 += value.sum2;\n" +
" result.sum3 += value.sum3;\n" +
" });\n" +
" return result;" +
"} ";
//REPLACE表示将结果写入到resTable表里面
MapReduceCommand cmd = new MapReduceCommand(dbCollection, map, reduce,
"resTable", MapReduceCommand.OutputType.REPLACE, null);
//对结果再次整理 可以不用
cmd.setFinalize("function(key,value){\n" +
" return {classid:key,count:value}\n" +
"}");
//map前sort 可以不用
cmd.setSort(new BasicDBObject("name",-1));
//限制数量
cmd.setLimit(12);
MapReduceOutput out = dbCollection.mapReduce(cmd);
//out.results是无法直接对结果进行排序的,所以如果想对结果拍寻,只能通过 REPLACE,写入数据库,再查找排序
for (DBObject o : out.results()) {
System.out.println(out.toString());
}
//对结果的结果进行排序
DBCursor cursor = out.getOutputCollection().find().sort(new BasicDBObject("value.sum3",1));
while (cursor.hasNext()){
System.out.println(cursor.next().toString());
}
}
}
MongoDB MapReduce java使用
最新推荐文章于 2021-03-16 23:57:15 发布