This document guides you on how to use Java Client through some simple examples. For more details, please visit TiKV Java Client User Documents.

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</version>
  5. </dependency>

Try the transactional key-value API

Below is the basic usages of TxnKV. Data should be written into TxnKV using TwoPhaseCommitter, and be read using org.tikv.txn.KVClient.

  1. import java.util.Arrays;
  2. import java.util.List;
  3. import org.tikv.common.BytePairWrapper;
  4. import org.tikv.common.ByteWrapper;
  5. import org.tikv.common.TiConfiguration;
  6. import org.tikv.common.TiSession;
  7. import org.tikv.common.util.BackOffer;
  8. import org.tikv.common.util.ConcreteBackOffer;
  9. import org.tikv.kvproto.Kvrpcpb.KvPair;
  10. import org.tikv.shade.com.google.protobuf.ByteString;
  11. import org.tikv.txn.KVClient;
  12. import org.tikv.txn.TwoPhaseCommitter;
  13. public class App {
  14. public static void main(String[] args) throws Exception {
  15. TiConfiguration conf = TiConfiguration.createDefault("127.0.0.1:2379");
  16. try (TiSession session = TiSession.create(conf)) {
  17. // two-phase write
  18. long startTS = session.getTimestamp().getVersion();
  19. try (TwoPhaseCommitter twoPhaseCommitter = new TwoPhaseCommitter(session, startTS)) {
  20. BackOffer backOffer = ConcreteBackOffer.newCustomBackOff(1000);
  21. byte[] primaryKey = "key1".getBytes("UTF-8");
  22. byte[] key2 = "key2".getBytes("UTF-8");
  23. // first phase: prewrite
  24. twoPhaseCommitter.prewritePrimaryKey(backOffer, primaryKey, "val1".getBytes("UTF-8"));
  25. List<BytePairWrapper> pairs = Arrays
  26. .asList(new BytePairWrapper(key2, "val2".getBytes("UTF-8")));
  27. twoPhaseCommitter.prewriteSecondaryKeys(primaryKey, pairs.iterator(), 1000);
  28. // second phase: commit
  29. long commitTS = session.getTimestamp().getVersion();
  30. twoPhaseCommitter.commitPrimaryKey(backOffer, primaryKey, commitTS);
  31. List<ByteWrapper> keys = Arrays.asList(new ByteWrapper(key2));
  32. twoPhaseCommitter.commitSecondaryKeys(keys.iterator(), commitTS, 1000);
  33. }
  34. try (KVClient kvClient = session.createKVClient()) {
  35. long version = session.getTimestamp().getVersion();
  36. ByteString key1 = ByteString.copyFromUtf8("key1");
  37. ByteString key2 = ByteString.copyFromUtf8("key2");
  38. // get value of a single key
  39. ByteString val = kvClient.get(key1, version);
  40. System.out.println(val);
  41. // get value of multiple keys
  42. BackOffer backOffer = ConcreteBackOffer.newCustomBackOff(1000);
  43. List<KvPair> kvPairs = kvClient.batchGet(backOffer, Arrays.asList(key1, key2), version);
  44. System.out.println(kvPairs);
  45. // get value of a range of keys
  46. kvPairs = kvClient.scan(key1, ByteString.copyFromUtf8("key3"), version);
  47. System.out.println(kvPairs);
  48. }
  49. }
  50. }
  51. }

Try the Raw key-value API

Below is the basic usages of RawKV.

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Optional;
  4. import org.tikv.common.TiConfiguration;
  5. import org.tikv.common.TiSession;
  6. import org.tikv.kvproto.Kvrpcpb;
  7. import org.tikv.raw.RawKVClient;
  8. import org.tikv.shade.com.google.protobuf.ByteString;
  9. public class Main {
  10. public static void main(String[] args) throws Exception {
  11. // You MUST create a raw configuration if you are using RawKVClient.
  12. TiConfiguration conf = TiConfiguration.createRawDefault("127.0.0.1:2379");
  13. TiSession session = TiSession.create(conf);
  14. RawKVClient client = session.createRawClient();
  15. // put
  16. client.put(ByteString.copyFromUtf8("k1"), ByteString.copyFromUtf8("Hello"));
  17. client.put(ByteString.copyFromUtf8("k2"), ByteString.copyFromUtf8(","));
  18. client.put(ByteString.copyFromUtf8("k3"), ByteString.copyFromUtf8("World"));
  19. client.put(ByteString.copyFromUtf8("k4"), ByteString.copyFromUtf8("!"));
  20. client.put(ByteString.copyFromUtf8("k5"), ByteString.copyFromUtf8("Raw KV"));
  21. // get
  22. Optional<ByteString> result = client.get(ByteString.copyFromUtf8("k1"));
  23. System.out.println(result.get().toStringUtf8());
  24. // batch get
  25. List<Kvrpcpb.KvPair> list = client.batchGet(new ArrayList<ByteString>() {{
  26. add(ByteString.copyFromUtf8("k1"));
  27. add(ByteString.copyFromUtf8("k3"));
  28. }});
  29. System.out.println(list);
  30. // scan
  31. list = client.scan(ByteString.copyFromUtf8("k1"), ByteString.copyFromUtf8("k6"), 10);
  32. System.out.println(list);
  33. // close
  34. client.close();
  35. session.close();
  36. }
  37. }