Using MapReduce in RapidQL Queries
MapReduce can be applied within RapidQL to any array result in your query.
For example, let's assume the following query:
{
transactions:MySQL.transactions.find(user:"Iddo") {
date,
amount
}
}
Now let's say we want to take all those transactions the user made, and sum up the total amount per day (date). The transactions item will be an array, and thus we can wrap it with a MapReduce predicate:
{
MapReduce.totalTransactionsByDay() {
transactions:MySQL.transactions.find(user:"Iddo") {
date,
float(amount)
}
}
}
You can see that when calling MapReduce, we refer to a specific MapReduce pipeline called totalTransactionsByDay. It's up to the developer to define the pipeline, which will hold all the MapReduce logic. Pipelines are defined in the options object, and have 3 key components:
map:
The map function will get called for each item in the array we are applying the MapReduce pipeline to. It should return a single object / array of objects of the form:
{key:"", value:""}
The key indicates the group this item will belong to when reduces, and the value is the value to be reduced.
reduce:
The reduce function will get 2 items at a time belonging to the same key (those items are the values returned by the map function). It should return a single value, which will be the reduced value for the key.
The reduce function will get applied against an accumulator and each element from the map function to reduce it to a single value.
result[key] = reduce(v1,reduce(v2,reduce(...,reduce(vn-1,vn))));
reduceInitial:
The initial value that the reduce function will start with.
Back in our example of the total transactions, the map function will map the transaction amount (value) to the day:
(transaction) => {
return {key: transaction[date], value: transaction[amount]
}
The reduce function will just sum up the different transactions:
(sum, new) => {
return sum + new;
}
reduceInitial will be 0 - as we want to start summing transactions from 0 every day.
These 3 components will go into the options object like so:
const rql = new RapidQL({
MapReduce: {
totalTransactionsByDay: { // the name opf the map reduce pipeline. We can have multiple piplines
map: (transaction) => {
return {key: transaction[date], value: transaction[amount]
},
reduce: (sum, new) => {
return sum + new;
},
reduceInitial: 0
}
}
});
Updated about 6 years ago