python - Google App Engine 中不止一种数据存储类型的 MapReduce

标签 python google-app-engine google-cloud-datastore mapreduce task-queue

我刚看了Batch data processing with App Engine session of Google I/O 2010 , 阅读 MapReduce article from Google Research 的部分内容现在我正在考虑使用 MapReduce on Google App Engine用 Python 实现推荐系统。

我更喜欢使用 appengine-mapreduce 而不是 Task Queue API,因为前者可以轻松迭代某种类型的所有实例、自动批处理、自动任务链等。问题是:我的推荐系统需要计算实例之间的相关性两种不同的模型,即两种不同类型的实例。

例子: 我有这两个模型:用户和项目。每个都有一个标签列表作为属性。以下是计算用户和项目之间相关性的函数。请注意,应该为用户和项目的每个组合调用 calculateCorrelation:

def calculateCorrelation(user, item):
    return calculateCorrelationAverage(u.tags, i.tags)

def calculateCorrelationAverage(tags1, tags2):
    correlationSum = 0.0
    for (tag1, tag2) in allCombinations(tags1, tags2):
        correlationSum += correlation(tag1, tag2)
    return correlationSum / (len(tags1) + len(tags2))

def allCombinations(list1, list2):
    combinations = []
    for x in list1:
        for y in list2:
            combinations.append((x, y))
    return combinations             

但是 calculateCorrelation 在 appengine-mapreduce 中不是一个有效的 Mapper,也许这个函数甚至与 MapReduce 计算概念不兼容。然而,我需要确定...如果我拥有 appengine-mapreduce 的优势(例如自动批处理和任务链),那真的很棒。

有什么解决办法吗?

我应该定义自己的 InputReader 吗?读取两种不同类型的所有实例的新 InputReader 是否与当前的 appengine-mapreduce 实现兼容?

或者我应该尝试以下方法吗?

  • 将这两种类型的所有实体的所有键,两个两个地组合成一个新模型的实例(可能使用 MapReduce)
  • 使用映射器迭代这个新模型的实例
  • 对于每个实例,使用其中的键来获取两个不同种类的实体并计算它们之间的相关性。

最佳答案

根据 Nick Johnson 的建议,我编写了自己的 InputReader。这个阅读器从两种不同的类型中获取实体。它产生包含这些实体的所有组合的元组。在这里:

class TwoKindsInputReader(InputReader):
    _APP_PARAM = "_app"
    _KIND1_PARAM = "kind1"
    _KIND2_PARAM = "kind2"
    MAPPER_PARAMS = "mapper_params"

    def __init__(self, reader1, reader2):
        self._reader1 = reader1
        self._reader2 = reader2

    def __iter__(self):
        for u in self._reader1:
            for e in self._reader2:
                yield (u, e)

    @classmethod
    def from_json(cls, input_shard_state):
        reader1 = DatastoreInputReader.from_json(input_shard_state[cls._KIND1_PARAM])
        reader2 = DatastoreInputReader.from_json(input_shard_state[cls._KIND2_PARAM])

        return cls(reader1, reader2)

    def to_json(self):
        json_dict = {}
        json_dict[self._KIND1_PARAM] = self._reader1.to_json()
        json_dict[self._KIND2_PARAM] = self._reader2.to_json()
        return json_dict

    @classmethod
    def split_input(cls, mapper_spec):
        params = mapper_spec.params
        app = params.get(cls._APP_PARAM)
        kind1 = params.get(cls._KIND1_PARAM)
        kind2 = params.get(cls._KIND2_PARAM)
        shard_count = mapper_spec.shard_count
        shard_count_sqrt = int(math.sqrt(shard_count))

        splitted1 = DatastoreInputReader._split_input_from_params(app, kind1, params, shard_count_sqrt)
        splitted2 = DatastoreInputReader._split_input_from_params(app, kind2, params, shard_count_sqrt)
        inputs = []

        for u in splitted1:
            for e in splitted2:
                inputs.append(TwoKindsInputReader(u, e))

        #mapper_spec.shard_count = len(inputs) #uncomment this in case of "Incorrect number of shard states" (at line 408 in handlers.py)
        return inputs

    @classmethod
    def validate(cls, mapper_spec):
        return True #TODO

当您需要处理两种实体的所有组合时,应使用此代码。您也可以将其推广到两种以上。

这是 TwoKindsInputReader 的有效 mapreduce.yaml:

mapreduce:
- name: recommendationMapReduce
  mapper:
    input_reader: customInputReaders.TwoKindsInputReader
    handler: recommendation.calculateCorrelationHandler
    params:
    - name: kind1
      default: kinds.User
    - name: kind2
      default: kinds.Item
    - name: shard_count
      default: 16

关于python - Google App Engine 中不止一种数据存储类型的 MapReduce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3766154/

相关文章:

python - 如何使用 pandas 旋转分组对象

google-app-engine - 为什么 Google Developers Console 中缺少 GAE 应用程序?

node.js - 如何使用 Node js从谷歌云数据存储返回所有实体ID?

python - 如何在字符串中搜索单词并根据匹配的大小写进行不同的打印?

python - sqlalchemy 在多个列中是唯一的

google-app-engine - 在Google App Engine上上传音频

java - 错误 403 禁止访问(权限不足)与 Google Cloud Storage JSON API Java 示例

google-app-engine - 如果最近的事务引发 "special"异常,事务性 db.get() 是否可能返回过时的结果?

python - 如何为由另一个数组过滤的列中的元素设置值

java - Blobstore/FileService 写入突然 100% 失败