1. 环境
环境:Windows10、WSL2、Ubuntu20.04、Docker Desktop
目标:单节点单机部署,测试用
2. 前置操作
docker pull pingcap/tikv:latest
docker pull pingcap/pd:latestmkdir -p /mnt/tikv/pd
mkdir -p /mnt/tikv/tikv
ip a 命令查看虚拟机ip
ip 为:172.22.176.88,下面启动docker容器时需要指定
3. 部署
参考 https://tikv.org/docs/4.0/tasks/deploy/docker/
pd:
docker run -d --name pd \
-p 2379:2379 \
-p 2380:2380 \
-v /etc/localtime:/etc/localtime:ro \
-v /mnt/tikv/pd:/data \
pingcap/pd:latest \
--name="pd" \
--data-dir="/data/pd" \
--client-urls="http://0.0.0.0:2379" \
--advertise-client-urls="http://172.22.176.88:2379" \
--peer-urls="http://0.0.0.0:2380" \
--advertise-peer-urls="http://172.22.176.88:2380" \
--initial-cluster="pd=http://172.22.176.88:2380"
tikv:
docker run -d --name tikv \
-p 20160:20160 \
-v /etc/localtime:/etc/localtime:ro \
-v /mnt/tikv/tikv:/data \
pingcap/tikv:latest \
--addr="0.0.0.0:20160" \
--advertise-addr="172.22.176.88:20160" \
--data-dir="/data/tikv" \
--pd="172.22.176.88:2379"
4. 服务验证:
curl 172.22.176.88:2379/pd/api/v1/stores
5. Java Client 使用
官方文档:
https://tikv.org/docs/7.1/develop/clients/java/
pom引入依赖
<dependency><groupId>org.tikv</groupId><artifactId>tikv-client-java</artifactId><version>3.3.0</version>
</dependency>
test
import org.tikv.common.TiConfiguration;
import org.tikv.common.TiSession;
import org.tikv.kvproto.Kvrpcpb;
import org.tikv.raw.RawKVClient;
import org.tikv.shade.com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;public class Test {public static void main(String[] args) throws Exception {TiConfiguration conf = TiConfiguration.createRawDefault("172.22.176.88:2379");TiSession session = TiSession.create(conf);RawKVClient client = session.createRawClient();// putclient.put(ByteString.copyFromUtf8("k1"), ByteString.copyFromUtf8("Hello"));client.put(ByteString.copyFromUtf8("k2"), ByteString.copyFromUtf8(","));client.put(ByteString.copyFromUtf8("k3"), ByteString.copyFromUtf8("World"));client.put(ByteString.copyFromUtf8("k4"), ByteString.copyFromUtf8("!"));client.put(ByteString.copyFromUtf8("k5"), ByteString.copyFromUtf8("Raw KV"));// getOptional<ByteString> result = client.get(ByteString.copyFromUtf8("k1"));System.out.println(result.get().toStringUtf8());// batch getList<Kvrpcpb.KvPair> list =client.batchGet(new ArrayList<ByteString>() {{add(ByteString.copyFromUtf8("k1"));add(ByteString.copyFromUtf8("k3"));}});System.out.println(list);// scanlist = client.scan(ByteString.copyFromUtf8("k1"), ByteString.copyFromUtf8("k6"), 10);System.out.println(list);// closeclient.close();session.close();}}
控制台输出:
Hello
[key: "k1"
value: "Hello"
, key: "k3"
value: "World"
]
[key: "k1"
value: "Hello"
, key: "k2"
value: ","
, key: "k3"
value: "World"
, key: "k4"
value: "!"
, key: "k5"
value: "Raw KV"
]