Packet statistics for MongoDB's MapReduce

Source: Internet
Author: User
Tags emit mongodb mongodb version

Map/reduce in MongoDB makes some compound queries, because MongoDB does not support group by queries, and MapReduce is similar to SQL's group by, so it can be thought that MapReduce is the MongoDB version of group B Y

The command is as follows:

    Db.runcommand ({mapreduce:, map:, reduce: [, query:] [ , sort:] [, limit:] [, Out:] [, Keeptemp:] [, Finalize:] [, SC  
      
    Ope:] [, Verbose:true]}); MapReduce: Specifies the collection Map:map function Reduce:reduce function to be processed by MapReduce query: A filter condition that only satisfies the condition  
      
    Row is added to the MapReduce collection, which is the sort: and query-combined sort parameter that is executed before the entire mapreduce process, which is the only place to optimize the grouping mechanism: ibid. Out: The collection name of the output of the result, does not specify that a random name will be created by default collection Keytemp:true or FALSE, indicating whether the output to the collection is temporary, if the TR UE, it will be automatically deleted after the client connection is interrupted, if you use a MongoDB MONGO client connection, it must exit before it is deleted.   
      
    If the script is executed, the script exits or calls close to automatically delete the result collection finalize: Like Map,reduce, it is a function that calculates a result of reduce and then evaluates the key and value once and returns a final result.  
      
      
    Scope: Sets the value of the parameter, where the value set here is visible in the Map,reduce,finalize function verbose: Prints debug information during execution.  
    The return result structure is as follows:  
    {result:, Counts: {input:, emit:, Output: }, Timemillis:, OK: <1_if_ok>, [, Err:]} result: The Colle that stores the result ction Name Input: The number of data rows that satisfy the condition emit:emit the number of calls, that is, the total amount of data in all collections ouput: Returns the number of result bars time   Millis: Execution time, Ms OK: successful, 1 err: If it fails, there can be a reason for failure, but from experience, the reason is rather vague, the effect is small
Map function
  m= function () {

                emit (This.hs,
                        {
                            "customid_data": [{Customid:this.customid}],
                            "Salescountry_data": [{salescountry:this.salescountry}],
                            "Usd_data": THIS.USD,
                            "Num_data": This.num,
                            "Compid_data": [{ Compid:this.compid}]
                        }
                )
            
       }

Reduce function:

  R=function (key,values) {var ret = {"Customid_data": [], "Salescoun
                     Try_data ": []," Usd_data ": 0," Num_data ": 0," Compid_data ": []
          };
          var customid= {};
          var salescountry={};
        
           var compid={};
                for (Var i=0;i<values.length;i++) {var ia = values[i]; for (Var j in Ia.customid_data) {if (!customid[ia.customid_data[j].customid]) {C
                         Ustomid[ia.customid_data[j].customid]=true;
                    Ret.customid_data.push (Ia.customid_data[j]); }} for (Var j in Ia.salescountry_data) {if (!salescountry[ia.salescount
                         Ry_data[j].salescountry]) {salescountry[ia.salescountry_data[j].salescountry]=true; Ret.salescountry_data.push (iA.SALESCOUNTRY_DATA[J]); }} for (Var j in Ia.compid_data) {if (!compid[ia.compid_data[j].compid]
                         ) {compid[ia.compid_data[j].compid]=true;
                    Ret.compid_data.push (Ia.compid_data[j]);
                    }} Ret.usd_data+=values[i].usd_data;
           Ret.num_data+=values[i].num_data;
               
    } return ret; }
Finalize function:
F=function (key, values) {
           return     

                 {

                     custom:values.customid_data.length,

                     salescountry: Values.salescountry_data.length,

                     usd_sum:values.usd_data,

                     num_sum:values.num_data,

                     comp: Values.compid_data.length

                };
        }
Execute the MapReduce function
     Db.collect.mapReduce (m,r,{out: ' Groupbyhs ', Query:{isimp: ' 1 '},keytemp:false,finalize:f})

If you want to use features such as sort and limit, you can use Res.find (). Sort ({"Value.totalnum": -1}). Limit (2) to achieve the purpose

It is important to note that the return value of the reduce function also needs to be consistent with the second parameter structure of the reduce function.

Reference article:

Some practical experiences on using MapReduce in MongoDB
http://www.l99.com/EditText_view.action?textId=461413
If you want to use features such as sort and limit , you can use Res.find (). Sort ({"Value.totalnum": -1}). Limit (2) to achieve the purpose (say good)

implements MapReduce with MongoDB
/HTTP/ www.open-open.com/lib/view/open1329107420952.html

Mongodb mapreduce programming Model
/HTTP chenzhou123520.iteye.com/blog/1637672


MongoDB MapReduce (EXT)
http://javabeezer.iteye.com/blog/ 1275124
So the return value of the reduce function also needs to be consistent with the second parameter structure of the reduce function (this is a very good saying.)

[Turn]mongodb MapReduce usage and introduction  
http://blog.163.com/sjy_814/blog/static/77801164201282531137196/


MongoDB Group, go to *******************************
http://my.oschina.net/huzorro/blog/73879

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.