从应用程序引擎中删除实体
我打算从我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用管理控制台的数据存储区管理选项卡删除同类的所有实体,有关详细信息,请参阅此处:
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
一些改进:
如果您要删除大部分或全部同类内容,您可能需要改用mapreduce 库。
A few improvements:
If you're deleting most or all of a kind, you may want to use the mapreduce library instead.
如果您想要删除大量数据,则可能需要使用延迟 Google 提供的库 - 让 cron 作业启动延迟任务,这可以批量删除您的对象:
您可以使用较新的 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:
You can likely do similar with the newer mapreduce library, however, I don't have an example for you.