// creating initial k-v pair
System.out.println("Creating initial Key and Value");
String keySchemaJson = "{ \"name\": \"key\", \"type\": \"record\", \"fields\": [{ \"name\": \"user_id\", \"type\": \"int\" }] }";
Schema keySchema = Schema.parse(keySchemaJson);
GenericRecord key = new GenericData.Record(keySchema);
key.put("user_id", 123);
String valueSchemaJson = "{\n" +
" \"name\": \"value\",\n" +
" \"type\": \"record\",\n" +
" \"fields\": [{ \n" +
" \"name\": \"user_id\",\n" +
" \"type\": \"int\"\n" +
" }, {\n" +
" \"name\": \"gender\",\n" +
" \"type\": \"string\"\n" +
" }, {\n" +
" \"name\": \"age\",\n" +
" \"type\": \"int\",\n" +
" \"optional\": true\n" +
" }]\n" +
" }";
Schema valueSchema = Schema.parse(valueSchemaJson);
GenericRecord value = new GenericData.Record(valueSchema);
value.put("user_id", 123);
value.put("gender", "male");
value.put("age", 23);
// put initial value
System.out.println("Putting Initial value");
client.put(key, value);
// get the value
System.out.println("Getting the value");
Versioned<GenericRecord> versioned = client.get(key);
System.out.println("Initial Versioned Object: " + String.valueOf(versioned));
System.out.println(" Initial Value: " + String.valueOf(versioned.getValue()));
// modify the value
System.out.println("Modifying the value");
GenericRecord modifiedRecord = versioned.getValue();
modifiedRecord.put("gender", "female");
modifiedRecord.put("age", 55);
versioned.setObject(modifiedRecord);
// update the value
System.out.println("Putting the new value");
client.put(key, versioned);