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

    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:

    Scan all data

    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.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. String keyPrefix = "p";
    12. for(int i = 1; i <= 9; i ++) {
    13. for(int j = 1; j <= 9; j ++) {
    14. client.put(ByteString.copyFromUtf8(keyPrefix + i + j), ByteString.copyFromUtf8("v" + i + j));
    15. }
    16. }
    17. // scan all data
    18. ByteString endKey = Key.toRawKey(ByteString.copyFromUtf8(keyPrefix + "99")).next().toByteString();
    19. int limit = 4;
    20. while(true) {
    21. Key maxKey = Key.MIN;
    22. for (Kvrpcpb.KvPair pair : list) {
    23. System.out.println(pair);
    24. Key currentKey = Key.toRawKey(pair.getKey());
    25. if(currentKey.compareTo(maxKey) > 0) {
    26. maxKey = currentKey;
    27. }
    28. }
    29. if(list.size() < limit) {
    30. break;
    31. }

    To get the example code above, click here.