put:向Aerospike中添加一条记录
ascli put
ascli put upf testuser test '{"a": "A", "b": 1, "c": [1,2,3], "d": {"x": 4}}'
当我们需要修改abcd中其中的一个属性值时,可以通过如下:
ascli put upf testuser test '{"c": [1,2,3,4]}'
ascli get upf testuser test 得到如下:
[ root@mad13 ~]# ascli get upf testuser test
{"d": { "x": 4 }, "a": "A", "b": 1, "c": [ 1, 2, 3, 4 ]}
44 while read LINE
45 do
46 var1=`echo ${LINE} | awk -F' ' '{print $1}'`
47 var2=`echo ${LINE} | awk -F' ' '{print $2}'`
48 var3=`echo ${LINE} | awk -F' ' '{print $3}'`
49 var4=`echo ${LINE} | awk -F' ' '{print $4}'`
50 echo ${var4}
51 #ascli -h 192.168.0.15 -p 3000 put upf testuser ${var1} "{\"dlcr\": \"${var2}\"}"
52 ascli -h 192.168.0.13 -p 3000 udf-record-apply upf testuser ${var1} updateDlcr updateDlcr "{\"appendDlcr\": { \"${var2}|${var3}\": \"${var4}\"}}" "{\"sqid\": \"${var1}\"}" "{\"advertiserid\": \"${var2}\"}" "{\"appid\": \"${var3}\"}" "{\"acttype\": \"${var4}\"}"
53 done < ${USERDIR}/output/dlcr/dlcr.dat
java put
Bin bin1 = new Bin("name", "John");
Bin bin2 = new Bin("age", "30");
Bin bin3 = new Bin("sex", "男");
//java put大数据类型map
Map<String, String> map = new HashMap<String, String>();
map.put("0001", "新闻,0.78,1441782138412");//新闻
map.put("0002", "游戏,0.75,1441782138412");//游戏
Bin bin4 = Bin.asMap("cate", map);
//java put大数据类型list
List<String> list = new ArrayList<String>();
Bin bin5 = Bin.asList("dlcr", list);
client.put(policy, key, bin1, bin2, bin3, bin4, bin5);
lua udf put
定义lua文件updateDlcr.lua,如下:
function updateDlcr(rec,appendDlcr,sqid,advertiserid,appid,acttype)
local ret = map()
if not aerospike:exists(rec) then--判断rec记录是否存在
ret['status'] = 'DOES NOT EXIST'
rec['sqid'] = sqid['sqid']
rec['dlcr'] = appendDlcr['appendDlcr']
aerospike:create(rec)--不存在则创建一个新的key, 属性为sqid和dlcr
else--key存在则更新intt属性的值
local dlcr = rec['dlcr']
if dlcr == nil then
rec['dlcr'] = appendDlcr['appendDlcr']
ret['status'] = 'DLCR DOES NOT EXIST'
ret['dlcr'] = appendDlcr['appendDlcr']
else
ret['status'] = 'DLCR DOES EXIST'
local dlcr = rec['dlcr']
dlcr[advertiserid['advertiserid'] .. '|' .. appid['appid']] = acttype['acttype']
rec['dlcr'] = dlcr
end
aerospike:update(rec)--修改
end
--aerospike:update(rec)
return ret
end
把lua文件添加到Aerospike
[ root@mad13 lua]# ascli udf-put updateDlcr.lua
从Aerospike中得到lua函数的内容
[ root@mad13 lua]# ascli udf-get updateDlcr.lua
通过lua udf自定义函数向Aerospike中put数据
[ root@mad13 lua]# ascli udf-record-apply upf testuser shq updateDlcr updateDlcr '{"appendDlcr": { "38|10": "88"}}' '{"sqid": "shq"}' '{"advertiserid": "38"}' '{"appid": "10"}' '{"acttype": "88"}'
{ "status": "DLCR DOES EXIST" }
[ root@mad13 lua]# ascli get upf testuser shq
{"sqid": "shq", "dlcr": { "38|8": "88", "38|9": "52", "38|10": "88" }}
[ root@mad13 lua]# ascli udf-record-apply upf testuser shq updateDlcr updateDlcr '{"appendDlcr": { "38|8": "52"}}' '{"sqid": "shq"}' '{"advertiserid": "38"}' '{"appid": "8"}' '{"acttype": "52"}'
{ "status": "DLCR DOES EXIST" }
[ root@mad13 lua]# ascli get upf testuser shq
{"sqid": "shq", "dlcr": { "38|8": "52", "38|9": "52", "38|10": "88" }}
[ root@mad13 lua]# ascli udf-record-apply upf testuser shq updateDlcr updateDlcr '{"appendDlcr": { "38|9": "66"}}' '{"sqid": "shq"}' '{"advertiserid": "38"}' '{"appid": "9"}' '{"acttype": "66"}'
{ "status": "DLCR DOES EXIST" }
go put
// aerospike project main.go
package main
import (
"fmt"
. "github.com/aerospike/aerospike-client-go"
)
func panicOnError(err error) {
if err != nil {
panic(err)
}
}
func main() {
// define a client to connect to
client, err := NewClient("127.0.0.1", 3000)
panicOnError(err)
key, err := NewKey("upf", "testuser", "songhq")
panicOnError(err)
// define some bins with data
bins := BinMap{
"bin1": 42,
"bin2": "An elephant is a mouse with an operating system",
"bin3": []interface{}{"Go", 2009, true},
}
// write the bins
err = client.Put(nil, key, bins)
panicOnError(err)
// read it back!
rec, err := client.Get(nil, key)
panicOnError(err)
fmt.Printf("%#v\n", rec.Bins["bin1"])
// delete the key, and check if key exists
//existed, err := client.Delete(nil, key)
//panicOnError(err)
//fmt.Printf("Record existed before delete? %v\n", existed)
}