MongoDB MapReduce java使用

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());
        }
        
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值