2013-08-31 67 views
0

這是我第一次使用數據存儲,也許有些東西我不明白。 我會存儲一些包含一些數據的實體,然後允許用戶使用我的web服務的服務來檢索它。 這對存儲類實現服務和數據存儲中獲取數據:在DataStore上執行查詢異常

@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) 

例外,在我的代碼上線trown:

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)); 
    } 
} 
} 

有什麼不對?

回答

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()調用迫使查詢執行。

+0

我已閱讀並理解錯誤意味着什麼,但我不知道爲什麼會出現錯誤! 在我的客戶端中,我首先調用服務來在數據存儲區內寫入一些數據,然後調用服務來檢索它。我剛剛修改了開發者教程的源代碼... 我在主要消息上粘貼客戶端調用方法的方式 – giozh

+0

如果索引尚未構建爲與該查詢匹配,那麼您將得到該錯誤。添加在錯誤中定義的索引。 –

+0

也許可能是客戶端無法執行sendData方法的問題,那麼將不會創建具有該名稱的索引? – giozh