Google AppEngine - 大数据存储区读取

时间:2012-08-23 19:55:34

标签: google-app-engine google-cloud-datastore

我需要阅读Google AppEngine数据存储区中的所有条目才能进行一些初始化工作。有很多实体(目前为80k)并且这种情况继续增长。我开始达到30秒的数据存储查询超时限制。

是否有关于如何在数据存储区中对这些类型的大型读取进行分片的最佳实践?有什么例子吗?

2 个答案:

答案 0 :(得分:3)

您可以通过多种方式解决这个问题:

  1. Task Queue执行您的代码,该代码有10分钟超时而非30秒(实际上更像是60秒)。最简单的方法是通过DeferredTask

    警告:DeferredTask必须是可序列化的,因此很难传递复杂的数据。也不要把它变成内心阶级。

  2. 请参阅backends。后端实例提供的请求没有时间限制。

  3. 最后,如果你需要分解一个大任务并且并行执行而不是查看mapreduce

答案 1 :(得分:0)

StackExchange上的这个答案对我很有帮助:

Expired queries and appengine

我不得不稍微修改它以便为我工作:

def loop_over_objects_in_batches(batch_size, object_class, callback):

    num_els = object_class.count() 
    num_loops = num_els / batch_size
    remainder = num_els - num_loops * batch_size
    logging.info("Calling batched loop with batch_size: %d, num_els: %s, num_loops: %s, remainder: %s, object_class: %s, callback: %s," % (batch_size, num_els, num_loops, remainder, object_class, callback))    
    offset = 0
    while offset < num_loops * batch_size:
        logging.info("Processing batch (%d:%d)" % (offset, offset+batch_size))
        query = object_class[offset:offset + batch_size]
        for q in query:
            callback(q)

        offset = offset + batch_size

    if remainder:
        logging.info("Processing remainder batch (%d:%d)" % (offset, num_els))
        query = object_class[offset:num_els]
        for q in query:
            callback(q)