go-tikv-client 使用

【事件描述】
目前我的环境有多个tikv-go-client 同时使用tikv ,因此使用prefix 进行逻辑隔离存储数据,例如
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

【需求】
因为客户端比较多,有到每个prefix下读取数据的需要。是否可以直接和 tikv://192.168.69.31:2379 建立链接,然后按照prefix 的方式去scanvalue,以便一次性的读取多个prefix下的数据。但是目前尝试是不可以的。
【使用的接口函数】
NewClient——建立链接;
tx.scanValues(prefix, limit, filter) ——扫描数据

这是业务上的需求没理清楚吧?
prefix 没有限制支持到多级或者多层次的硬性规定,可自行安排规则

可参考 tree 的 算法,来约定层次和高度

问题是 从上一级 按照前缀 取不到数据~~~

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"
)

// 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.

no no no, 我并不是这么使用的,我的使用方式如下:
1、在这个tikv 路径下上传数据:192.168.69.31:2379/abcd-efjh-ijkl;
2、在这个192.168.69.31:2379 路径下获取上面1存储的数据 获取不到;

是说 tx.scanValues 获取不到数据?这个 scanValues 是如何实现的?


prefix 就是"abcd-efjh-ijkl"

kvTxn.scanValues 仍然是你们自己的代码吧?请提供一下直接使用 client-go 接口的代码。

另外,请问 client-go 的版本。

如果需要 scan 数据,可以参考 TiKV | Go Client 其中的 Iter 接口部分

非常感谢您的回复。
接口代码:



image

看上去没什么问题。

能否提供一段可以复现问题的最小可运行代码?

感谢分享