Using go-tikv-client

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: go-tikv-client 使用

| username: TiDBer_gvH6Tl64

[Event Description]
Currently, my environment has multiple tikv-go-clients using TiKV simultaneously. Therefore, I use prefixes for logical isolation to store data, for example:
client1 NewClient: tikv://192.168.69.31:2379/f5987654321
client2 NewClient: tikv://192.168.69.31:2379/f5123456789
client3 NewClient: tikv://192.168.69.31:2379/f59959d1234

[Requirement]
Because there are many clients, there is a need to read data under each prefix. Is it possible to directly establish a connection with tikv://192.168.69.31:2379 and then scan values according to the prefix, so as to read data under multiple prefixes at once? However, the current attempt is not working.
[Interface Functions Used]
NewClient - Establish connection;
tx.scanValues(prefix, limit, filter) - Scan data

| username: xfworld | Original post link

Isn’t this a case of unclear business requirements?
There are no strict regulations limiting the prefix to multiple levels or hierarchies; you can arrange the rules yourself.

You can refer to tree algorithms to define the levels and heights.

| username: TiDBer_gvH6Tl64 | Original post link

The issue is that data cannot be retrieved from the previous level according to the prefix.

| username: Billmay表妹 | Original post link

Yes, you can establish a connection to tikv://192.168.69.31:2379 and then use the prefix to scan values from multiple prefixes at once. However, you need to use the tikv-go client’s RawScan method to scan the values, and specify the range of keys that you want to scan using the prefix.

Here is an example code snippet that demonstrates how to scan values from multiple prefixes using the RawScan method:

import (
    "github.com/tikv/client-go/v2/tikv"
    "github.com/tikv/client-go/v2/config"
    "context"
)

// create a TiKV client
pdAddrs := []string{"192.168.69.31:2379"}
cfg := config.Default()
cfg.PDAddrs = pdAddrs
client, err := tikv.NewRawKVClient(context.Background(), pdAddrs, cfg)
if err != nil {
    // handle error
}

// specify the range of keys to scan using the prefix
startKey := []byte("f5")
endKey := []byte("f6")

// scan the values using the RawScan method
iter, err := client.RawScan(context.Background(), startKey, endKey)
if err != nil {
    // handle error
}
defer iter.Close()

// iterate over the values and do something with them
for iter.Valid() {
    key := iter.Key()
    value := iter.Value()
    // do something with the key and value
    iter.Next()
}

In this example, we create a tikv-go client and specify the PD address. Then, we use the RawScan method to scan the values from the range of keys that start with the prefix “f5” and end with the prefix “f6”. Finally, we iterate over the values and do something with them.

Please note that the RawScan method returns all the key-value pairs in the specified range, regardless of the prefix. Therefore, you need to filter the key-value pairs that belong to the prefixes that you are interested in.

| username: TiDBer_gvH6Tl64 | Original post link

No no no, that’s not how I use it. My usage is as follows:

  1. Upload data to this TiKV path: 192.168.69.31:2379/abcd-efjh-ijkl;
  2. Cannot retrieve the data stored in step 1 from this path: 192.168.69.31:2379.
| username: pingyu | Original post link

Are you saying that tx.scanValues is not able to retrieve data? How is this scanValues implemented?

| username: TiDBer_gvH6Tl64 | Original post link

The prefix is “abcd-efjh-ijkl”

| username: pingyu | Original post link

kvTxn.scanValues is still your own code, right? Please provide the code that directly uses the client-go interface.

Also, may I know the version of client-go?

If you need to scan data, you can refer to the Iter interface section in TiKV | Go Client.

| username: TiDBer_gvH6Tl64 | Original post link

Thank you very much for your reply.
Interface code:



image

| username: pingyu | Original post link

It doesn’t seem to have any issues.

Could you provide a minimal runnable code snippet that can reproduce the problem?

| username: TiDBer_rvITcue9 | Original post link

Thank you for sharing.