我正在尝试遵循代码。获取“找不到密钥”错误。我想知道是否可以使用aerospike多操作来创建一个记录。
public long incrementSingle(String counterName, long by){
// Create a key
Key recordKey = new Key(Constants.NAMESPACE, Constants.SINGLE_SET, counterName);
// Increment operation
Bin incrementCounter = new Bin(Constants.SINGLE_COUNTER_BIN, by);
// https://www.aerospike.com/docs/client/java/usage/kvs/multiops.html#operation-specification
Record record = asClient.operate(null, recordKey,
Operation.add(incrementCounter),
Operation.get(Constants.SINGLE_COUNTER_BIN));
return record.getLong(Constants.SINGLE_COUNTER_BIN);
} 发布于 2019-11-26 12:10:16
我认为要让add()起作用,记录之前必须存在。测试创建由recordKey引用的记录,在计数器bin中使用某个初始值(比如初始化为0),然后递增该记录。用于add()的Operation API并没有特别指出这一点,但我只是猜测,因为add()将首先读取磁盘上的现有记录,但可能找不到它。(编辑:请参阅下面的注释中使用的更多最终测试/每次写入澄清策略。)
发布于 2019-11-27 03:40:19
我不认为这有什么问题。下面是一些做同样事情的Python代码:
# -*- coding: utf-8 -*-
from __future__ import print_function
import aerospike
from aerospike import exception as e
from aerospike_helpers.operations import operations as oh
import sys
config = {"hosts": [("192.168.243.133", 3000)]}
try:
client = aerospike.client(config).connect()
except e.ClientError as e:
print("Error: {0} [{1}]".format(e.msg, e.code))
sys.exit(2)
key = ("test", "demo", "count-this")
print("Nuke the record")
try:
client.remove(key)
except:
pass
ops = [oh.increment("counter", 2), oh.read("counter")]
meta = {"ttl": aerospike.TTL_NEVER_EXPIRE}
for i in range(4):
try:
k, m, b = client.operate_ordered(key, ops, meta)
print("Iteration {0}: bin value is {1}".format(i, b))
except e.RecordError as e:
print("Error: {0} [{1}]".format(e.msg, e.code))
sys.exit(4)
client.close()导致预期的
Nuke the record
Iteration 0: bin value is [('counter', 2)]
Iteration 1: bin value is [('counter', 4)]
Iteration 2: bin value is [('counter', 6)]
Iteration 3: bin value is [('counter', 8)]https://stackoverflow.com/questions/59034025
复制相似问题