The first glimpse of Mongodb Mapreduce

Source: Internet
Author: User
Tags emit mongodb
Summary:The author declares: This paper is a by-product in the process of learning MongoDB, because contact time is not long, it is inevitable to understand the deviation, hope to use this article and interested friends to discuss, hehe. At the end of last year, began to contact and learn the MapReduce model. Because at work,

The author declares: This paper is a by-product in the process of learning MongoDB, because contact time is not long, it is inevitable to understand the deviation, hope to use this article and interested friends to discuss, hehe.

At the end of last year, began to contact and learn the MapReduce model. Because of the work relationship, we have recently begun to study MongoDB, which is interested in the implementation of the new feature (April 2010) reduce model because of special attention. Of course, the content of the Internet on this aspect is not much, and many for the en, so I think it is necessary to learn the use of some of the problems in the process of recording and finishing, because there is this article.

Don't say much nonsense, start the body.

Currently, the C # client that supports MongoDB should be the Samuel Corder Open source project, Link: http://github.com/samus/mongodb-csharp.

In the Mongodb.net-tests directory in its source pack there are corresponding test cases for testmapreduce and Testmapreducebuilder, because I did not install nunit locally, so the next thing I did was copy some of them directly in a new Web project Code Test (Note: Please refer to the relevant information for the MapReduce model).

First we have to load the test data, here we dnt in the online user list (structure) as the basis, batch poured into 10 records, the code is as follows:

Mongo db = new mongo ("servers=10.0.4.66:27017; connecttimeout=300000; connectionlifetime=300000; minimumpoolsize=25; maximumpoolsize=25; Pooled=true ");              DB. Connect ();      DATABASE&NBSP;TEST&NBSP;=&NBSP;DB. Getdatabase ("test");   imongocollection things = test["things"];     for  (int i = 1; i <= 10;i++)              {                      document record = new document ();                 record["_id"] =  i;                                 record["userid"] = i;                     record["IP"] =  "10.0.7."  + i;                 record["username"]  =  "User"  + i;                 record["nickname"]  =  "User"  + i;                 record["Password"]  =  "";                 record["GroupID"]  = i;//The MapReduce method is used to group statistics                  record["olimg"] =  "";                 record["Adminid"]  = 0;                 record["Invisible"]  = 0;                 record["Action"]  = 0;                 record["LastActivity "] = 1;                 record["Lastposttime "] = datetime.now.tostring ();                 record[" Lastpostpmtime "] = datetime.now.tostring ();                 record[" Lastsearchtime "] = datetime.now.tostring ();                 record[" LastUpdateTime "] = " 1212313221231231213321 "; &nBsp               record["ForumID"] =  0;                 record["Forumname"]  =  "";                 record["TitleID"]  = 0;                 record["title"]  =  "";                 record["Verifycode"]  =  "";                 record["Newpms"]  = 0;                 record["Newnotices"]  = 0;                 things. Insert (record);                              }      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;DB. Disconnect ();  

Suppose we have a need to find the number of users in the table with a user group (groupid) field of 5, and of course we don't use the normal query method, but instead use the mapreduce approach, which is divided into two phases: the map phase and the reduce phase. Each phase has a key/value pair as input and output, and their types can be selected by the programmer. Here's how it's implemented:

The first is the map method:

String mapfunction = "function () {if (this.groupid==5) {emit ({groupid:5}, 1);}}";

Then the reduce method:

string reducefunction =  "function (key, current ) {"  +                                       "   var count  = 0; "  +                                      "    for (var i in current)  {" +                                       "       count+= Current[i]; "  +                                     "  &NBSP;&NBSP} " +                                       "   return { groupcount : count };"  +  //Note how to return here                                     "};";   

Finally, we use the following code to implement the corresponding code binding and MapReduce class declarations above map,reduce:

MapReduce Mr = Mrcol.       MapReduce (); Mr.       Map = new Code (mapfunction); Mr.       Reduce = new Code (REDUCEFUNCTION4); Mr.       Execute (); foreach (Document doc in Mr.       Documents) {int groupcount = Convert.ToInt32 (doc["value"]); } Mr. Dispose ();

Run the above code, showing the results as follows:

The "ID:" {"GroupID": 5} in the current Watch window is defined in Mapfunction, of course, if you want to count the number of users in all user groups (10 user groups), simply rewrite the mapfunction to:

String mapfunction = "function () {Emit (This.groupid, 1);}";

In this way, it will be the key (make sure not to repeat) according to the GroupID that the current user belongs to, the same group of users will be sent as output (emit), emit can be understood as calling the reduce method, where the parameter is 1[that is cumulative 1 operation]).

At the moment I hit the MongoDB on the Internet. The example basically revolves around the grouping statistic function.

Of course, the reference and return values can be used like tuples, remember the above "emit ({groupid:5}, 1)" code. Return value This can also be used in the following way:

string reducefunction =  "function (key, current ) {"  +                                       "   var count  = 0; "  +                                      "    for (var i in current)  {" +                                       "       count+= Current[i]; "  +                                     "  &NBSP;&NBSP} " +                                       "   return { groupcount : count };"  +  //Note how to return here                                     "};";   

The return type is changed, and the way in which the value is taken is also changed:

int groupcount = Int. Parse (((Document) doc["value"]) ["GroupCount"]. ToString ());

Of course, the declarations of the MapReduce class above are used in a way that is overly formal, using chained calls:

using  (Mapreducebuilder mrb = mrcol. Mapreducebuilder (). Map (mapfunction). Reduce (reducefunction))     {          using  ( MAPREDUCE&NBSP;MR&NBSP;=&NBSP;MRB. Execute ())           {                      foreach  (Document doc  in mr. Documents)                       {                          int groupcount = int. Parse (((Document) doc["value"]) ["GroupCount"]. ToString ());                     &NBSP;&NBSP}   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}  } 
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.