Example #1
0
    def mapred(self, inputs, query, timeout=None):
        # Construct the job, optionally set the timeout...
        job = {'inputs': inputs, 'query': query}
        if timeout is not None:
            job['timeout'] = timeout

        content = json.dumps(job)

        req = riak_pb.RpbMapRedReq()
        req.request = content
        req.content_type = "application/json"

        # dictionary of phase results - each content should be an encoded array
        # which is appended to the result for that phase.
        result = {}

        def _handle_response(resp):
            if resp.HasField("phase") and resp.HasField("response"):
                content = json.loads(resp.response)
                if resp.phase in result:
                    result[resp.phase] += content
                else:
                    result[resp.phase] = content

        self.send_msg_multi(MSG_CODE_MAPRED_REQ, req, MSG_CODE_MAPRED_RESP,
                            _handle_response)

        # If a single result - return the same as the HTTP interface does
        # otherwise return all the phase information
        if not len(result):
            return None
        elif len(result) == 1:
            return result[max(result.keys())]
        else:
            return result
    def stream_mapred(self, inputs, query, timeout=None):
        # Construct the job, optionally set the timeout...
        content = self._construct_mapred_json(inputs, query, timeout)

        req = riak_pb.RpbMapRedReq()
        req.request = content
        req.content_type = "application/json"

        self._send_msg(MSG_CODE_MAPRED_REQ, req)

        return RiakPbcMapredStream(self)