2014-02-08 39 views
2

我有一個要求,我已經從couchbase中獲取文檔。如何使用couchbase中的視圖獲取文檔

繼我使用了相同的地圖功能 -

function (doc, meta) { 
    if (meta.type == "json" && doc!=null) { 
    emit(doc); 
    } 
} 

有沒有降低的功能。另外下面是我的Java代碼來獲取文件 -

List<URI> hosts = Arrays.asList(
      new URI("http://<some DNS with port>/pools") 
    ); 

    // Name of the Bucket to connect to 
    String bucket = "Test-Sessions"; 

    // Password of the bucket (empty) string if none 
    String password = ""; 
    //System.setProperty("viewmode", "development"); 
    // Connect to the Cluster 
    CouchbaseClient client = new CouchbaseClient(hosts, bucket, password); 


    String designDoc = "sessions"; 
    String viewName = "by_test"; 
    View view = client.getView(designDoc, viewName); 
    Query query = new Query(); 
    query.setIncludeDocs(true); 
    query.setKey(String.valueOf(122)); 
    ViewResponse result = client.query(view, query); 
    Object object = null; 
    for(ViewRow row : result) { 
     if(null != row) { 
     object = row.getDocument(); 
     }// deal with the document/data 
    } 
    System.out.println("Object" + object); 

和數據,我在couchbase是關鍵 - 「122」和價值 - 「真」。但由於某種原因,ViewResponse中沒有任何行。出什麼事了,誰能幫忙?

回答

2

我不明白你想在這裏實現什麼,你正在使用視圖來獲取它的關鍵文件? Key == 122?爲什麼你不能只做client.get(122)?

如果你只需要在你的水桶所有鍵的列表(其中,你可以用它來拉回來的所有文件通過包括文檔),然後讓你的功能,像這樣:

function (doc, meta) { 
    if (meta.type == "json") { 
     emit(); 
    } 
} 

的關鍵文檔始終以ID(viewRow.getId())形式發出。您不需要發送文檔,儘量發出儘可能少的數據以保持較小的視圖大小。

如果您需要操作桶中的所有文檔,請注意隨着大小的增加,也許您需要查看分頁以循環查看結果。 http://tugdualgrall.blogspot.com.es/

而且一旦你有ViewResponse遍歷它像這樣:

for(ViewRow row : result) { 
    row.getDocument(); // deal with the document/data 
} 

你並不需要做檢查就行空。

相關問題