java-Aerospike:在一个调用中从LDT Bin中检索一组密钥
作者:互联网
假设在我的LDT(LargeMap)Bin中,我具有以下值,
key1, value1
key2, value2
key3, value3
key4, value4
. .
key50, value50
现在,我使用以下代码段获取所需的数据:
Map<?, ?> myFinalRecord = new HashMap<?, ?>();
// First call to client to get the largeMap associated with the bin
LargeMap largeMap = myDemoClient.getLargeMap(myPolicy, myKey, myLDTBinName, null);
for (String myLDTKey : myRequiredKeysFromLDTBin) {
try {
// Here each get call results in one call to aerospike
myFinalRecord.putAll(largeMap.get(Value.get(myLDTKey)));
} catch (Exception e) {
log.warn("Key does not exist in LDT Bin");
}
}
如果myRequiredKeysFromLDTBin包含20个密钥,那么问题就出在这里.然后largeMap.get(Value.get(myLDTKey))将对Aerospike进行20次调用.
因此,如果我每笔事务的检索时间为1毫秒,那么我从记录中检索20个ID的一次调用将导致对Aerospike的20次调用.这将使我的响应时间增加到大约. 20毫秒!
那么,有什么方法可以让我传递一组要从LDT Bin中检索的ID,而只需要一个调用就可以了?
解决方法:
没有直接的API可以进行多重获取.一种方法是直接通过UDF从服务器多次调用lmap API.
示例“ mymap.lua”
local lmap = require('ldt/lib_lmap');
function getmany(rec, binname, keys)
local resultmap = map()
local keycount = #keys
for i = 1,keycount,1 do
local rc = lmap.exists(rec, binname, keys[i])
if (rc == 1) then
resultmap[keys[i]] = lmap.get(rec, binname, keys[i]);
else
resultmap[keys[i]] = nil;
end
end
return resultmap;
end
注册这个lua文件
aql> register module 'mymap.lua'
OK, 1 module added.
aql> execute lmap.put('bin', 'c', 'd') on test.demo where PK='1'
+-----+
| put |
+-----+
| 0 |
+-----+
1 row in set (0.000 secs)
aql> execute lmap.put('bin', 'b', 'c') on test.demo where PK='1'
+-----+
| put |
+-----+
| 0 |
+-----+
1 row in set (0.001 secs)
aql> execute mymap.getmany('bin', 'JSON["b","a"]') on test.demo where PK='1'
+--------------------------+
| getmany |
+--------------------------+
| {"a":NIL, "b":{"b":"c"}} |
+--------------------------+
1 row in set (0.000 secs)
aql> execute mymap.getmany('bin', 'JSON["b","c"]') on test.demo where PK='1'
+--------------------------------+
| getmany |
+--------------------------------+
| {"b":{"b":"c"}, "c":{"c":"d"}} |
+--------------------------------+
1 row in set (0.000 secs)
Java代码来调用这个
try {
resultmap = myClient.execute(myPolicy, myKey, 'mymap', 'getmany', Value.get(myLDTBinName), Value.getAsList(myRequiredKeysFromLDTBin)
} catch (Exception e) {
log.warn("One of the key does not exist in LDT bin");
}
如果键存在,则将设置值;如果不存在,则将返回NIL.
标签:aerospike,java 来源: https://codeday.me/bug/20191028/1955739.html