This document walks you through how to use RawKV’s basic operations such as Get, Put, and Delete.

Java

Import packages

First, import all necessary packages as shown in the example.

  1. import java.util.Optional;
  2. import org.tikv.shade.com.google.protobuf.ByteString;
  3. import org.tikv.common.TiConfiguration;
  4. import org.tikv.common.TiSession;
  5. import org.tikv.raw.RawKVClient;

In the example above, com.google.protobuf.ByteString is used as the type of Key and Value.

To avoid conflict, com.google.protobuf.ByteString is shaded to org.tikv.shade.com.google.protobuf.ByteString, and is included in the client package.

Create RawKVClient

To connect to TiKV, a PD address 127.0.0.1:2379 is passed to TiConfiguration.

A comma is used to separate multiple PD addresses. For example, 127.0.0.1:2379,127.0.0.2:2379,127.0.0.3:2379.

Using the connected org.tikv.raw.RawKVClient, you can perform actions such as Get, Put, and Delete.

  1. TiConfiguration conf = TiConfiguration.createRawDefault("127.0.0.1:2379");
  2. TiSession session = TiSession.create(conf);
  3. RawKVClient client = session.createRawClient();

Write data to TiKV

Using the put API, you can write a key-value pair to TiKV.

  1. ByteString key = ByteString.copyFromUtf8("Hello");
  2. ByteString value = ByteString.copyFromUtf8("RawKV");
  3. client.put(key, value);

Read data from TiKV

Using the get API, you can get the value of a key from TiKV. If the key does not exist, result.isPresent() will be false.

  1. Optional<ByteString> result = client.get(key);
  2. assert(result.isPresent());
  3. assert("RawKV".equals(result.get().toStringUtf8()));

Delete data from TiKV

Using the delete API, you can delete a key-value pair from TiKV.

  1. client.delete(key);
  2. result = client.get(key);
  3. assert(!result.isPresent());

Close working instances

Finally, do not forget to close the client and session instance.

  1. client.close();
  2. session.close();

The code example used in this chapter can be found here.