How to Query the Number of Keys in a TiKV Cluster Using Java API

This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 使用 java api 如何查询 tikv集群中key的数量

| username: tikv-lu

How to use tikv-client-java 3.1.0 to operate a TiKV cluster v5.0.1 to check the number of keys in the cluster

| username: xfworld | Original post link

Here is an example code in Java using the TiKV API to get the total number of keys:

import org.tikv.common.TiConfiguration;
import org.tikv.common.TiSession;
import org.tikv.raw.RawKVClient;

public class TikvClientExample {
    public static void main(String[] args) {
        // Create TiKV client
        TiConfiguration conf = TiConfiguration.createDefault();
        conf.setPdAddrs(""); // PD address of the TiKV cluster
        TiSession session = TiSession.create(conf);
        RawKVClient client = session.createRawClient();

        // Scan the entire TiKV cluster and count the number of keys
        byte[] startKey = new byte[0];
        byte[] endKey = new byte[0];
        long limit = 0;
        boolean keyOnly = false;
        long count = 0;
        while (true) {
            // Scan the TiKV cluster
            Iterable<org.tikv.raw.Coprocessor.KeyValue> pairs = client.scan(startKey, endKey, limit, keyOnly);
            for (org.tikv.raw.Coprocessor.KeyValue pair : pairs) {
            // If the last key is scanned, exit the loop
            if (pairs.iterator().hasNext()) {
                startKey = pairs.iterator().next().getKey().toByteArray();
            } else {

        // Output the total number of keys
        System.out.printf("Total keys: %d\n", count);

        // Close the TiKV client

Note that the above example code uses the TiKV Java client API, and you need to add the following dependency to your project:


Additionally, the TiKV Java client API is still under development and may have some limitations and instabilities, so it is recommended to use it cautiously in production environments.

| username: tikv-lu | Original post link

It seems that the tikv-client-java version 3.1.0 does not support this method.

| username: 有猫万事足 | Original post link

I think you can try using the method in the test case of this 3.1.0 version to see if it can traverse.
RawKVClient does have this test case. TxnClient does not. If you are using TxnClient, you may need to adjust it a bit.

| username: redgame | Original post link

In our notes, we found this segment:
Response<Pair<byte, Long>> response = client.scanKeys(