通过一大组键进行分页似乎涉及到使用WithFromKey()和WithLimit()选项来获取()。例如,如果我想获取10个项目的2个页面,我会这样做:
opts := []clientv3.OpOption {
clientv3.WithPrefix(),
clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend),
clientv3.WithLimit(10),
}
gr, err := kv.Get(ctx, "key", opts...)
if err != nil {
log.Fatal(err)
}
fmt.Println("--- First page ---")
for _, item := range gr.Kvs {
fmt.Println(string(item.Key), string(item.Key))
}
lastKey := string(gr.Kvs[len(gr.Kvs)-1].Value)
fmt.Println("--- Second page ---")
opts = append(opts, clientv3.WithFromKey())
gr, _ = kv.Get(ctx, lastKey, opts...)
// Skipping the first item, which the last item from from the previous Get
for _, item := range gr.Kvs[1:] {
fmt.Println(string(item.Key), string(item.Value))
}问题是,最后一个键作为第二页的第一个项被再次获取,我需要跳过它,并且只有9个新项。
这是正确的分页方式吗?还是我漏掉了什么?
发布于 2018-06-13 14:03:34
查看以下clientv3代码,可以通过将0x00附加到最后一个密钥来计算下一个密钥。
https://github.com/coreos/etcd/blob/88acced1cd7ad670001d1280b97de4fe7b647687/clientv3/op.go#L353
也就是说,我倾向于忽略第二页和后续页面中的第一个关键字。
https://stackoverflow.com/questions/44873514
复制相似问题