This document walks you through how to use RawKV’s Scan API.

Java

Scan with limit

Using the Scan API, you can scan key-value pairs from TiKV in a range (from a startKey to an endKey).

startKey is inclusive while endKey is exclusive.

To configure the Scan API to return a limited number of key-value pairs, you can use the limit argument as in the following example code:

  1. import java.util.List;
  2. import org.tikv.common.TiConfiguration;
  3. import org.tikv.common.TiSession;
  4. import org.tikv.kvproto.Kvrpcpb;
  5. import org.tikv.raw.RawKVClient;
  6. import org.tikv.shade.com.google.protobuf.ByteString;
  7. TiConfiguration conf = TiConfiguration.createRawDefault("127.0.0.1:2379");
  8. TiSession session = TiSession.create(conf);
  9. RawKVClient client = session.createRawClient();
  10. // prepare data
  11. client.put(ByteString.copyFromUtf8("k1"), ByteString.copyFromUtf8("v1"));
  12. client.put(ByteString.copyFromUtf8("k2"), ByteString.copyFromUtf8("v2"));
  13. client.put(ByteString.copyFromUtf8("k3"), ByteString.copyFromUtf8("v3"));
  14. client.put(ByteString.copyFromUtf8("k4"), ByteString.copyFromUtf8("v4"));
  15. // scan with limit
  16. int limit = 1000;
  17. List<Kvrpcpb.KvPair> list = client.scan(ByteString.copyFromUtf8("k1"), ByteString.copyFromUtf8("k5"), limit);
  18. for(Kvrpcpb.KvPair pair : list) {
  19. System.out.println(pair);
  20. }
  21. // close
  22. client.close();
  23. session.close();

Scan all data

The Scan API only returns a limited number of key-value pairs. If you want to fetch all the data in the range from startKey to endKey, refer to the following example code for a simple demo:

  1. import java.util.List;
  2. import org.tikv.common.TiConfiguration;
  3. import org.tikv.common.TiSession;
  4. import org.tikv.common.key.Key;
  5. import org.tikv.kvproto.Kvrpcpb;
  6. import org.tikv.raw.RawKVClient;
  7. import org.tikv.shade.com.google.protobuf.ByteString;
  8. TiConfiguration conf = TiConfiguration.createRawDefault("127.0.0.1:2379");
  9. TiSession session = TiSession.create(conf);
  10. RawKVClient client = session.createRawClient();
  11. // prepare data
  12. String keyPrefix = "p";
  13. for(int i = 1; i <= 9; i ++) {
  14. for(int j = 1; j <= 9; j ++) {
  15. client.put(ByteString.copyFromUtf8(keyPrefix + i + j), ByteString.copyFromUtf8("v" + i + j));
  16. }
  17. }
  18. // scan all data
  19. ByteString startKey = ByteString.copyFromUtf8(keyPrefix + "11");
  20. ByteString endKey = Key.toRawKey(ByteString.copyFromUtf8(keyPrefix + "99")).next().toByteString();
  21. int limit = 4;
  22. while(true) {
  23. List<Kvrpcpb.KvPair> list = client.scan(startKey, endKey, limit);
  24. Key maxKey = Key.MIN;
  25. for (Kvrpcpb.KvPair pair : list) {
  26. System.out.println(pair);
  27. Key currentKey = Key.toRawKey(pair.getKey());
  28. if(currentKey.compareTo(maxKey) > 0) {
  29. maxKey = currentKey;
  30. }
  31. }
  32. if(list.size() < limit) {
  33. break;
  34. }
  35. startKey = maxKey.next().toByteString();
  36. }

To get the example code above, click here.