API Docs for: 2.2.2
Show:

MapReduce Class

Command used to perform a Map-Reduce query in Riak.

The Riak Map-Reduce API uses JSON for its query.

A typical map-reduce query (JSON) will look like:

{
  "inputs": "goog",
  "query": [
    {
       "map": {
         "language": "javascript",
         "source": "function(value, keyData, arg){ var data = Riak.mapValuesJson(value)[0]; var month = value.key.split('-').slice(0,2).join('-'); var obj = {}; obj[month] = data.High - data.Low; return [ obj ];}"
       }
    },
    {
       "reduce": {
         "language": "javascript",
         "source": "function(values, arg){ return [ values.reduce(function(acc, item){ for(var month in item){ if(acc[month]) { acc[month] = (acc[month] < item[month]) ? item[month] : acc[month]; } else { acc[month] = item[month]; } } return acc;  }) ];}",
         "keep": true
       }
    }
  ]
}

For more info see: Loading Data and Running MapReduce

Constructor

MapReduce

(
  • query
  • callback
  • [stream=true]
)

Parameters:

  • query String

    The Map-Reduce query. This is a string containing JSON.

  • callback Function

    The callback to be executed by this command.

    • err String

      An error message. Will be null if no error.

    • response Object

      the response from Riak.

      • done Boolean
        True if the entire response has been received.
      • phase Number
        The phase the response is from.
      • response Object
        The results.
    • data Object

      additional error data. Will be null if no error.

  • [stream=true] Boolean optional

    stream the results or accumulate before calling callback.

Methods

constructPbRequest

() Object protected
Construct and return the Riak protocol buffer message for this command. Subclasses must override this method.

Returns:

Object: a protocol buffer message builder

getCallback

() Function private
Fires the user's callback with the arguments passed in.

Returns:

Function: the user supplied callback

getExpectedResponseCode

() Number private
Returns the expected response code for this command.

Returns:

Number: the expected response code for this command.

getPbReqBuilder

() Object private
Returns and instance of the protocol buffer message builder for this command. This is determined via the pbRequestName passed to the constructor.

Returns:

Object: the builder for the protocol buffer message to be sent for this command

getRiakMessage

() Object private
Returns the encoded protobuf and message header.

Returns:

Object: object containing the header and encoded protobuf

onError

(
  • msg
  • data
)
protected
Called by RiakNode if an error occurs executing the command and all retries are exhausted.

Parameters:

  • msg String
    an error message
  • data Object
    additional error data

onRiakError

(
  • rpbErrorResp
)
protected
Called by RiakNode when a RpbErrorResp is received and all retries are exhausted. Commands may override this method.

Parameters:

  • rpbErrorResp Object
    the RpbErrorResp protocol buffer

onSuccess

(
  • pbResponseMessage
)
Boolean protected
Called by RiakNode when a response is received. Subclasses must override this method.

Parameters:

  • pbResponseMessage Object
    the protocol buffer received from riak

Returns:

Boolean: true if not streaming or the last response has been received, false otherwise.