This document guides you on how to use Java Client in TiKV.

TiKV Java Client is developed and released using Java8. The minimum supported version of TiKV is 2.0.0.

Add the dependency

To start, open the pom.xml of your project, and add the tikv-client-java as dependencies if you are using Maven.

  1. <dependency>
  2. <groupId>org.tikv</groupId>
  3. <artifactId>tikv-client-java</artifactId>
  4. <version>3.2.0-SNAPSHOT</version>
  5. </dependency>

Try the Raw key-value API

Using a connected org.tikv.raw.RawKVClient, you can perform actions such as put, get, delete, and scan:

  1. import com.google.protobuf.ByteString;
  2. import java.util.List;
  3. import org.tikv.common.TiConfiguration;
  4. import org.tikv.common.TiSession;
  5. import org.tikv.kvproto.Kvrpcpb;
  6. import org.tikv.raw.RawKVClient;
  7. TiConfiguration conf = TiConfiguration.createRawDefault("127.0.0.1:2379");
  8. TiSession session = TiSession.create(conf);
  9. RawKVClient client = session.createRawClient();
  10. ByteString key = ByteString.copyFromUtf8("Hello");
  11. ByteString value = ByteString.copyFromUtf8("RawKV");
  12. // put
  13. client.put(key, value);
  14. // get
  15. ByteString result = client.get(key);
  16. assert("RawKV".equals(result.toStringUtf8()));
  17. System.out.println(result.toStringUtf8());
  18. // delete
  19. client.delete(key);
  20. // get
  21. result = client.get(key);
  22. assert(result.toStringUtf8().isEmpty());
  23. System.out.println(result.toStringUtf8());
  24. // scan
  25. int limit = 1000;
  26. client.put(ByteString.copyFromUtf8("k1"), ByteString.copyFromUtf8("v1"));
  27. client.put(ByteString.copyFromUtf8("k2"), ByteString.copyFromUtf8("v2"));
  28. client.put(ByteString.copyFromUtf8("k3"), ByteString.copyFromUtf8("v3"));
  29. client.put(ByteString.copyFromUtf8("k4"), ByteString.copyFromUtf8("v4"));
  30. List<Kvrpcpb.KvPair> list = client.scan(ByteString.copyFromUtf8("k1"), ByteString.copyFromUtf8("k5"), limit);
  31. for(Kvrpcpb.KvPair pair : list) {
  32. System.out.println(pair);
  33. }

These functions also have batch variants (batchPut, batchGet, batchDelete, and batchScan) which considerably reduce network overhead and increase performance under certain workloads.

You can find all functions supported by RawKVClient in this JAVA file.

Transactional key-value API

Transactional key-value API is still in the stage of prove-of-concept and under heavy development.