Cookies help us deliver our services. By using our services, you agree to our use of cookies. More information

Difference between revisions of "MapReduce"

From NoSQLZoo
Jump to: navigation, search
Line 12: Line 12:
 
The reduce function has two inputs, for every distinct key emitted by map the reduce function is called with a list of the corresponding values.
 
The reduce function has two inputs, for every distinct key emitted by map the reduce function is called with a list of the corresponding values.
  
==emit all continents==
+
==Population of each continent==
 
<div class=q data-lang="mongo">
 
<div class=q data-lang="mongo">
This example returns the number of countries in each continent.
+
Here the map function emits the continent and the population for each country.
  
 +
The reduce function uses the JavaScript function Array.sum to add the populations.
 +
<pre class=def>
 +
db.world.mapReduce(
 +
  function(){emit(this.continent, this.population);},
 +
  function(k, v){ return Array.sum(v); },
 +
  {out:{inline:1}}
 +
)
 +
</pre>
 +
</div>
 +
 +
 +
==Number of countries in each continent==
 +
<div class=q data-lang="mongo">
 +
Instead of sending populations you can send a list one 1s to the reduce function.
 +
 +
That was the reduce function will count the number of countries in each continent.
 
<pre class=def>
 
<pre class=def>
 
db.world.mapReduce(
 
db.world.mapReduce(

Revision as of 20:14, 2 August 2016

Introducing the MapReduce function

The MapReduce function is an aggregate function that consists of two functions: Map and Reduce.

The map is always performed before the reduce.

The map function examines every document in the collection and emits (key,value) pairs.

The map function takes no input however the current document can be accessed as this

The reduce function has two inputs, for every distinct key emitted by map the reduce function is called with a list of the corresponding values.

Population of each continent

Here the map function emits the continent and the population for each country.

The reduce function uses the JavaScript function Array.sum to add the populations.

db.world.mapReduce(
  function(){emit(this.continent, this.population);}, 
  function(k, v){ return Array.sum(v); },
  {out:{inline:1}}
)


Number of countries in each continent

Instead of sending populations you can send a list one 1s to the reduce function.

That was the reduce function will count the number of countries in each continent.

db.world.mapReduce(
  function(){emit(this.continent, 1);}, 
  function(k, v){ return Array.sum(v); },
  {out:{inline:1}}
)

emit only some continents

The map function may emit only sometimes.

In the example we are only counting the countries that have a large population

db.world.mapReduce(
  function(){
    if (this.population>100000000)
      emit(this.continent, 1);},
  function(k, v){ return v.length; },
  {out:{"inline":1}}
)

examine the reduce function

Examine the reduce function.

Here we emit the continent and the name, in the reduce function we return v.join(',') to see a comma separated list of the values in the list.

db.world.mapReduce(
  function(){
    if (this.population>100000000)
      emit(this.continent, this.name);},
  function(k, v){ return v.join(','); },
  {out:{"inline":1}}
)