MongoDB中使用MapReduce進行分組統計

來源:互聯網
上載者:User

MongoDB中使用MapReduce進行分組統計

MongoDB中使用MapReduce進行分組統計

最近在統計某一個時間段的url去重數,由於資料量巨大導致報錯,提示:

distinct failed: {

"errmsg" : "exception: distinct too big, 16mb cap",

"code" : 17217,

"ok" : 0

} at src/mongo/shell/collection.js:1108


經過查閱資料,最終通過mapreduce來解決如下:

//定義map函數

map=function(){

    emit(this.url,{"count":1});

}

//定義reduce函數

reduce=function(key,values){

    var total=0;

    for(var i=0; i < values.length; i++){

        total+=values[i].count;

    }

    return {count:total}

}

//執行mapreduce函數,其中out的值是儲存執行結果的集合

db.runCommand({"mapreduce":"visit","map":map,"reduce":reduce,"query":{"vtime":{"$gte":1412611200,"$lte":1413907119}},"out":"test.tmp"});

CentOS編譯安裝MongoDB

CentOS 編譯安裝 MongoDB與mongoDB的php擴充

CentOS 6 使用 yum 安裝MongoDB及伺服器端配置

Ubuntu 13.04下安裝MongoDB2.4.3

MongoDB入門必讀(概念與實戰並重)

Ubunu 14.04下MongoDB的安裝指南

《MongoDB 權威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]

Nagios監控MongoDB分區叢集服務實戰

基於CentOS 6.5作業系統搭建MongoDB服務

MongoDB 的詳細介紹:請點這裡
MongoDB 的:請點這裡

本文永久更新連結地址:

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.