Aerospike:在一次调用中从LDT Bin中检索一组键

时间:2014-12-10 11:22:48

标签: java aerospike

假设在我的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))将拨打20个电话来进行空袭。

因此,如果我按照每个事务1毫秒的检索时间,那么我在一次从记录中检索20个ID的调用将导致20次对aerospike的调用。这会将我的响应时间增加到约。 20毫秒

那么有什么方法可以传递一组id来从LDT Bin中检索,只需要一次调用就可以了吗?

1 个答案:

答案 0 :(得分:4)

没有直接的API可以进行多次获取。这样做的方法是直接从服务器通过UDF调用lmap API。

示例&#39; mymap.lua&#39;

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。