充气塞中的仓是Map[String -> List]
我正在努力实现这样的行为:
bin.computeIfAbsent(key, k -> new List()).addAll(itemsToAdd)
有没有一种方法可以在Aerospike中原子化地实现这一点而不实现UDF?如果我正确阅读了文档,如果它是Map[String -> Map]
,我本可以根据需要使用CTX.mapKeyCreate
来创建内部Map,但我看不到任何类似于创建List 的东西
UPD:以下是我尝试做的
我有一连串的三元组:
{"pk","attr","value"}
我需要将此流放入由pk
和attr
聚合的Aeropike集合中,格式如下:
{
"PK":"pk",
"mapBin": {
"attr": ["value"]
}
}
假设流中有三项:{"pk","attr1","value1"},{"pk","attr2","value2"},{"pk","attr1","value3"}
他们需要这样降落在收费公路上:
{
"PK":"pk",
"mapBin": {
"attr1": ["value1","value3"],
"attr2": ["value2"]
}
}
要插入新项目{"pk","attr2","value2"}
,我需要执行以下几个操作:
- 在
mapBin[attr2]
获取列表 - 如果不存在,请在映射中插入空列表
- 执行
ListOperation.append
将项目附加到现有列表
问题是:有没有一种方法可以在没有UDF的情况下实现原子化?
您应该能够使用以下(Java语法(以原子方式执行这些操作:
record = client.operate(null, pk,
ListOperation.append("mapBin", Value.get("value2"),
CTX.mapKeyCreate(Value.get("attr2"),
MapOrder.UNORDERED)));
如果有多个项目要附加到一个列表,并且在同一个运算符((中映射中的不同列表有多个ListOperation,则可以使用ListOperation.appendItems((,所有这些操作都将原子执行。
我测试了上面@assnozzle提供的解决方案。(服务器5.7.0.11,Java客户端5.1.11(。(我把PK:PK作为k:v放在mapBin中。我想你想把它放在自己的bin中。(
package com.aerospike;
import com.aerospike.client.*;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.client.cdt.MapPolicy;
import com.aerospike.client.cdt.CTX;
import com.aerospike.client.cdt.MapReturnType;
import com.aerospike.client.cdt.MapOrder;
import com.aerospike.client.cdt.ListOperation;
import com.aerospike.client.cdt.MapOperation;
public class TestExample {
public static void main(String[] args) throws Exception {
TestExample ce = new TestExample();
ce.runExample();
return;
}
public void runExample() throws Exception {
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
insertTuple(client, "pk", "attr1", "a1v1");
insertTuple(client, "pk", "attr1", "a1v2");
insertTuple(client, "pk", "attr2", "a2v1");
insertTuple(client, "pk", "attr2", "a2v2");
insertTuple(client, "pk", "attr2", "a2v3");
insertTuple(client, "pk", "attr3", "a3v1");
}
private void insertTuple(AerospikeClient client, String s1, String s2, String s3) throws Exception {
Key key = new Key("test", "testset", "k1");
Record record = client.get(null, key);
System.out.println("nInitial Record: " + record);
// Insert tuple data
WritePolicy wPolicy = new WritePolicy(client.writePolicyDefault);
MapPolicy mPolicy = new MapPolicy();
client.operate(wPolicy, key,
MapOperation.put(mPolicy, "mapBin", Value.get("PK"), Value.get(s1)),
ListOperation.append("mapBin", Value.get(s3), CTX.mapKeyCreate(Value.get(s2), MapOrder.UNORDERED))
);
record = client.get(null,key);
System.out.println("Get Record: " + record);
}
}
输出为:
Initial Record: null
Get Record: (gen:1),(exp:386490396),(bins:(mapBin:{attr1=[a1v1], PK=pk}))
Initial Record: (gen:1),(exp:386490396),(bins:(mapBin:{attr1=[a1v1], PK=pk}))
Get Record: (gen:2),(exp:386490396),(bins:(mapBin:{attr1=[a1v1, a1v2], PK=pk}))
Initial Record: (gen:2),(exp:386490396),(bins:(mapBin:{attr1=[a1v1, a1v2], PK=pk}))
Get Record: (gen:3),(exp:386490396),(bins:(mapBin:{attr2=[a2v1], attr1=[a1v1, a1v2], PK=pk}))
Initial Record: (gen:3),(exp:386490396),(bins:(mapBin:{attr2=[a2v1], attr1=[a1v1, a1v2], PK=pk}))
Get Record: (gen:4),(exp:386490396),(bins:(mapBin:{attr2=[a2v1, a2v2], attr1=[a1v1, a1v2], PK=pk}))
Initial Record: (gen:4),(exp:386490396),(bins:(mapBin:{attr2=[a2v1, a2v2], attr1=[a1v1, a1v2], PK=pk}))
Get Record: (gen:5),(exp:386490396),(bins:(mapBin:{attr2=[a2v1, a2v2, a2v3], attr1=[a1v1, a1v2], PK=pk}))
Initial Record: (gen:5),(exp:386490396),(bins:(mapBin:{attr2=[a2v1, a2v2, a2v3], attr1=[a1v1, a1v2], PK=pk}))
Get Record: (gen:6),(exp:386490396),(bins:(mapBin:{PK=pk, attr2=[a2v1, a2v2, a2v3], attr1=[a1v1, a1v2], attr3=[a3v1]}))