Skip to content

Instantly share code, notes, and snippets.

@mikewied
Created July 5, 2011 20:00
Show Gist options
  • Save mikewied/1065771 to your computer and use it in GitHub Desktop.
Save mikewied/1065771 to your computer and use it in GitHub Desktop.
Java Couchbase API
===========
Examples:
===========
CouchbaseClient couchbase_client = new CouchbaseClient(Arrays.asList(URI.create("http://localhost:8091/pools")), "default", "");
// Get views from a design document
List<View> views = couchbase_client.getViews("design_doc_name");
// Get a single view from the server
View view = couchbase_client.getView("design_doc_name", "view_name");
// Build a query
Query query = new Query();
query.setRange("start", "end");
query.setLimit(100);
query.setPerPage(25);
query.setPage(2);
// Do a range query on a view
couchbase_client.rangeQuery(views.get("view_name"), "start", "end");
// Query a function with only a map and get Documents
Collection<ViewResponseWithDocs> resp1 = couchbase_client.query(view, query);
// Can access the result by iterating over the documents
Iterator<RowWithDocs> itr = resp1.iterator();
while (itr.hasNext()) {
RowWithDocs row = itr.next();
}
// Or we can get a specific row if we know the doc id
RowWithDocs row = resp1.get("doc_id");
// Query a function with only a map and don't get Documents
Collection<ViewResponseWithoutDocs> resp2 = couchbase_client.queryWithoutDocs(view, query);
// Can access the result by iterating over the documents
Iterator<RowWithoutDocs> itr = resp1.iterator();
while (itr.hasNext()) {
RowWithoutDocs row = itr.next();
}
// Or we can get a specific row if we know the doc id
RowWithoutDocs row = resp1.get("doc_id");
// Query a function with only a map and don't get Documents
Collection<ReducedViewResponse> resp2 = couchbase_client.queryAndReduce(view, query);
// Can access the result by iterating over the documents
Iterator<ReduceRow> itr = resp1.iterator();
while (itr.hasNext()) {
RowWithoutDocs row = itr.next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment