-
Notifications
You must be signed in to change notification settings - Fork 157
Using a custom Converter
When storing data in or retrieving data from Riak, a Converter is used to serialize the Java object passed in. When no Converter is specified and you're using your own Domain Object (POJO) the default JSONConverter is used. Behind the scenes this takes your object, uses the Jackson JSON library to serialize it, and stores the resulting JSON text in Riak. This can be advantageous if, for example, you are planning on accessing this data from other (non-java) applications.
If you wanted to use a different serialization library, a custom Converter would be required. Lets look at how you would write a Converter to use the popular Kryo serialization library to serialize and deserialize your objects.
Note this example is very basic. We're just covering the use of Kryo in a Converter. In this example any links, user metadata, indexes, etc. would be lost in translation. We'll cover that in the next example.
import com.basho.riak.client.IRiakClient;
import com.basho.riak.client.RiakException;
import com.basho.riak.client.RiakFactory;
import com.basho.riak.client.bucket.Bucket;
public class App
{
public static void main( String[] args ) throws RiakException
{
IRiakClient client = RiakFactory.httpClient();
Person p = new Person("Brian Roach", "1111 Basho Drive", "555-1212");
Bucket bucket = client.fetchBucket("PersonBucket").execute();
bucket.store(p).withConverter(new KryoPersonConverter("PersonBucket")).execute();
p = new Person();
p.setName("Brian Roach");
p = bucket.fetch(p).withConverter(new KryoPersonConverter("PersonBucket")).execute();
System.out.println(p.getName());
System.out.println(p.getAddress());
System.out.println(p.getPhone());
client.shutdown();
}
}
import com.basho.riak.client.convert.RiakKey;
public class Person
{
@RiakKey private String name;
private String address;
private String phone;
public Person() {}
public Person(String name, String address, String phone)
{
this.name = name;
this.address = address;
this.phone = phone;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getPhone()
{
return phone;
}
public void setPhone(String phone)
{
this.phone = phone;
}
}
import com.basho.riak.client.http.util.Constants;
import com.basho.riak.client.builders.RiakObjectBuilder;
import com.esotericsoftware.kryo.ObjectBuffer;
import com.esotericsoftware.kryo.Kryo;
import com.basho.riak.client.IRiakObject;
import com.basho.riak.client.cap.VClock;
import com.basho.riak.client.convert.ConversionException;
import com.basho.riak.client.convert.Converter;
import com.basho.riak.client.convert.NoKeySpecifedException;
import static com.basho.riak.client.convert.KeyUtil.getKey;
/**
*
* @author roach
*/
public class KryoPersonConverter implements Converter<Person>
{
private String bucket;
public KryoPersonConverter(String bucket)
{
this.bucket = bucket;
}
public IRiakObject fromDomain(Person domainObject, VClock vclock) throws ConversionException
{
String key = getKey(domainObject);
if (key == null)
{
throw new NoKeySpecifedException(domainObject);
}
Kryo kryo = new Kryo();
kryo.register(Person.class);
ObjectBuffer ob = new ObjectBuffer(kryo);
byte[] value = ob.writeObject(domainObject);
return RiakObjectBuilder.newBuilder(bucket, key)
.withValue(value)
.withVClock(vclock)
.withContentType(Constants.CTYPE_OCTET_STREAM)
.build();
}
public Person toDomain(IRiakObject riakObject) throws ConversionException
{
if (riakObject == null)
return null;
Kryo kryo = new Kryo();
kryo.register(Person.class);
ObjectBuffer ob = new ObjectBuffer(kryo);
return ob.readObject(riakObject.getValue(), Person.class);
}
}