I want to access TIKV using its java client on MacOS.
I have a hello-world app:
package com.pv.app;
import org.tikv.common.TiConfiguration;
import org.tikv.common.TiSession;
import org.tikv.raw.RawKVClient;
import shade.com.google.common.base.Charsets;
import shade.com.google.protobuf.ByteString;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "1" );
TiConfiguration conf = TiConfiguration.createRawDefault("0.0.0.0:32798");
System.out.println( "2" );
TiSession session = TiSession.create(conf);
System.out.println( "3" );
RawKVClient client = session.createRawClient();
System.out.println( "4" );
client.put(ByteString.copyFrom("hello-key", Charsets.UTF_8), ByteString.copyFrom("hello-message", Charsets.UTF_8));
System.out.println(client.get(ByteString.copyFrom("hello-key", Charsets.UTF_8)));
System.out.println( "Hello World!" );
}
}
I have docker-compose.yml (based on https://github.com/pingcap/tidb-docker-compose/blob/master/docker-compose.yml , I have added ports
and removed all redundant containers)
version: '2.1'
services:
pd0:
image: pingcap/pd:latest
ports:
- "2379"
volumes:
- ./config/pd.toml:/pd.toml:ro
- ./data:/data
- ./logs:/logs
command:
- --name=pd0
- --client-urls=http://0.0.0.0:2379
- --peer-urls=http://0.0.0.0:2380
- --advertise-client-urls=http://pd0:2379
- --advertise-peer-urls=http://pd0:2380
- --initial-cluster=pd0=http://pd0:2380,pd1=http://pd1:2380,pd2=http://pd2:2380
- --data-dir=/data/pd0
- --config=/pd.toml
- --log-file=/logs/pd0.log
restart: on-failure
pd1:
image: pingcap/pd:latest
ports:
- "2379"
volumes:
- ./config/pd.toml:/pd.toml:ro
- ./data:/data
- ./logs:/logs
command:
- --name=pd1
- --client-urls=http://0.0.0.0:2379
- --peer-urls=http://0.0.0.0:2380
- --advertise-client-urls=http://pd1:2379
- --advertise-peer-urls=http://pd1:2380
- --initial-cluster=pd0=http://pd0:2380,pd1=http://pd1:2380,pd2=http://pd2:2380
- --data-dir=/data/pd1
- --config=/pd.toml
- --log-file=/logs/pd1.log
restart: on-failure
pd2:
image: pingcap/pd:latest
ports:
- "2379"
volumes:
- ./config/pd.toml:/pd.toml:ro
- ./data:/data
- ./logs:/logs
command:
- --name=pd2
- --client-urls=http://0.0.0.0:2379
- --peer-urls=http://0.0.0.0:2380
- --advertise-client-urls=http://pd2:2379
- --advertise-peer-urls=http://pd2:2380
- --initial-cluster=pd0=http://pd0:2380,pd1=http://pd1:2380,pd2=http://pd2:2380
- --data-dir=/data/pd2
- --config=/pd.toml
- --log-file=/logs/pd2.log
restart: on-failure
tikv0:
image: pingcap/tikv:latest
ports:
- "20160"
volumes:
- ./config/tikv.toml:/tikv.toml:ro
- ./data:/data
- ./logs:/logs
command:
- --addr=0.0.0.0:20160
- --advertise-addr=tikv0:20160
- --data-dir=/data/tikv0
- --pd=pd0:2379,pd1:2379,pd2:2379
- --config=/tikv.toml
- --log-file=/logs/tikv0.log
depends_on:
- "pd0"
- "pd1"
- "pd2"
restart: on-failure
tikv1:
image: pingcap/tikv:latest
ports:
- "20160"
volumes:
- ./config/tikv.toml:/tikv.toml:ro
- ./data:/data
- ./logs:/logs
command:
- --addr=0.0.0.0:20160
- --advertise-addr=tikv1:20160
- --data-dir=/data/tikv1
- --pd=pd0:2379,pd1:2379,pd2:2379
- --config=/tikv.toml
- --log-file=/logs/tikv1.log
depends_on:
- "pd0"
- "pd1"
- "pd2"
restart: on-failure
tikv2:
image: pingcap/tikv:latest
ports:
- "20160"
volumes:
- ./config/tikv.toml:/tikv.toml:ro
- ./data:/data
- ./logs:/logs
command:
- --addr=0.0.0.0:20160
- --advertise-addr=tikv2:20160
- --data-dir=/data/tikv2
- --pd=pd0:2379,pd1:2379,pd2:2379
- --config=/tikv.toml
- --log-file=/logs/tikv2.log
depends_on:
- "pd0"
- "pd1"
- "pd2"
restart: on-failure
When I do curl 0.0.0.0:32810
I get @@
. When I launch my java code, I get
1
2
Exception in thread "main" java.lang.NullPointerException: Failed to init client for PD cluster.
at shade.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:228)
at org.tikv.common.PDClient.initCluster(PDClient.java:351)
at org.tikv.common.PDClient.createRaw(PDClient.java:378)
at org.tikv.common.TiSession.<init>(TiSession.java:37)
at org.tikv.common.TiSession.create(TiSession.java:45)
at com.pv.app.App.main(App.java:21)
How to fix this?
You have to launch your java application in docker using the same network as in your docker-compose and connect by the following ip: pd0:2379