2017-01-10 56 views
1

只需要檢查,是否有方法可以獲取每個記錄的所有bin名稱?aerospike,golang - 獲取所有記錄的所有bin名稱

我知道我們可以使用下面的代碼得到binmap。但我想要讀取所有的密鑰並創建一個map[string]interface{},然後將該映射轉換爲字節數組。

這是我的代碼把所有的垃圾桶,將它們添加到陣列,轉換陣列字節數組:

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 
} 

感謝

+0

現在增加了完整的主要功能.... – Raaghu

回答

2

如果你只想要一個命名空間的箱的列表中,有一種更便宜的方法。您不需要執行非常昂貴的查詢/掃描。一個問題是你無法獲得每套。有一個info命令"bins/nsname"(用所需的名稱空間名稱替換「nsname」)以獲取該名稱空間中的所有bin。

如果您不關心獲取binlist的編程方式,您可以使用aerospike-tools軟件包提供的asinfo工具獲取它。您可以發出以下命令。輸出是合理的自我解釋。 (替換 「127.0.0.1」 與節點的正確的IP地址)

asinfo -v 「箱/ nsname」 -h 127.0.0.1

如果您希望以編程方式獲取列表,您可以使用this example中給出的RequestInfo() API併發送上述命令。你應該編寫解析器來提取所需的字段。