dongyuli4538 2019-03-26 05:39
浏览 121
已采纳

在etcd集群中查找密钥的API

I am trying to write a code where I need to find if a key exists in etcd or not. I tried this:

_, err = kapi.Get(context.Background(), key, nil)
        if err != nil {
          return err      
        } else { ...

But even if the key is not in the cluster, the error is always nil.

Any idea what am I doing wrong here? Or is there any other API call to make?

  • 写回答

1条回答 默认 最新

  • doutang9037 2019-03-26 14:56
    关注

    If you use the go client v3 KV client here: https://godoc.org/go.etcd.io/etcd/clientv3#KV

    It returns the following type: https://godoc.org/go.etcd.io/etcd/etcdserver/etcdserverpb#RangeResponse

    type RangeResponse struct {
        Header *ResponseHeader `protobuf:"bytes,1,opt,name=header" json:"header,omitempty"`
        // kvs is the list of key-value pairs matched by the range request.
        // kvs is empty when count is requested.
        Kvs []*mvccpb.KeyValue `protobuf:"bytes,2,rep,name=kvs" json:"kvs,omitempty"`
        // more indicates if there are more keys to return in the requested range.
        More bool `protobuf:"varint,3,opt,name=more,proto3" json:"more,omitempty"`
        // count is set to the number of keys within the range when requested.
        Count int64 `protobuf:"varint,4,opt,name=count,proto3" json:"count,omitempty"`
    }
    

    So as you see, there is a Count property and a Kvs property, if you want to check that your response contains keys you can just check that Count > 0 or len(res.Kvs) > 0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应