Skip to content
Switch branches/tags

TiKV JAVA Client

A Java client for TiDB/TiKV. It is supposed to:

  • Communicate via gRPC
  • Talk to Placement Driver searching for a region
  • Talk to TiKV for reading/writing data and the resulted data is encoded/decoded just like what we do in TiDB.
  • Talk to Coprocessor for calculation pushdown

How to build


The alternative way to build a usable jar for testing will be

mvn clean install -Dmaven.test.skip=true

The following command can install dependencies for you.

mvn package

The jar can be found in ./target/


This project is designed to hook with pd and tikv.

When you work with this project, you have to communicate with pd and tikv. Please run TiKV and PD in advance.

Component: Raw Ti-Client in Java

Java Implementation of Raw TiKV-Client to support RawKVClient commands.

Demo is avaliable in KVRawClientTest


mvn clean install -Dmaven.test.skip=true

Add to dependency

Use jar for binary

Add your jar built with all dependencies into you project's library to use tikv-client-java as dependency

Use as maven dependency

After building, add following lines into your pom.xml if you are using Maven




Create a RawKVClient

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

public class Main {
	public static void main() {
		// You MUST create a raw configuration if you are using RawKVClient.
		TiConfiguration conf = TiConfiguration.createRawDefault(YOUR_PD_ADDRESSES);
		TiSession session = TiSession.create(conf);
		RawKVClient client = session.createRawClient();

Java Client Configuration Parameter

JVM Parameter

The following includes JVM related parameters.


  • pd addresses, separated by comma
  • default:


  • timeout of grpc request
  • default: 600ms


  • timeout of scan/delete range grpc request
  • default: 20s


  • Maximal package size transporting from clients to TiKV Server (ingest API)
  • default: 1048576 (1M)


  • Maximal batch size transporting from clients to TiKV Server (ingest API)
  • default: 32768 (32K)


  • time to wait for scattering regions
  • default: 300 (5min)


  • RawKV default backoff in milliseconds
  • default: 20000 (20 seconds)

Metrics Parameter


  • whether to enable metrics exporting
  • default: false


  • the metrics exporting http port
  • default: 3140

ThreadPool Parameter

The following includes ThreadPool related parameters, which can be passed in through JVM parameters.


  • the thread pool size of batchGet on client side
  • default: 20


  • the thread pool size of batchPut on client side
  • default: 20


  • the thread pool size of batchDelete on client side
  • default: 20


  • the thread pool size of batchScan on client side
  • default: 5


  • the thread pool size of deleteRange on client side
  • default: 20


  • whether to enable Compare And Set, set true if using RawKVClient.compareAndSet or RawKVClient.putIfAbsent
  • default: false


Client Java supports exporting metrics to Prometheus using poll mode and viewing on Grafana. The following steps shows how to enable this function.

Step 1: Enable metrics exporting

  • set the config tikv.metrics.enable to true
  • call TiConfiguration.setMetricsEnable(true)

Step 2: Set the metrics port

  • set the config tikv.metrics.port
  • call TiConfiguration.setMetricsPort

Default port is 3140.

Step 3: Config Prometheus

Add the following config to conf/prometheus.yml and restart Prometheus.

- job_name: "tikv-client"
    honor_labels: true
    - targets:
        - ''
        - ''
        - ''

Step 4: Config Grafana

Import the Client-Java-Summary dashboard config to Grafana.


Apache 2.0 license. See the LICENSE file for details.