Get, Put, and Delete

How to use RawKV's basic operations such as Get, Put, and Delete.

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


Import packages

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

import java.util.Optional;
import org.tikv.common.TiConfiguration;
import org.tikv.common.TiSession;
import org.tikv.raw.RawKVClient;

In the example above, is used as the type of Key and Value.

To avoid conflict, is shaded to, and is included in the client package.

Create RawKVClient

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

A comma is used to separate multiple PD addresses. For example,,,

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

TiConfiguration conf = TiConfiguration.createRawDefault("");
TiSession session = TiSession.create(conf);
RawKVClient client = session.createRawClient();

Write data to TiKV

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

ByteString key = ByteString.copyFromUtf8("Hello");
ByteString value = ByteString.copyFromUtf8("RawKV");
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.

Optional<ByteString> result = client.get(key);

Delete data from TiKV

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

result = client.get(key);

Close working instances

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


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