-
Notifications
You must be signed in to change notification settings - Fork 158
Using links
broach edited this page Apr 11, 2012
·
7 revisions
Links are metadata that establish one-way relationships between objects in Riak. They can be used to loosely model graph like relationships between objects in Riak.For an introduction see our Overview on the Wiki as well as the entry in our fasttrack
Working with Links requires using the IRiakObject interface directly, as they are really metadata attached to the data you're storing.
import com.basho.riak.client.IRiakClient;
import com.basho.riak.client.IRiakObject;
import com.basho.riak.client.RiakException;
import com.basho.riak.client.RiakFactory;
import com.basho.riak.client.RiakLink;
import com.basho.riak.client.bucket.Bucket;
import com.basho.riak.client.query.WalkResult;
import java.util.Collection;
import java.util.Iterator;
public class App
{
public static void main( String[] args ) throws RiakException
{
IRiakClient client = RiakFactory.httpClient();
Bucket b = client.fetchBucket("test_bucket").execute();
// Create some records in riak
b.store("user_1234", "{\"name\":\"Brian Roach\"}").execute();
b.store("user_2345","{\"name\":\"Russell Brown\"}").execute();
b.store("user_3456","{\"name\":\"Sean Cribbs\"}").execute();
// Retrieve an object, add links, store back to Riak
IRiakObject myObject = b.fetch("user_1234").execute();
myObject.addLink(new RiakLink("test_bucket","user_2345","friend"))
.addLink(new RiakLink("test_bucket","user_3456","friend"));
b.store(myObject).execute();
// Retrieve the object we just stored, display the links
myObject = b.fetch("user_1234").execute();
for (RiakLink link : myObject.getLinks())
{
System.out.println(link.getBucket() + " " + link.getKey() + " " + link.getTag());
}
}
}