• MongoDB MapReduce 小例子


    var map = function(){
        if (this.gscode == "ZTJB"){
            ymd = this.ymd;
            emit("maxymd", ymd);
        }
    }
    var reduce = function(key, values){
        var maxYmd = values[0];
        for (var i=1; i<values.length; i++){
            if (maxYmd < values[i]){
                maxYmd = values[i];
            }
        }
        return maxYmd;
    }
    db.getCollection('calcgsdataflash').mapReduce(
        map,
        reduce,
        {out:{inline:1}}
    );

    C API 小例子:

      官方文档地址:http://mongoc.org/libmongoc/1.8.2/distinct-mapreduce.html

    vector<string> vMaxDate;
    const char *const MAPPER = "function(){"
    	"Date = this.Date;"
    	"emit('maxDate',Date)"
    "}";
    
    const char *const REDUCER = "function(key, values){"
    	"var maxDate = values[0];"
    	"for (var i=1; i<values.length; i++){"
    		"if (maxDate < values[i]){"
    			"maxDate = values[i];"
    		"}"
    	"}"
    	"return maxDate;"
    "}";
    
    m_mongoDBMgr.MapReduce(MAPPER, REDUCER, vMaxDate);
    for (vector<string>::iterator iter = vMaxDate.begin(); iter != vMaxDate.end(); iter++)
    {
    	Json::Reader reader;
    	Json::Value value;
    	if (reader.parse((*iter).c_str(), value))
    	{
    		iMaxDate = value["value"].asInt();
    		TLOG_DEBUG("L2Dynamic iMaxDate:" << iMaxDate << endl);
    	}
    }//maxYmd
    
    bool CMongoDBMgr::MapReduce(const char *const MAPPER, const char *const REDUCER, vector<string> &vData)
    {
        TLOG_DEBUG("begin MapReduce" << endl);
    
        bson_t reply;
        bson_t *command;
        bson_error_t error;
        mongoc_cursor_t *cursor;
        const bson_t *doc;
    
        bool map_reduce_done = false;
        bool query_done = false;
    
        const char *out_collection_name = "outCollection";
        mongoc_collection_t *out_collection;
    
        bson_t find_query = BSON_INITIALIZER;
    
        //do MapReduce
        command = BCON_NEW (
            "mapReduce",
            BCON_UTF8 (m_sCollection.c_str()),
            "map",
            BCON_CODE (MAPPER),
            "reduce",
            BCON_CODE (REDUCER),
            "out",
            BCON_UTF8 (out_collection_name)
        );
        bool bRet = mongoc_database_command_simple (m_database, command, NULL, &reply, &error);
        map_reduce_done = true;
        if (!bRet)
        {
            TLOG_DEBUG("MapReduce failed:" << error.message << endl);
            goto cleanup;
            return false;
        }
    
        //do query
        out_collection = mongoc_database_get_collection (m_database, out_collection_name);
    
        cursor = mongoc_collection_find_with_opts (out_collection, &find_query, NULL, NULL);
        query_done = true;
    
        while (mongoc_cursor_next (cursor, &doc))
        {
            char *str;
            str = bson_as_json(doc, NULL);
            vData.push_back(str);
        }
    
        if (mongoc_cursor_error (cursor, &error))
        {
            TLOG_DEBUG("An error occurred:" << error.message << endl);
            goto cleanup;
            return false;
        }
    
    cleanup:
        if (map_reduce_done)
        {
            bson_destroy (&reply);
            bson_destroy (command);
        }
        
        if (query_done)
        {
            mongoc_cursor_destroy(cursor);
            mongoc_collection_destroy (out_collection);
        }
    
        return true;
    }
    

      

      

  • 相关阅读:
    SQL中的escape 将通配符 变成普通符号
    shell 变量
    vi 常用命令
    Java 内存模型基础
    Linux 的文件权限与目录配置
    Mybatis 源码分析之事物管理
    Mybatis 源码分析之一二级缓存
    Mybatis源码分析之结果集处理
    从JDBC看Mybatis的设计
    Mybatis源码分析之插件的原理
  • 原文地址:https://www.cnblogs.com/SZxiaochun/p/8017310.html
Copyright © 2020-2023  润新知