对DataStore异常执行查询

时间:2013-08-31 10:36:56

标签: java google-app-engine google-cloud-datastore

这是我第一次使用数据存储区,也许还有一些我不理解的东西。 我会存储一些包含一些数据的实体,然后允许用户使用我的webservice服务来检索它。 这是实现存储服务并从数据存储区获取数据的类:

@WebService
public class Functions {
private final String KEY_NAME = "keyName";
private final String KEY_TYPE = "storedData";
private final String ENTITY_TYPE = "record";
private final String TEMP_ROW = "temperatura";
private final String HUM_ROW = "umidita";
private final String DATE_ROW = "date";
private DatastoreService dataStore;
private Key dataKey = KeyFactory.createKey(KEY_TYPE, KEY_NAME);
private final Query query = new Query(ENTITY_TYPE, dataKey).addSort(DATE_ROW,
        Query.SortDirection.ASCENDING);

@WebMethod
public List<String[]> getData() {
    dataStore = DatastoreServiceFactory.getDatastoreService();
    List<Entity> list = dataStore.prepare(query).asList(FetchOptions.Builder.withLimit(100));
    if (!list.isEmpty()) {
        List<String[]> res=new ArrayList<String[]>();
        String[] to_add;
        for (Entity current : list) {
            to_add=new String[3];
            to_add[0]=String.valueOf(current.getProperty(TEMP_ROW));
            to_add[1]=String.valueOf(current.getProperty(HUM_ROW));
            to_add[2]=String.valueOf(current.getProperty(DATE_ROW));

            res.add(to_add);
        }
        return res;
    }

    return null;
}

@WebMethod
public int sendData(String temperatura, String umidita) {
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyy HH:mm:ss");
    Date date = new Date();

    Entity record = new Entity(ENTITY_TYPE, dataKey);
    record.setProperty(TEMP_ROW, temperatura);
    record.setProperty(HUM_ROW, umidita);
    record.setProperty(DATE_ROW, date);
    dataStore = DatastoreServiceFactory.getDatastoreService();
    dataStore.put(record);  
    return 1;
}

}

现在调用getData服务时,我有这个错误:

/dataserver
com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found.
The suggested index for this query is:
<datastore-index kind="record" ancestor="true" source="manual">
    <property name="date" direction="asc"/>
</datastore-index>

    at com.google.appengine.api.datastore.DatastoreApiHelper.translateError(DatastoreApiHelper.java:39)
at com.google.appengine.api.datastore.DatastoreApiHelper$1.convertException(DatastoreApiHelper.java:70)
at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:94)
at com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:86)
at com.google.appengine.api.datastore.FutureHelper.getInternal(FutureHelper.java:71)
at com.google.appengine.api.datastore.FutureHelper.quietGet(FutureHelper.java:32)
at com.google.appengine.api.datastore.QueryResultsSourceImpl.peekQueryResultAndIfFirstRecordIndexList(QueryResultsSourceImpl.java:161)
at com.google.appengine.api.datastore.QueryResultsSourceImpl.loadMoreEntities(QueryResultsSourceImpl.java:104)
at com.google.appengine.api.datastore.QueryResultIteratorImpl.ensureLoaded(QueryResultIteratorImpl.java:155)
at com.google.appengine.api.datastore.QueryResultIteratorImpl.nextList(QueryResultIteratorImpl.java:110)
at com.google.appengine.api.datastore.LazyList.forceResolveToIndex(LazyList.java:93)
at com.google.appengine.api.datastore.LazyList.resolveToIndex(LazyList.java:73)
at com.google.appengine.api.datastore.LazyList.resolveToIndex(LazyList.java:56)
at com.google.appengine.api.datastore.LazyList.isEmpty(LazyList.java:260)
at com.example.Functions.getData(Functions.java:44)
at com.example.FunctionsAdapter.getData(FunctionsAdapter.java:14)
at com.example.FunctionsHandler.handleGetDataRequest(FunctionsHandler.java:72)
at com.example.FunctionsHandler.handleSOAPRequest(FunctionsHandler.java:45)
at com.example.DataServerServlet.doPost(DataServerServlet.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:266)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:146)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:439)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:435)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:442)
at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:186)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:306)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:298)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:439)
at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251)
at java.lang.Thread.run(Thread.java:722)
我的代码上的

异常在线上播放:

if (!list.isEmpty())

客户代码:

@SuppressWarnings("serial")
public class SOAPClientProvaServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    String[] temp = {"22.3", "25.4", "26.3"};
    String[] hum={"44%", "30%", "50%"};
    resp.setContentType("text/html");
    Functions functions = new FunctionsService().getFunctionsPort();
    for(int i=0;i<temp.length;i++){
        functions.sendData(temp[1], hum[1]);
    }       
    ArrayList<StringArray> list = (ArrayList<StringArray>) functions.getData();

    PrintWriter out = resp.getWriter();
    for(int i=0; i<list.size();i++){
        out.println(list.get(i));
    }
}
}

出了什么问题?

1 个答案:

答案 0 :(得分:2)

请注意实际错误com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found

您正在尝试执行需要不存在的索引的查询。该错误甚至暗示该怎么办。

The suggested index for this query is:
<datastore-index kind="record" ancestor="true" source="manual">
    <property name="date" direction="asc"/>
</datastore-index>

阅读堆栈跟踪并尝试了解它告诉您的内容是值得的。

为什么在if (!list.isEmpty())行上收到错误 - 因为isEmpty()调用正在强制执行查询。