在我的应用程序的 Servlet 中,我想控制其数据存储区是否为空(我第一次运行 servlet 时它将为空,但现在情况并非如此,因为我已经填充了它并使其持久化)在继续其余代码之前。在此Datastore Docs我发现了一种查询数据存储并获取不同类型统计信息的方法。 但是在执行时:
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Entity globalStat = datastore.prepare(new Query("__Stat_Total__")).asSingleEntity();
Long totalBytes = (Long) globalStat.getProperty("bytes");
Long totalEntities = (Long) globalStat.getProperty("count");
我得到一个NullPointerException
当我尝试存储两个 Long 变量时。
为什么是Entity globalStat
查询后对象为空?
编辑 1:附加信息
我正在尝试在使用 List<Entity>
填充数据存储区统计信息后获取数据存储区统计信息。实体数量:
public class MyServlet extends HttpServlet {
ArrayList<Tour> m_tours = new ArrayList<Tour>();
Key tourKey;
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//.... some code
private DatastoreService populateDatastore(){
//... some other code ...
List<Entity> List = Arrays.asList(tour,tour1,tour2,tour3,
tour4,tour5,tour6,tour7,tour8);
datastore.put(List);
Entity globalStat = datastore.prepare(
new Query("__Stat_Total__")).asSingleEntity();
try{
Long totalEntities = (Long) globalStat.getProperty("count");
Long totalBytes = (Long) globalStat.getProperty("bytes");
}catch (NullPointerException e){
e.printStackTrace();
}`
}
}
这表明实体全局统计对象在调试工具中仍然显示为空:
怎么会new Query("__Stat_Total__")).asSingleEntity();
当我刚刚用多个实体填充数据存储区时不会产生任何结果?
最佳答案
ANSWER: as reported here: Using App Engine Datastore Low Level API with Java, "Stat_Total" and "Stat_Kind" don't work in a local development server. They only work when deployed in
App Engine Server
.
关于java - 在 App Engine Java Servlet 中查询数据存储区统计信息时出现 NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29416865/