Linux学习|Gentoo/Arch/FreeBSD

golang 实现查看遍历zk节点

2020-04-21  本文已影响0人  dwade06
package main
import (
    "fmt"
    "github.com/samuel/go-zookeeper/zk"
    "time"
)

func ZkStateString(s *zk.Stat) string {
    return fmt.Sprintf("Czxid:%d, Mzxid: %d, Ctime: %d, Mtime: %d, Version: %d, Cversion: %d, Aversion: %d, EphemeralOwner: %d, DataLength: %d, NumChildren: %d, Pzxid: %d",
        func ZKOperateTest() {
    fmt.Printf("ZKOperateTest\n")

    var hosts = []string{"127.0.0.1:2181"}
    conn, _, err := zk.Connect(hosts, time.Second*5)
    if err != nil {
        fmt.Println(err)
        return
    }
    defer conn.Close()

    var path = "/consumers"

    // get
    /*_,_,err := conn.Get(path)
    if err != nil {
        fmt.Println(err)
        return
    }*/

    children, _, err := conn.Children(path)
    if err != nil {
        return
    }
    for _, p := range children {
        fmt.Println("test node: ", p)
        //geti
        tmp := path +  "/" + p + "/offsets"
        fmt.Println("cur node: ", tmp)
        child, _, err := conn.Children(tmp)
        if err != nil {
            continue
        }
        for _, t := range child {
            fmt.Println("test child: ", t);
            if t == "topic1" || t == "topic2" {
                fmt.Println("wky : ", p)
            }
        }
    }
}

func main() {
    ZKOperateTest()
}
上一篇下一篇

猜你喜欢

热点阅读