我正在尝试对单个bin集进行原子操作。然而,我注意到一个奇怪的行为,随机记录没有得到增加/减少。操作命令的响应返回null。也没有得到任何异常。有时有效,有时无效。
尝试同时使用persistent进行检查和inmemory存储。这是一个3个节点的集群环境。
在某些情况下,它给出了热键错误,我可以通过设置属性transaction-pending-limit来解决这个问题
= 0。Aerospike client:
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>aerospike-client</artifactId>
<version>5.1.5.1</version>
</dependency>
操作命令:
public long incrementAndGetBinValue(AerospikeClient aerospikeClient, String key, Bin bin, long value, Key asKey) {
Record record;
try {
record = aerospikeClient.operate(aerospikeClient.writePolicyDefault, asKey, Operation.add(bin), Operation.get(bin.name));
logger.info("Aeroospike record {} ", record);
} catch (AerospikeException exception) {
logger.error("Aerospike Exception while incrementing bin {} ,key {} by value {} error : {}", bin.name, key, value, ExceptionUtils.getStackTrace(exception));
throw new NexusException(ErrorCodes.AEROSPIKE_OPERATION_ERROR, "aerospike exception operation error");
} catch (Exception exception) {
logger.error("Exception while incrementing bin {} ,key {} by value {} error : {}", bin.name, key, value, ExceptionUtils.getStackTrace(exception));
}
if (record == null) {
logger.error("incrementAndGetBinValue : No record returned for key {} ,bin {}", key, bin.name);
throw new NexusException(ErrorCodes.AEROSPIKE_NO_RECORD_FOUND, "aerospike no record returned");
}
return record.getLong(bin.name);
}
政策:
ClientPolicy policy = new ClientPolicy();
policy.timeout = 5000;
WritePolicy writePolicy = new WritePolicy();
writePolicy.socketTimeout = 60000;
writePolicy.maxRetries = 2;
writePolicy.sendKey = true;
writePolicy.expiration = -1; // never expire record
writePolicy.respondAllOps=true;
writePolicy.durableDelete=true;
policy.writePolicyDefault = writePolicy;
Policy readPolicy = new Policy();
readPolicy.maxRetries = 2;
readPolicy.socketTimeout = 5000;
policy.readPolicyDefault = readPolicy;
return policy;
1 -您正在传递长值但未使用它。2 -它必须在传递给Operation.add()时在Bin构造函数中使用。
例如
Bin bin1 = new Bin("name", "John Doe");
Bin bin2 = new Bin("age", 32);
Bin bin3 = new Bin("greeting", "Hello World!");
// Write a record
client.put(null, key, bin1, bin2, bin3);
//This creates the record with bin2=32
import com.aerospike.client.Operation;
Bin addTobin2 = new Bin("age", 4);
client.operate(null,key,Operation.add(addTobin2));
在这里,我期望put()调用创建值为32的bin。然后,operation()调用Operation.add(addTobin2)…将它的值4加到32。预期净值:36.
// Read the record
Record record = client.get(null, key);
System.out.println(record);
Record values are:
(gen:6),(exp:394484896),(bins:(name:John Doe),(age:36),(greeting:Hello World!))
Upsert Code Demo:
client.delete(null,key);
// Write a record
import com.aerospike.client.Operation;
Bin bin2 = new Bin("test", 2);
client.operate(null,key,Operation.add(bin2));
Record record = client.get(null, key);
System.out.println(record);
client.operate(null,key,Operation.add(bin2));
Record record = client.get(null, key);
System.out.println(record);
client.operate(null,key,Operation.add(bin2));
Record record = client.get(null, key);
System.out.println(record);
(gen:1),(exp:394490665),(bins:(test:2))
(gen:2),(exp:394490665),(bins:(test:4))
(gen:3),(exp:394490666),(bins:(test:6))
多个操作自动完成/代码示例:
client.delete(null,key);
// Write a record
import com.aerospike.client.Operation;
Bin bin2 = new Bin("test", 2);
System.out.println(bin2);
Record record = client.operate(null,key,
Operation.add(bin2),
Operation.get(bin2.name),
Operation.add(bin2),
Operation.get(bin2.name),
Operation.add(bin2),
Operation.get(bin2.name)
);
List<?> retList = (ArrayList<?>)record.getList(bin2.name);
System.out.println(retList.get(0));
System.out.println(retList.get(1));
System.out.println(retList.get(2));
client.operate(null,key,Operation.add(bin2));
record = client.get(null, key);
System.out.println(record);
client.operate(null,key,Operation.add(bin2));
record = client.get(null, key);
System.out.println(record);
输出:
test:2
2
4
6
(gen:2),(exp:394501613),(bins:(test:8))
(gen:3),(exp:394501613),(bins:(test:10))