3

假设在我的 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 并且只需要一个调用就可以做到这一点?

4

1 回答 1

4

没有直接的 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。

于 2014-12-11T11:25:45.597 回答