Created
July 29, 2013 15:37
-
-
Save tgrall/6105234 to your computer and use it in GitHub Desktop.
Sample Java Lookup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "[email protected]"; | |
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