GAE,本地数据存储不创建 [英] GAE, Local datastore does not create
问题描述
我不知道GAE在什么情况下不易理解:($ / b>
我的servlet处理一个json字符串,然后我试图将它存储在数据存储区。
当我运行应用程序时,我得到了这个输出:
2014年1月27日6:59:04 PM com.google.appengine.api.datastore.dev.LocalDatastoreService加载
INFO:后备存储区D:\Android\IntelliJ IDEA\workspace\EyeBall\AppEngine\out\artifacts\\ \\ AppEngine_war_exploded \WEB-INF \appengine-generated\local_db.bin不存在,它将被创建。
1
2
3
4
5
7
***
***
***
***
***
8
9
尽管提到将创建local_db.bin,但是当我导航到该目录时,文件当我在浏览器中打开 http:// localhost:8080 / _ah / admin / datastore
时,在Entity Kind下拉列表中不显示任何内容。
因此,wtf发生在local_db.bin?为什么它不会生成?
任何建议,将不胜感激。感谢。
==================
更新:
我根据请求添加了我的代码。
private static final String NO_DEVICE_ID =FFFF0000;
private static final String SAMPLE_JSON ={\history \:[{\date \:null,\info \:null,\title \: \ Maybank2u.com\,\ url\:\ https://www.maybank2u.com.my/mbb/Mobile/info.do\,\visits\ :14},{\ date\ :空,\ info\ :空,\ title\ :\ Maybank2u.com\ \ url\ :\ https://www.maybank2u.com.my/mbb/Mobile/adaptInfo.do\,\ visits\:4},{\ date\:空, \ info\:空,\ title\:\ Maybank2u.com\,\ url\:\http://www.maybank2u.com.my /mbb_info/m2u/public/personalBanking.do\\",\\"visits\\":16},{\\"date\\":null,\\"info\\":null,\\"title \:\Maybank2u.com Online Financial Services \,\url \:\https://www.maybank2u.com.my/mbb/m2u/common/M2ULogin.do?行动= Login\,\ visits\:52},{\ date\:空,\ info\:NU LL,\ title\:\ BBC\,\ url\:\ http://www.bbc.co.uk/persian/\ \ visits\:16}]};
private static final String QUERY_HISTORY_DEVICE =SELECT m FROM HistoryDeviceJPA m WHERE m.userUUID =:keyword ORDER BY m.domain ASC;
private static final String QUERY_HISTORY =SELECT m FROM HistoryJPA m WHERE m.pageAddress =:keyword;
protected void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {
// displayError(response,The page does not support httpGet);
字符串deviceId = NO_DEVICE_ID;
字符串内容= SAMPLE_JSON;
System.out.println(1);
HistoryBrowser historyBrowser = parseJson(content);
if(historyBrowser == null)
return;
System.out.println(2);
列表< HistoryBrowser.BrowserInfo> historyList = historyBrowser.getHistory();
if(historyList == null)
return;
System.out.println(3);
列表< HistoryDeviceJPA> historyDeviceJPAList = new ArrayList< HistoryDeviceJPA>(historyList.size());
for(int i = 0; i< historyList.size(); i ++){
try {
HistoryBrowser.BrowserInfo browser = historyList.get(i);
HistoryDeviceJPA historyDeviceJPA = new HistoryDeviceJPA();
historyDeviceJPA.setUserUUID(deviceId);
historyDeviceJPA.setDomain(getDomainName(browser.getUrl()));
historyDeviceJPA.setPageAddress(browser.getUrl());
historyDeviceJPA.setPageTitle(browser.getTitle());
historyDeviceJPA.setPageVisits(browser.getVisits());
historyDeviceJPAList.add(historyDeviceJPA);
} catch(URISyntaxException e){
System.out.println(e.getMessage());
}
}
System.out.println(4);
//从数据存储获取设备的历史记录
EntityManager em = EMF.get()。createEntityManager();
Query q = em.createQuery(QUERY_HISTORY_DEVICE).setParameter(keyword,deviceId);
@SuppressWarnings(unchecked)
List< HistoryDeviceJPA> dbList =(List< HistoryDeviceJPA>)q.getResultList();
System.out.println(5);
//如果没有结果(显示该设备没有记录)
if(dbList == null)
addHistoryDeviceJPAToDs(historyDeviceJPAList);
else {
System.out.println(7);
//找到数据存储中的每个项目,并根据需要替换它们
//如果当前页面访问不如前一次访问等于不执行任何操作(删除项目形式historyDeviceJPAList)
outerLoop:
for(int i = 0; i< historyDeviceJPAList.size(); i ++){
HistoryDeviceJPA deviceItem = historyDeviceJPAList.get(i);
System.out.println(***);
for(int j = 0; j HistoryDeviceJPA dbItem = dbList.get(j);
$ b $ if(deviceItem.getPageVisits()> dbItem.getPageVisits()){
(deviceItem.getPageAddress()。equalsIgnoreCase(dbItem.getPageAddress())){
if long diff = deviceItem.getPageVisits() - dbItem.getPageVisits();
dbItem.setPageVisits(deviceItem.getPageVisits());
HistoryJPA historyJPA = findHistoryJPA(dbItem.getPageAddress());
historyJPA.setPageVisits(historyJPA.getPageVisits()+ diff);
//更新数据存储
addHistoryDeviceJPAToDs(dbItem);
addHistoryJPAToDs(historyJPA);
//不检查j列表中的其他项目
break outerLoop;
}
}
}
}
System.out.println(8);
}
System.out.println(9);
// http://www.sohailaziz.com/2012/06/scheduling-activities-services-and.html
// https://dev.twitter.com/docs/api/1.1
// https://developers.google.com/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata?csw=1#Updating_an_Object
// http://en.wikibooks.org/wiki / Java_Persistence / Inheritance
}
和6在这里:
private void addHistoryDeviceJPAToDs(List< HistoryDeviceJPA> list){
System.out.println(6);
EntityManager em = EMF.get()。createEntityManager();
尝试{
for(int i = 0; i< list.size(); i ++){
System.out.println(=>+ i + - + list.get(ⅰ)的ToString());
em.getTransaction()。begin();
em.persist(list.get(i));
em.getTransaction()。commit();
}
} finally {
em.close();
}
}
调试后我发现问题在这一行:
List< HistoryDeviceJPA> dbList =(List< HistoryDeviceJPA>)q.getResultList();
if(dbList == null)
addHistoryDeviceJPAToDs(historyDeviceJPAList);
'dbList'永远不会为空,如果数据存储区中没有任何内容,则其大小为0。这就是为什么从未调用 addHistoryDeviceJPAToDs
方法的原因。
列表< HistoryDeviceJPA>通过更改代码来解决以下问题并创建本地数据库。 dbList =(List< HistoryDeviceJPA>)q.getResultList();
if(dbList == null)
return;
System.out.println(5);
//如果没有结果(显示该设备没有记录)
if(dbList.size()== 0)
addHistoryDeviceJPAToDs(historyDeviceJPAList);
I have no idea in what extend GAE is not easy to understand :(
My servlet manipulate a json string and then I'm trying to store it in datastore. When I run the application I'm getting this output:
Jan 27, 2014 6:59:04 PM com.google.appengine.api.datastore.dev.LocalDatastoreService load
INFO: The backing store, D:\Android\IntelliJ IDEA\workspace\EyeBall\AppEngine\out\artifacts\AppEngine_war_exploded\WEB-INF\appengine-generated\local_db.bin, does not exist. It will be created.
1
2
3
4
5
7
***
***
***
***
***
8
9
Although it's mentioned that local_db.bin will be created but when I navigate to that directory the file is not there. Also, when I open http://localhost:8080/_ah/admin/datastore
in browser nothing displays in Entity Kind drop down list.
So wtf happene to local_db.bin? Why it doesn't generates? any suggestion would be appreciated. thanks.
================== UPDATE: I added my code based on request.
private static final String NO_DEVICE_ID = "FFFF0000";
private static final String SAMPLE_JSON = "{\"history\":[{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com\",\"url\":\"https://www.maybank2u.com.my/mbb/Mobile/info.do\",\"visits\":14},{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com\",\"url\":\"https://www.maybank2u.com.my/mbb/Mobile/adaptInfo.do\",\"visits\":4},{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com\",\"url\":\"http://www.maybank2u.com.my/mbb_info/m2u/public/personalBanking.do\",\"visits\":16},{\"date\":null,\"info\":null,\"title\":\"Maybank2u.com Online Financial Services\",\"url\":\"https://www.maybank2u.com.my/mbb/m2u/common/M2ULogin.do?action=Login\",\"visits\":52},{\"date\":null,\"info\":null,\"title\":\"BBC\",\"url\":\"http://www.bbc.co.uk/persian/\",\"visits\":16}]}";
private static final String QUERY_HISTORY_DEVICE = "SELECT m FROM HistoryDeviceJPA m WHERE m.userUUID = :keyword ORDER BY m.domain ASC";
private static final String QUERY_HISTORY = "SELECT m FROM HistoryJPA m WHERE m.pageAddress = :keyword";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// displayError(response, "The page doesn't support httpGet");
String deviceId = NO_DEVICE_ID;
String content = SAMPLE_JSON;
System.out.println("1");
HistoryBrowser historyBrowser = parseJson(content);
if(historyBrowser == null)
return;
System.out.println("2");
List<HistoryBrowser.BrowserInfo> historyList = historyBrowser.getHistory();
if(historyList == null)
return;
System.out.println("3");
List<HistoryDeviceJPA> historyDeviceJPAList = new ArrayList<HistoryDeviceJPA>(historyList.size());
for(int i=0; i<historyList.size(); i++) {
try {
HistoryBrowser.BrowserInfo browser = historyList.get(i);
HistoryDeviceJPA historyDeviceJPA = new HistoryDeviceJPA();
historyDeviceJPA.setUserUUID(deviceId);
historyDeviceJPA.setDomain(getDomainName(browser.getUrl()));
historyDeviceJPA.setPageAddress(browser.getUrl());
historyDeviceJPA.setPageTitle(browser.getTitle());
historyDeviceJPA.setPageVisits(browser.getVisits());
historyDeviceJPAList.add(historyDeviceJPA);
} catch (URISyntaxException e) {
System.out.println(e.getMessage());
}
}
System.out.println("4");
// get history of device from data store
EntityManager em = EMF.get().createEntityManager();
Query q = em.createQuery(QUERY_HISTORY_DEVICE).setParameter("keyword", deviceId);
@SuppressWarnings("unchecked")
List<HistoryDeviceJPA> dbList = (List<HistoryDeviceJPA>) q.getResultList();
System.out.println("5");
// If there is no result (shows there is no record for that device)
if(dbList == null)
addHistoryDeviceJPAToDs(historyDeviceJPAList);
else {
System.out.println("7");
// find each item in datastore and replace them if needed
// if current page visit is less ot equal than previous visit don't do anything (remove item form historyDeviceJPAList)
outerLoop:
for(int i=0; i<historyDeviceJPAList.size(); i++) {
HistoryDeviceJPA deviceItem = historyDeviceJPAList.get(i);
System.out.println("***");
for(int j=0; j<dbList.size(); j++) {
HistoryDeviceJPA dbItem = dbList.get(j);
if(deviceItem.getPageAddress().equalsIgnoreCase(dbItem.getPageAddress())) {
if(deviceItem.getPageVisits() > dbItem.getPageVisits()) {
long diff = deviceItem.getPageVisits() - dbItem.getPageVisits();
dbItem.setPageVisits(deviceItem.getPageVisits());
HistoryJPA historyJPA = findHistoryJPA(dbItem.getPageAddress());
historyJPA.setPageVisits(historyJPA.getPageVisits() + diff);
// update datastore
addHistoryDeviceJPAToDs(dbItem);
addHistoryJPAToDs(historyJPA);
// don't check other items of j list
break outerLoop;
}
}
}
}
System.out.println("8");
}
System.out.println("9");
// http://www.sohailaziz.com/2012/06/scheduling-activities-services-and.html
// https://dev.twitter.com/docs/api/1.1
// https://developers.google.com/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata?csw=1#Updating_an_Object
// http://en.wikibooks.org/wiki/Java_Persistence/Inheritance
}
and 6 is here:
private void addHistoryDeviceJPAToDs(List<HistoryDeviceJPA> list) {
System.out.println("6");
EntityManager em = EMF.get().createEntityManager();
try {
for (int i=0; i<list.size(); i++) {
System.out.println("=> " + i + " - " + list.get(i).toString());
em.getTransaction().begin();
em.persist(list.get(i));
em.getTransaction().commit();
}
} finally {
em.close();
}
}
after debug I found the problem is in this line:
List<HistoryDeviceJPA> dbList = (List<HistoryDeviceJPA>) q.getResultList();
if(dbList == null)
addHistoryDeviceJPAToDs(historyDeviceJPAList);
'dbList' is never null and it's size is 0 if there is nothing in datastore. That's why addHistoryDeviceJPAToDs
method never invoked. By changing the code to following problem solved and local db created.
List<HistoryDeviceJPA> dbList = (List<HistoryDeviceJPA>) q.getResultList();
if(dbList == null)
return;
System.out.println("5");
// If there is no result (shows there is no record for that device)
if(dbList.size() == 0)
addHistoryDeviceJPAToDs(historyDeviceJPAList);
这篇关于GAE,本地数据存储不创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!