package main
import (
"fmt"
as "github.com/aerospike/aerospike-client-go"
"github.com/ugorji/go/codec"
)
type Human struct {
Name struct {
First string
Last string
}
Age int
}
// MessagePackでシリアライズしてAerospikeに保存
func put() {
ieyasu := &Human{
Name: struct {
First string
Last string
}{
"Ieyasu",
"Tokugawa",
},
Age: 70,
}
var buf []byte
mh := &codec.MsgpackHandle{RawToString: true}
codec.NewEncoderBytes(&buf, mh).Encode(ieyasu)
client, _ := as.NewClient("localhost", 3000)
key, _ := as.NewKey("bar", "set", "key")
bins := as.BinMap{"selialized": buf}
client.Put(nil, key, bins)
}
// Aerospikeから取得したデータをMessagePackでデシリアライズして表示
func get() {
client, _ := as.NewClient("localhost", 3000)
key, _ := as.NewKey("bar", "set", "key")
res, _ := client.Get(nil, key)
var human Human
mh := codec.MsgpackHandle{RawToString: true}
codec.NewDecoderBytes(res.Bins["selialized"].([]uint8), &mh).Decode(&human)
fmt.Println(human)
}
func main() {
put()
get()
}