2013-08-26 48 views
0

我正在使用java。這是我的代碼谷歌雲數據存儲runquery得到日期變量?

RunQueryResponse response = dataset.runQuery("project_name", queryrequest).execute(); 

and response.tostring()。我有我想要的所有查詢,但有很多。 如何獲得每個字段的單個值。就像把它放在一個數組中,或者我們可以用for循環或interator調用。 感謝

----添加代碼-------- 這裏我的一些代碼:

Iterator<EntityResult> entity_interator = response.getBatch().getEntityResults().iterator(); 
Map<String, Property> entity; 
while(entity_interator.hasNext()){ 
    entity = entity_interator.next().getEntity().getProperties(); 
    String first = entity.get("First").toString(); 
    String last = entity.get("Last").toString(); 
    String time = entity.get("Time").toString();   
    System.out.println(first); 
    System.out.println(last); 
    System.out.println(time); 
} 

和響應:

{"values":[{"stringValue":"first name"}]} 
{"values":[{"stringValue":"last name"}]} 
{"values":[{"dateTimeValue":"2013-08-28T08:21:58.498Z"}]} 

我怎樣才能得到時間日期時間變量和名字和姓氏與出{{價值觀:[{「stringValue」:「和所有垃圾東西。

回答

0

每個Property可能包含一個或多個Value對象,並且每個Value可以包含幾種不同值類型之一(每種類型都有其自己的字段)。如果你的屬性是單值,你可以只取前一個從列表:

Iterator<EntityResult> entity_interator = response.getBatch().getEntityResults().iterator(); 
Map<String, Property> entity; 
while(entity_interator.hasNext()){ 
    entity = entity_interator.next().getEntity().getProperties(); 

    String first = entity.get("First").getValues().get(0).getStringValue(); 
    String last = entity.get("Last").getValues().get(0).getStringValue(); 
    DateTime dateTime = entity.get("Time").getValues().get(0).getDateTimeValue(); 
    Date time = new Date(dateTime.getValue()); 

    System.out.println(first); 
    System.out.println(last); 
    System.out.println(time); 
} 

注意,這是使用JSON APIfull JavaDoc reference)的例子。 Google Cloud Datastore docs中的示例使用Protocol Buffers API,其結構相同但語法略有不同。

+0

方法getEntityResultList()未定義。我正在使用谷歌雲數據存儲與API引擎訪問我的另一個項目數據。 –

+0

您使用的是哪個版本的客戶端庫,以及如何安裝它們? –

+0

我使用com.google.api.services.datastore版本:v1beta1-rev8-1.15.0-rc和com.google.api.services.datastore.model –