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 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?