Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 使用 java api 如何查询 tikv集群中key的数量

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
Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 使用 java api 如何查询 tikv集群中key的数量
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
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("127.0.0.1:2379"); // 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) {
count++;
}
// If the last key is scanned, exit the loop
if (pairs.iterator().hasNext()) {
startKey = pairs.iterator().next().getKey().toByteArray();
} else {
break;
}
}
// Output the total number of keys
System.out.printf("Total keys: %d\n", count);
// Close the TiKV client
client.close();
session.close();
}
}
Note that the above example code uses the TiKV Java client API, and you need to add the following dependency to your project:
<dependency>
<groupId>org.tikv</groupId>
<artifactId>tikv-client</artifactId>
<version>4.0.0</version>
</dependency>
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.
It seems that the tikv-client-java version 3.1.0 does not support this method.
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.
In our notes, we found this segment:
Response<Pair<byte, Long>> response = client.scanKeys(
KeyUtils.EMPTY_BYTE_ARRAY,
KeyUtils.MAX_BYTE_ARRAY,
Long.MAX_VALUE
);