7

我需要运行一个动态的mapreduce 作业,因为每次运行mapreduce 作业时(例如,响应用户请求),都需要将参数传递给map 和reduce 函数。

我该如何做到这一点?我在文档中的任何地方都看不到如何在运行时对 map 和 reduce 进行动态处理。

class MatchProcessing(webapp2.RequestHandler):

  def get(self):
      requestKeyID=int(self.request.get('riderbeeRequestID'))
      userKey=self.request.get('userKey')
      pipeline = MatchingPipeline(requestKeyID, userKey)
      pipeline.start()
      self.redirect(pipeline.base_path + "/status?root=" + pipeline.pipeline_id)


class MatchingPipeline(base_handler.PipelineBase):
    def run(self, requestKeyID, userKey):
        yield mapreduce_pipeline.MapreducePipeline(
            "riderbee_matching",
            "tasks.matchingMR.riderbee_map",
            "tasks.matchingMR.riderbee_reduce",
            "mapreduce.input_readers.DatastoreInputReader",
            "mapreduce.output_writers.BlobstoreOutputWriter",
            mapper_params={
                "entity_kind": "models.rides.RiderbeeRequest",
                "requestKeyID": requestKeyID,
                "userKey": userKey,
            },
            reducer_params={
                "mime_type": "text/plain",
            },
            shards=16)


def riderbee_map(riderbeeRequest):
    # would like to access the requestKeyID and userKey parameters that were passed in mapper_params
    # so that we can do some processing based on that

    yield (riderbeeRequest.user.email, riderbeeRequest.key().id())


def riderbee_reduce(key, values):
    # would like to access the requestKeyID and userKey parameters that were passed earlier, perhaps through reducer_params
    # so that we can do some processing based on that

    yield "%s: %s\n" % (key, len(values))

请帮忙?

4

2 回答 2

5

我很确定您可以在 mapper_parameters 中指定参数,然后从上下文模块中读取它们。有关更多详细信息,请参阅http://code.google.com/p/appengine-mapreduce/wiki/UserGuidePython#Mapper_parameters

于 2012-06-29T23:24:23.923 回答
4

这是使用上下文模块从映射器函数访问映射器参数的方法:

from mapreduce import context

def riderbee_map(riderbeeRequest):
    ctx = context.get()
    params = ctx.mapreduce_spec.mapper.params
    requestKeyID = params["requestKeyID"]
于 2013-11-26T19:23:04.507 回答