Skip to content

Map Reduce

Disclaimer: work in progress, not complete yet.

Introduction

Map Reduce is supported, this will allow two javascript functions to be passed in for Map and Reduce, the query is a partial TQL query statement, this only needs to contain a WHERE clause and optionally a LIMIT and/or ORDER BY clause. This is another way to perform an Aggregate/query by modifying what is available with TQL.

Parameters

Clause Description
map This is a javascript function that will map a value with a key to create a key-value pair.
reduce This is a javascript function that can reduce/group the number of documents based on their key.
query TQL query to get records that match a specific condition (Only the WHERE, LIMIT and ORDER BY clauses are used).
finalize Similar to the finalize supported in TQLFlow, this allows the records to be passed through an anonymous javascript function to format the data again.

The Map portion of the map-reduce query will be applied to each record that is found from the result of the query passed in. The function is able to map/link values to a specific key and if their are multiple values it will group the result into a single object. Think similar to a normal GROUP BY in TQL or TQLFlow.

Map

This is a javascript function that will map a value with a key to create a key-value pair.

Example:

A basic map javascript function:

function() {
    emit('all', {key: 'all', num_dependants: this.num_dependants});
}

Reduce

This is a javascript function that can reduce/group the number of documents based on their key.

Example:

A basic reduce javascript function, here it takes the value for key k and counts up the number of dependents and then creates the key value pair of key k: value num_dependents.

function(k, v) {
    var num = 0;
    for (var i in v) {
        num += v[i].num_dependants;
    }

    // ensure that the M/R is idempotent to the map()
    return {key: k,
            num_dependants: num}
}

Query

TQL query to get records that match a specific condition (Only the WHERE, LIMIT and ORDER BY clauses are used). This does allow for LIMIT and ORDER BY however this does a limit on the result list prior to doing the map, reduce, or finalize stage.

Example:

This is a partial TQL statement that matches on the name “John Smith”, limits the returned results to 100 and orders it by the attribute name.

WHERE name = "John Smith"
LIMIT 100
ORDER BY name

Finalize

Similar to the finalize supported in TQLFlow, this allows the records to be passed through an anonymous javascript function to format the data again.

Example:

a finalize example that doesn’t make any modifications:

function(k,rv) {
   return rv;
}