App Engine: How to “reset” the datastore?

2019-03-09 01:21发布

Well, I'm developing in App Engine (Java) and after a lot of tries and deployments, I need to reset the datastore. There is a lot of random data I added to test performance, and besides that the entities changed a lot, so I need to delete all: data, tables, indexes.

How can I do that?

15条回答
我命由我不由天
2楼-- · 2019-03-09 01:34

When working locally, on Windows 7 the file is user\UserName\AppData\Local\Temp\dev_appserver.datastore

查看更多
贪生不怕死
3楼-- · 2019-03-09 01:37

There is no built in command equivalent to DROP TABLE or TRUNCATE TABLE in SQL. You just need to create a "delete everything" page in your app, then repeatedly call that page via a script. In that page, you want to delete as many entities as you can yet still reasonably expect to finish before the request times out. The exact code depends on whether you're using JDO/JPA or the low level API. (the low level API will be faster because you can use batch operations.)

This previous SO question is pretty much the same, only for Python

查看更多
你好瞎i
4楼-- · 2019-03-09 01:41

Delete all (or a part) of your application’s data is now part of the Admin console

To enable this functionality, simply enable the following builtin in your app.yaml file:

builtins:
- datastore_admin: on

Adding these lines to app.yaml enables the “Datastore Admin” page in your app’s Admin Console

查看更多
神经病院院长
5楼-- · 2019-03-09 01:42

sorry to be so late on this, but I was just trying to do the same myself...

I logged into my account (appengine.google.com) and found the option to browse the datastore through an admin utility (datastore/dataviewer)... that allows create/update/delete.

查看更多
戒情不戒烟
6楼-- · 2019-03-09 01:44

Just execute a query without a filter to fetch all the entities, and delete them one by one.

import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

public class MyServlet extends HttpServlet {
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        DatastoreService datastore = 
                    DatastoreServiceFactory.getDatastoreService();

    Query mydeleteq = new Query();
    PreparedQuery pq = datastore.prepare(mydeleteq);
    for (Entity result : pq.asIterable()) {
        datastore.delete(result.getKey());      
    }   
}
查看更多
家丑人穷心不美
7楼-- · 2019-03-09 01:44

I was using app engine with Google Coursebuilder and had to use this command to clear the datastore:

python dev_appserver.py --clear_datastore /path/to/app
查看更多
登录 后发表回答