在没有模型的数据存储区中进行查询 [英] query in datastore without a model

查看:63
本文介绍了在没有模型的数据存储区中进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在appengine中创建一个应用程序,该应用程序搜索键列表,然后使用此列表从数据存储区中删除这些记录,该服务必须是通用服务,因此我不能仅使用模型搜索可以通过appengine功能来做到这一点吗?

I am trying to create an application in appengine that searches for a list of keys and then I use this list to delete these records from the datastore, this service has to be a generic service so I could not use a model just search by the name of kind, it is possible to do this through appengine features?

在我的代码下面,但这需要我有一个模型.

Below my code, but it requires that I have a model.

导入httplib导入日志从datetime导入datetime,timedelta

import httplib import logging from datetime import datetime, timedelta

import webapp2
from google.appengine.api import urlfetch
from google.appengine.ext import ndb

DEFAULT_PAGE_SIZE = 100000
DATE_PATTERN = "%Y-%m-%dT%H:%M:%S"


def get_date(amount):
    date = datetime.today() - timedelta(days=30 * amount)
    date = date.replace(hour=0, minute=0, second=0)
    return date


class Purge(webapp2.RequestHandler):

    def get(self):
        kind = self.request.get('kind')
        datefield = self.request.get('datefield')
        amount = self.request.get('amount', default_value=3)
        date = get_date(amount)

        logging.info('Executando purge para Entity {}, mantendo periodo de {} meses.'.format(kind, amount))

        # cria a query
        query = ndb.Query(kind=kind, namespace='development')

        logging.info('Setando o filtro [{} <= {}]'.format(datefield, date.strftime(DATE_PATTERN)))

        # cria um filtro
        query.filter(ndb.DateTimeProperty(datefield) <= date)

        query.fetch_page(DEFAULT_PAGE_SIZE)

        while True:
            # executa a consulta
            keys = query.fetch(keys_only=True)

            logging.info('Encontrados {} {} a serem exluidos'.format(len(keys), kind))

            # exclui usando as keys
            ndb.delete_multi(keys)

            if len(keys) < DEFAULT_PAGE_SIZE:
                logging.info('Nao existem mais registros a serem excluidos')
                break


app = webapp2.WSGIApplication(
    [
        ('/cloud-datastore-purge', Purge),
    ], debug=True)

踪迹

Traceback (most recent call last):
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/p~telefonica-dev-155211/cloud-datastore-purge-python:20180629t150020.410785498982375644/purge.py", line 38, in get
    query.fetch_page(_DEFAULT_PAGE_SIZE)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/utils.py", line 160, in positional_wrapper
    return wrapped(*args, **kwds)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/query.py", line 1362, in fetch_page
    return self.fetch_page_async(page_size, **q_options).get_result()   
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 383, in get_result
    self.check_success()
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 427, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/query.py", line 1380, in _fetch_page_async
    while (yield it.has_next_async()):
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 427, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/query.py", line 1793, in has_next_async
    yield self._fut
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/context.py", line 890, in helper
    batch, i, ent = yield inq.getq()
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/query.py", line 969, in run_to_queue
    batch = yield rpc
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 513, in _on_rpc_completion
    result = rpc.get_result()
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 613, in get_result
    return self.__get_result_hook(self)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/datastore/datastore_query.py", line 2951, in __query_result_hook
    self.__results = self._process_results(query_result.result_list())
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/datastore/datastore_query.py", line 2984, in _process_results
    for result in results]
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 194, in pb_to_query_result
    return self.pb_to_entity(pb)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 690, in pb_to_entity
    modelclass = Model._lookup_model(kind, self.default_model)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3101, in _lookup_model
    kind)
KindError: No model class found for kind 'Test'. Did you forget to import it?

推荐答案

在设置fetch_page的行上发现了问题.

The problem was found on the line where fetch_page is set.

删除此行

query.fetch_page(DEFAULT_PAGE_SIZE)

为此

keys = query.fetch(limit=_DEFAULT_LIMIT, keys_only=True)

这篇关于在没有模型的数据存储区中进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆