Grouping and aggregation
In the following example, we will perform grouping and aggregation in order to get statistics (Sum, Max, Min, and Average) about NBA players and which number of points they scored. First, the map
function will send the name of the player and the number of points scored for each game. The map
function will look like this:
function(){emit(this.player, this.points); }
Then, we can perform all the aggregation functions simultaneously using the sum
method from the JavaScript Array
object and the max
/min
functions of the JavaScript Math
object. The reduce
function will look like this:
function(key, values) {
var explain = {total:Array.sum(values),
max:Math.max.apply(Math, values),
min:Math.min.apply(Math, values),
avg:Array.sum(values)/values.length}
return explain
}
For this example, we will create synthetic data, randomly mixing the names of the 10 players...