dongsi7067 2017-01-10 11:30
浏览 129
已采纳

aerospike,golang —获取所有记录的所有bin名称

Just wanted to check, if there is a way to get all bin names for every record ?

I know we can get the binmap using the following code. But I want to read all the keys and create a map[string]interface{} and then convert that map to byte array.

This is my code to get all the bins , add them to an array, convert array to byte array :

package main

import (
    "bytes"
    "encoding/gob"
    "fmt"
    as "github.com/aerospike/aerospike-client-go"
    "strconv"
    "time"
)

var client *as.Client


func main() {
    stmt := as.NewStatement("txn", "user")
    stmt.Addfilter(as.NewEqualFilter("p", "param"))
    rs, err := GetAerospikeClient().Query(nil, stmt)
    if err == nil {

        //Conv to byte array
        var ret []interface{}
        for res := range rs.Results() {
            if res.Err != nil {
                // handle error here
                // if you want to exit, cancel the recordset to release the resources
                fmt.Println("Err------", res.Err)

            } else {
                // process record here
                fmt.Printf("Success------%#v
", res.Record.Bins)
                ret = append(ret, res.Record.Bins)
            }
        }

        b, _ := GetBytes(ret)
        fmt.Println("Len of byte array ", len(b))



    }

}

func GetAerospikeClient() *as.Client {
    var err error
    port, _ := strconv.Atoi("3000")
    maxconn, _ := strconv.Atoi("10")
    host := "172.28.128.3"
    timeout, _ := strconv.Atoi("50")
    idletimeout, _ := strconv.Atoi("3600")
    clientPolicy := as.NewClientPolicy()
    clientPolicy.ConnectionQueueSize = maxconn
    clientPolicy.LimitConnectionsToQueueSize = true
    clientPolicy.Timeout = time.Duration(timeout) * time.Millisecond
    clientPolicy.IdleTimeout = time.Duration(idletimeout) * time.Second
    client, err = as.NewClientWithPolicy(clientPolicy, host, port)
    if err != nil {
        panic(err)
    }
    return client
}   

func GetBytes(key []interface{}) ([]byte, error) {
    var buf bytes.Buffer
    gob.Register(as.BinMap{})
    gob.Register([]interface{}{})
    enc := gob.NewEncoder(&buf)
    err := enc.Encode(key)
    if err != nil {
        panic(err)
        return nil, err
    }
    return buf.Bytes(), nil
}

Thanks

  • 写回答

1条回答 默认 最新

  • doqau82086 2017-01-11 09:49
    关注

    If you only want the list of bins of a namespace, there is a much less expensive way. You do not need to do a query/scan which is very expensive. One catch is that you cannot get it per set. There is an info command "bins/nsname" (replace "nsname" with your desired namespace name) to get all the bins in that namespace.

    If you do not care about a programmatic way of getting the binlist, you can get it using the asinfo tool provided with aerospike-tools package. You can issue the following command. The output is reasonably self explanatory. (Replace "127.0.0.1" with the proper IP address of the node)

    asinfo -v "bins/nsname" -h 127.0.0.1

    If you want to get the list programmatically, You can use the RequestInfo() API as given in this example and send the above command . You should write parser to extract the desirable fields.

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

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿