sunty
2020-09-29 69de6114e99403830f06adcf765e4fb3c563eac5
get shards by index
1个文件已修改
71 ■■■■■ 已修改文件
EsApi.go 71 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
EsApi.go
@@ -257,8 +257,8 @@
    if err != nil {
        return nil, err
    }
    if len(source) == 0{
        return source,nil
    if len(source) == 0 {
        return source, nil
    }
    faceSource := make([]map[string]interface{}, 0)
    for index, info := range source {
@@ -358,8 +358,8 @@
    if err != nil {
        return nil, err
    }
    if len(source) == 0{
        return source,nil
    if len(source) == 0 {
        return source, nil
    }
    faceSource := make([]map[string]interface{}, 0)
    for index, info := range source {
@@ -1735,3 +1735,66 @@
    }
    return result, nil
}
type ShardInfo struct {
    ShardIndex string `json:"shardIndex"`
    ShardNum   int    `json:"shardNum"`
    ShardRole  string `json:"shardRole"`
    ShardState string `json:"shardState"`
    ShardDocs  int    `json:"shardDocs"`
    ShardStore string `json:"shardStore"`
    ShardIp    string `json:"shardIp"`
    ShardNode  string `json:"shardNode"`
}
//获取索引分片信息
func GetShardsByIndex(serverIp string, serverPort string, indexName string) ([]ShardInfo, error) {
    url := "http://" + serverIp + ":" + serverPort + "/_cat/shards?v"
    buf, err := EsReq("GET", url, []byte(""))
    if err != nil {
        return nil, err
    }
    var inf = []ShardInfo{}
    res := strings.Split(string(buf), "\n")[1:]
    for _, r := range res {
        if r != "" {
            inx := strings.Fields(r)
            index := inx[0]
            shard, _ := strconv.Atoi(inx[1])
            prired := inx[2]
            if prired == "r" {
                prired = "replica"
            }
            if prired == "p" {
                prired = "primary"
            }
            state := ""
            docs := 0
            store := ""
            ip := ""
            node := ""
            if state == "STARTED" {
                docs, _ = strconv.Atoi(inx[4])
                store = inx[5]
                ip = inx[6]
                node = inx[7]
            }
            if index == indexName {
                inf = append(inf, ShardInfo{
                    ShardIndex: index,
                    ShardNum:   shard,
                    ShardRole:  prired,
                    ShardState: state,
                    ShardDocs:  docs,
                    ShardStore: store,
                    ShardIp:    ip,
                    ShardNode:  node,
                })
            }
        }
    }
    return inf, nil
}