Writing Records to the Store

Other put Operations

Creating a new record in the store and updating an existing record are usually identical operations (although methods exist that work only if the record is being updated, or only if it is being created — these are described a little later in this section). In both cases, you simply write a record to the store that uses the appropriate key. If a record with that key does not currently exist in the store, then the record is created for you. If a record exists that does use the specified key, then that record is updated with the information that you are writing to the store.

In order to put an ordinary record into the store:

  1. Construct a key, making sure to specify all of the key's major and minor path components. For information on major and minor path components, see Record Design Considerations.

  2. Construct a value. This is the actual data that you want to put into the store. Normally you want to use the Avro data format for this, which is described in Avro Schemas and Avro Bindings.

  3. Use one of the KVStore class's put methods to put the record to the store.

The following is a trivial example of writing a record to the store. It assumes that the KVStore handle has already been created. For the sake of simplicity, this example trivially serializes a string to use as the value for the put operation. This is not what you should do in your code. Rather, you should use the Avro data format, even for this trivial case.

package kvstore.basicExample;

...

import oracle.kv.Key;
import oracle.kv.Value;
import java.util.ArrayList;

...

ArrayList<String> majorComponents = new ArrayList<String>();
ArrayList<String> minorComponents = new ArrayList<String>();

...

// Define the major and minor path components for the key
majorComponents.add("Smith");
majorComponents.add("Bob");

minorComponents.add("phonenumber");

// Create the key
Key myKey = Key.createKey(majorComponents, minorComponents);

String data = "408 555 5555";

// Create the value. Notice that we serialize the contents of the
// String object when we create the value.
Value myValue = Value.createValue(data.getBytes());

// Now put the record. Note that we do not show the creation of the
// kvstore handle here.

kvstore.put(myKey, myValue); 

Other put Operations

Beyond the very simple usage of the KVStore.put() method illustrated above, there are three other important put operations that you can use:

  • KVStore.putIfAbsent()

    This method will only put the record if the key DOES NOT current exist in the store. That is, this method is successful only if it results in a create operation.

  • KVStore.putIfPresent()

    This method will only put the record if the key already exists in the store. That is, this method is only successful if it results in an update operation.

  • KVStore.putIfVersion()

    This method will put the record only if the value matches the supplied version information. For more information, see Using Versions.