有人知道如何删除Google App Engine中的所有数据存储吗?
最佳方法是远程API方法,正如来自Google的App Engine工程师Nick所建议的那样,所以请相信他。
这并不难做,最新的1.2.5 SDK提供了remote_shell_api.py。所以去下载新的SDK,然后按照以下步骤操作:
在命令行中连接远程服务器:remote_shell_api.py yourapp /remote_api
Shell会要求您输入登录信息,如果授权,将为您创建一个Python shell。您需要在app.yaml中设置/remote_api的URL处理程序。
获取您想要删除的实体,代码看起来像这样:
from models import Entry
query = Entry.all(keys_only=True)
entries =query.fetch(1000)
db.delete(entries)
\# This could bulk delete 1000 entities a time
更新于2013年10月28日:
remote_shell_api.py
已被remote_api_shell.py
所取代,您应该使用remote_api_shell.py -s your_app_id.appspot.com
进行连接,根据文档。
有一个新的实验性功能Datastore Admin,在应用程序设置中启用后,您可以通过Web界面批量删除和备份数据存储。
from mapreduce import operation as op
def process(entity):
yield op.db.Delete(entity)
@Override
public void map(Key key, Entity value, Context context) {
log.info("Adding key to deletion pool: " + key);
DatastoreMutationPool mutationPool = this.getAppEngineContext(context)
.getMutationPool();
mutationPool.delete(value.getKey());
}
编辑:
自SDK 1.3.8版本以来,有一个数据存储管理员功能可用于此目的。
在运行服务器时,您可以清除开发服务器数据存储器:
/path/to/dev_appserver.py --clear_datastore=yes myapp
您也可以使用-c
缩写替代--clear_datastore
。
您可以查看此处的文档了解更多信息。
/path/to/google_appengine/dev_appserver.py --clear_datastore yes myappname/
(注意 'yes')。 - Melllvar如果你拥有大量的数据需要删除,那么你需要使用脚本来完成这项任务。通过 remote_api ,你可以轻松地从客户端清除 datastore 数据库内容。
请前往Datastore管理员,选择您想删除的实体类型,然后单击删除。Mapreduce将负责删除!
您可以使用多种方法从App Engine的Datastore中删除条目:
首先,考虑是否真的需要删除条目。这很耗费资源,也许不删除它们会更便宜。
您可以使用Datastore Admin手动删除所有条目。
您可以使用远程API交互式地删除条目。
您可以使用几行代码以编程方式删除条目。
您可以使用任务队列和游标批量删除它们。
或者您可以使用Mapreduce来获得更强大和更华丽的功能。
以下每种方法都在以下博客文章中解释: http://www.shiftedup.com/2015/03/28/how-to-bulk-delete-entries-in-app-engine-datastore
希望对您有所帮助!
import urllib
import urllib2
urllib2.urlopen('http://localhost:8080/_ah/admin/interactive/execute',
data = urllib.urlencode({'code' : 'from google.appengine.ext import db\n' +
'db.delete(db.Query())'}))
对于Python,1.3.8版本包含了一个实验性的内置管理员。他们说:“在您的app.yaml文件中启用以下内置功能:”
builtins:
- datastore_admin: on
我从 http://code.google.com/appengine/articles/remote_api.html 获取了此内容。
首先,您需要定义一个交互式的appengine控制台。因此,创建一个名为appengine_console.py的文件并输入以下内容:
#!/usr/bin/python
import code
import getpass
import sys
# These are for my OSX installation. Change it to match your google_appengine paths. sys.path.append("/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine")
sys.path.append("/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/yaml/lib")
from google.appengine.ext.remote_api import remote_api_stub
from google.appengine.ext import db
def auth_func():
return raw_input('Username:'), getpass.getpass('Password:')
if len(sys.argv) < 2:
print "Usage: %s app_id [host]" % (sys.argv[0],)
app_id = sys.argv[1]
if len(sys.argv) > 2:
host = sys.argv[2]
else:
host = '%s.appspot.com' % app_id
remote_api_stub.ConfigureRemoteDatastore(app_id, '/remote_api', auth_func, host)
code.interact('App Engine interactive console for %s' % (app_id,), None, locals())
一旦这个基类创建好了,就可以创建Mapper类。我创建了一个名为utils.py的新文件,并添加了以下内容:
class Mapper(object):
# Subclasses should replace this with a model class (eg, model.Person).
KIND = None
# Subclasses can replace this with a list of (property, value) tuples to filter by.
FILTERS = []
def map(self, entity):
"""Updates a single entity.
Implementers should return a tuple containing two iterables (to_update, to_delete).
"""
return ([], [])
def get_query(self):
"""Returns a query over the specified kind, with any appropriate filters applied."""
q = self.KIND.all()
for prop, value in self.FILTERS:
q.filter("%s =" % prop, value)
q.order("__key__")
return q
def run(self, batch_size=100):
"""Executes the map procedure over all matching entities."""
q = self.get_query()
entities = q.fetch(batch_size)
while entities:
to_put = []
to_delete = []
for entity in entities:
map_updates, map_deletes = self.map(entity)
to_put.extend(map_updates)
to_delete.extend(map_deletes)
if to_put:
db.put(to_put)
if to_delete:
db.delete(to_delete)
q = self.get_query()
q.filter("__key__ >", entities[-1].key())
entities = q.fetch(batch_size)
Mapper是一个抽象类,允许您迭代给定类型的每个实体,无论是提取它们的数据还是修改它们并将更新后的实体存储回数据存储区。
现在,启动您的App Engine交互式控制台:
$python appengine_console.py <app_id_here>
这应该会启动交互式控制台。在其中创建一个 Model 的子类:
from utils import Mapper
# import your model class here
class MyModelDeleter(Mapper):
KIND = <model_name_here>
def map(self, entity):
return ([], [entity])
最后,在您的交互式控制台中运行它:
mapper = MyModelDeleter() mapper.run()
就这样!
/path/to/google_appengine/dev_appserver.py --clear_datastore yes myappname/
,其中myappname是包含应用程序app.yaml文件的目录。您需要切换到此目录路径。 来源:Steven Almeroth和Melllvar的答案 - gsinha