2017-01-10 13 views
1

すべてのレコードのすべてのビン名を取得する方法があるかどうかをチェックしたいのですか?aerospike、golang - すべてのレコードのすべてのビン名を取得

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

をあなただけの名前空間のビンのリストをしたい場合は、はるかに安価な方法があります。あなたは非常に高価なクエリ/スキャンを行う必要はありません。 1つのキャッチは、それを1セットごとに得ることができないということです。そのネームスペース内のすべてのビンを取得するには、infoコマンド"bins/nsname"( "nsname"をあなたのネームスペース名に置き換えます)があります。

ビンリストを取得するプログラム的な方法について気にしない場合は、パッケージで提供されているasinfoツールを使用して取得できます。次のコマンドを発行することができます。出力は合理的に自明です。 (ノードの正しいIPアドレスを持つ "127.0.0.1" を交換してください)

asinfo -v "ビン/ますNSname" -h 127.0.0.1

あなたは、プログラムのリストを取得したい場合は

this exampleのように RequestInfo() APIを使用し、上記のコマンドを送信できます。望ましいフィールドを抽出するためにパーサーを書くべきです。

関連する問題