Use C # To feel the charm of MongoDB mapreduce

Source: Internet
Author: User
Tags emit

With the increasing number of hadoop users, MapReduce is becoming more and more popular. MapReduce is a highlight of MongoDB. I also want to know more about MapReduce. In addition, MongoDB is easy to operate, so I chose it. MapReduce divides the problem into multiple different parts and distributes them to different servers for parallel processing. Each server returns the result to the master server after processing a part of the problem assigned to it, the master server summarizes the results and finally completes troubleshooting.

Map maps the document to a set and operates the document. This step may generate multiple keys and values or have nothing (the value to be processed in the document is blank ). Then, group by key and put the generated Value List into the corresponding key. Reduce, the values in the list are reduced to a value. This value is returned, and then the key group is continued to simplify until each key has only one value in the list. This value is the final result.

In MongoDB, you need to use JavaScript to write a Map () function and a Reduce () function. The Map function uses the emit function to return the value to be processed. Emit will pass a key and a value to the redcuce function. The Reduce function receives two parameters. The first parameter is the key, and the second parameter is the key returned by emit. The second parameter is the values array, that is, the value returned by emit, it consists of one or more documents corresponding to keys.

Let's write an instance and use the instance to understand MapReduce.

First, create an object class,

Next, connect to the database,

Insert several test records,

The following is the core of this example. Two JavaScript methods are mainly used to count the number of times different words appear in the set,

Use the following code to traverse the results returned by mapreduce:

Execution result

In this example, the mapreduce processing result method may be similar, and the execution process may be different, but the idea is such an idea,

During computing, mongodb generates several intermediate results and is automatically deleted after the calculation is completed,

You can write a JavaScript function to calculate the record in which the word appears,

The execution result is as follows (below the dotted line)

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.