Skip to content
iosmanthus edited this page Apr 28, 2023 · 1 revision

client-go has fully supported API V2 for both RawKV and TxnKV, however, it's not entirely GA yet. If you want to try the features like keyspace, you should enable API V2 for the whole TiKV cluster. This Wiki page will introduce the basic configuration to enable the API V2 in a TiKV cluster.

First, you might enable API V2 in TiKV with the following config:

[storage]
api-version = 2
enable-ttl = true

By default, there is a pre-defined keyspace called DEFAULT with keyspace id: 0 in the newly created V2 cluster.

For example, you could create a V2 client with the following code:

// create a rawkv client
cli, err := rawkv.NewClientWithOpts(context.Background(), addrs, rawkv.WithAPIVersion(kvrpcpb.APIVersion_V2))
// using `cli` do some stuff

// or create a txnkv client
cli, err := txn.NewClient(addrs, txn.WithAPIVersion(kvrpcpb.APIVersion_V2))

This client will read/write from/to the data located in keyspace DEFAULT, any other keyspace client could not access them.

If you want to access keyspaces other than DEFAULT, there are two ways to achieve this:

  1. config pre-defined keyspaces in PD with the following config:
[keyspace]
pre-alloc = ["a", "b", "c"]

This config will create 3 keyspaces while the cluster bootstraps.

Then, you could create a client with the keyspace options:

// create a rawkv client
cli, err := rawkv.NewClientWithOpts(context.Background(), addrs, rawkv.WithAPIVersion(kvrpcpb.APIVersion_V2), rawkv.WithKeyspace("a"))
// using `cli` do some stuff

// or create a txnkv client
cli, err := txn.NewClient(addrs, txn.WithAPIVersion(kvrpcpb.APIVersion_V2), rawkv.WithKeyspace("a"))
  1. call PD's API manually
// with HTTPie
> http post http://127.0.0.1:2379/pd/api/v2/keyspaces name=<keyspace-name>
Clone this wiki locally