Skip to content

Instantly share code, notes, and snippets.

@tgrall
Created July 29, 2013 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgrall/6105234 to your computer and use it in GitHub Desktop.
Save tgrall/6105234 to your computer and use it in GitHub Desktop.
Sample Java Lookup
package com.couchbase.sample;
import com.couchbase.client.CouchbaseClient;
import com.google.gson.Gson;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
class User
{
String uid;
String type = "user";
String name;
String email;
String fbId;
User(String uid, String type, String name, String email, String fbId) {
this.uid = uid;
this.type = type;
this.name = name;
this.email = email;
this.fbId = fbId;
}
}
public class SampleLookupApp {
public static void main(String[] args) {
List<URI> uris = new LinkedList<URI>();
uris.add(URI.create("http://127.0.0.1:8091/pools"));
CouchbaseClient cb = null;
try {
cb = new CouchbaseClient(uris, "default", "");
Gson json = new Gson();
String id = UUID.randomUUID().toString();
String email = "john@demo.couchbase.com";
User u = new User(id, "user", "John Doe", email, "jdoe");
// store the document as JSON
cb.set("user::"+id, json.toJson(u));
// store lookups
cb.set("email::"+ u.email, u.uid);
cb.set("fb::"+ u.fbId, u.uid);
// Look user by email
String idLookup = (String)cb.get("email::"+ email);
System.out.println( cb.get("user::"+ idLookup ) );
cb.shutdown(10, TimeUnit.SECONDS);
} catch (Exception e) {
System.err.println("Error connecting to Couchbase: " + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment