2016-08-02 64 views
1

使用com.couchbase.client, java-client版本2.2.7我一直無法得到使用與多個項目看我的示例查詢和Java代碼中的IN語句n1ql查詢工作如下Couchbase參數N1QL查詢在聲明

public int getCountForDuration(Long startTime, Long endTime, String ids){ 
    JsonObject placeHolders = JsonObject.create().put("ids", ids).put("startTime", startTime).put("endTime", endTime); 
    N1qlQuery query = N1qlQuery.parameterized(COUNT_STATEMENT, placeHolders)    
    N1qlQueryResult result = bucket.query(query); 
    ... 
} 

public static final String COUNT_STATEMENT = "select count(*) as count " + 
      "from bucketName " + 
      "where docType = 'docId' " + 
      "and (id IN [$ids]) " + <----- OFFENDING LINE 
      "and publishTimestamp between $startTime and $endTime"; 

我試着使用('),(「)設置ids,和(`),如:

ids = "'123', '456'"; 
ids = "\"123\" , \"456\"; 
ids = "`123`,`456`"; 

這些都不是當有多個ID但是如果只有一個諸如ids = "'123'"它工作正常,工作也是我查詢工作如果我在終端上使用CBQ。

我的問題是,我如何創建參數化的N1QL查詢,其中 可以在IN語句中取多個項目?

+0

放錯了地方的括號應該是「和(身份證件([$ IDS] ))「 – Stavm

回答

4

刪除語句中的$ids周圍的括號,並把實際的ID添加到placeholders作爲JsonArray對象應該工作:

JsonObject placeHolders = JsonObject.create() 
    .put("ids", JsonArray.from("id1", "id2", "id3")) 
    .put("startTime", startTime) 
    .put("endTime", endTime); 
+0

嘿西蒙你看看這個問題http://stackoverflow.com/questions/38845615/n1ql-query-times-out-on-aws-server couchbase collectInfo記錄這裏HTTPS://s3.amazonaws .com/cb-customers/TE2 /提前致謝。 –

+0

如何在Spring Data中設置這樣的佔位符? 例如。我有: '@Query( 「#{#n1ql.selectEntity}使用鍵$ IDS」)收集 findByIdIn(@Param( 「IDS」)列表ID);' 但調用它通過HTTP我得到' 「不支持的類型JsonArray:類java.util.ArrayList」' 我可以使它工作的唯一途徑是: '@Query( 「#{#n1ql.selectEntity}使用鍵$ IDS」)收集 findByIdIn( @Param( 「IDS」)JsonArray IDS);' ,然後定義自定義objectMapper從REST調用獲得JsonArray。有沒有更好的辦法? – rashtao

+0

我的意思是使用@RepositoryRestResource – rashtao