Skip to content

Instantly share code, notes, and snippets.

@xsalefter
Created March 27, 2015 19:42
Show Gist options
  • Save xsalefter/622da1d01f86594d0adb to your computer and use it in GitHub Desktop.
Save xsalefter/622da1d01f86594d0adb to your computer and use it in GitHub Desktop.
package ca.visdom.mailsync;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import org.junit.Test;
import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.protocol.views.Query;
import com.couchbase.client.protocol.views.View;
import com.couchbase.client.protocol.views.ViewResponse;
import com.couchbase.client.protocol.views.ViewRow;
public class DirectCouchbaseClientTest {
@Test @org.junit.Ignore
public void helloCouchbaseTest() throws InterruptedException, ExecutionException {
List<URI> nodes = new ArrayList<URI>();
// Add one or more nodes of your cluster (exchange the IP with yours)
nodes.add(URI.create("http://127.0.0.1:8091/pools"));
// Try to connect to the client
CouchbaseClient client = null;
try {
client = new CouchbaseClient(nodes, "general-testing", "");
} catch (Exception e) {
System.err.println("Error connecting to Couchbase: " + e.getMessage());
}
// Set your first document with a key of "hello" and a value of "couchbase!"
client.set(UUID.randomUUID().toString(), "{\"value\": \"" + new Random().nextLong() + "!\"}").get();
// Return the result and cast it to string
String result = (String) client.get("hello");
System.out.println(result);
// Shutdown the client
client.shutdown();
}
@Test
public void readBeerSampleView() {
List<URI> nodes = new ArrayList<URI>();
nodes.add(URI.create("http://127.0.0.1:8091/pools"));
CouchbaseClient client = null;
try {
client = new CouchbaseClient(nodes, "mailsync-dev", "");
} catch (Exception e) {
System.err.println("Error connecting to Couchbase: " + e.getMessage());
}
// View view = client.getView("beer", "by_type");
// Query query = new Query();
// query.setIncludeDocs(true).setLimit(5); // include all docs and limit to 5
// ViewResponse result = client.query(view, query);
//
// // Iterate over the result and print the key of each document:
// for(ViewRow row : result) {
// System.out.println(row.getId() + ": " + row.getDocument());
// }
View view = client.getView("email", "all");
Query query = new Query();
query.setIncludeDocs(true).setLimit(5); // include all docs and limit to 5
ViewResponse result = client.query(view, query);
// Iterate over the result and print the key of each document:
for(ViewRow row : result) {
System.out.println(row.getId() + ": " + row.getDocument());
}
client.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment