从应用程序引擎中删除实体

发布于 2024-10-16 19:49:43 字数 747 浏览 7 评论 0原文

我打算从我的 python 应用程序引擎服务器中删除条目,如下所示:

    try:
        while True:
            q = db.GqlQuery("SELECT __key__ FROM SampleData")
            assert q.count()
            db.delete(q.fetch(400))
            time.sleep(0.2)
        except Exception, e:
        self.response.out.write(repr(e)+'\n')
        pass

        try:
        while True:
            q = db.GqlQuery("SELECT __key__ FROM UserData")
            assert q.count()
            db.delete(q.fetch(400))
            time.sleep(0.2)
        except Exception, e:
        self.response.out.write(repr(e)+'\n')
        pass

..但它看起来很丑陋,我一直怀疑它并不完全可靠。有没有什么方法可以更好地删除某些类型的条目,而不是分别创建一个 while 循环?

更新:我的一个限制是我通过 cron 作业定期运行它,所以不希望手动执行它(例如通过管理控制台)。

I'm going about deleting entries from my python app engine server like this:

    try:
        while True:
            q = db.GqlQuery("SELECT __key__ FROM SampleData")
            assert q.count()
            db.delete(q.fetch(400))
            time.sleep(0.2)
        except Exception, e:
        self.response.out.write(repr(e)+'\n')
        pass

        try:
        while True:
            q = db.GqlQuery("SELECT __key__ FROM UserData")
            assert q.count()
            db.delete(q.fetch(400))
            time.sleep(0.2)
        except Exception, e:
        self.response.out.write(repr(e)+'\n')
        pass

..but it just seems to ugly and I keep suspecting it's not entirely reliable. Is there some way to do this better to delete entries of some number of types instead of making one of each of these while loops?

Update: One restriction I have is that I am running this periodically via a cron job, so do not wish to be doing it manually (i.e. via the admin console for instance).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌梦深 2024-10-23 19:49:43

您可以使用管理控制台的数据存储区管理选项卡删除同类的所有实体,有关详细信息,请参阅此处:

http://code.google.com/appengine/docs/adminconsole/datastoreadmin.html#Deleting_Entities_in_Bulk

You can use the datastore admin tab of the admin console to delete all entities of a kind, see here for details:

http://code.google.com/appengine/docs/adminconsole/datastoreadmin.html#Deleting_Entities_in_Bulk

橘虞初梦 2024-10-23 19:49:43

一些改进:

  1. 您不需要在每个批次后睡眠
  2. 您应该使用任务队列,并准备好链接另一个任务,如果您在截止日期到来时没有完成。
  3. 你应该使用游标。如果不这样做,后续查询必须跳过您已删除的所有“逻辑删除”行,才能访问仍然存在的行。

如果您要删除大部分或全部同类内容,您可能需要改用mapreduce 库。

A few improvements:

  1. You don't need to sleep after each batch
  2. You should use the task queue, and be prepared to chain another task if you don't finish by the time the deadline hits.
  3. You should use cursors. If you don't, subsequent queries have to skip over all the 'tombstoned' rows you've already deleted in order to get to the still existing rows.

If you're deleting most or all of a kind, you may want to use the mapreduce library instead.

转角预定愛 2024-10-23 19:49:43

如果您想要删除大量数据,则可能需要使用延迟 Google 提供的库 - 让 cron 作业启动延迟任务,这可以批量删除您的对象:

class DeleteMapper(mapper.Mapper):
    KIND = MyKindOfObject

    # Delete all objects of type MyKindOfObject.
    def map(self, key):
        todelete = [key]
        return ([], todelete)

您可以使用较新的 mapreduce 库,但是,我没有给你的例子。

If you want to delete a lot of data, you might want to use the deferred library provided by Google -- have the cron job kick off a deferred task, which can delete your objects in batches:

class DeleteMapper(mapper.Mapper):
    KIND = MyKindOfObject

    # Delete all objects of type MyKindOfObject.
    def map(self, key):
        todelete = [key]
        return ([], todelete)

You can likely do similar with the newer mapreduce library, however, I don't have an example for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文