英文:
aerospike, golang -- get all bin names for all records
问题
只是想确认一下,是否有办法获取每个记录的所有bin名称?
我知道我们可以使用以下代码获取binmap
。但是我想读取所有的键并创建一个map[string]interface{}
,然后将该映射转换为字节数组。
这是我的代码,用于获取所有的bins,将它们添加到一个数组中,然后将数组转换为字节数组:
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\n", 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
}
谢谢。
英文:
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\n", 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
得分: 2
如果您只想获取命名空间的存储箱列表,有一种更经济的方法。您不需要执行昂贵的查询/扫描操作。但是有一个限制,您无法按集合获取存储箱列表。可以使用info命令"bins/nsname"
(将"nsname"
替换为您想要的命名空间名称)来获取该命名空间中的所有存储箱。
如果您不关心以编程方式获取存储箱列表,可以使用aerospike-tools
软件包提供的asinfo
工具来获取。您可以执行以下命令。输出结果相当自解释。(将"127.0.0.1"
替换为节点的正确IP地址)
asinfo -v "bins/nsname" -h 127.0.0.1
如果您想以编程方式获取列表,可以使用此示例中提供的RequestInfo()
API,并发送上述命令。您应该编写解析器来提取所需的字段。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论