[英]Best way to update single field in Aerospike
I have an Aerospike cache consists of list of data with value of json like structure.我有一个 Aerospike 缓存,其中包含类似结构的 json 值的数据列表。
example value: {"name": "John", "count": 10}
示例值:
{"name": "John", "count": 10}
I wanted to scan for all records and reset count to zero.我想扫描所有记录并将计数重置为零。 What would be a good option to handle this problem?
什么是处理这个问题的好选择?
I think the best way to do this would be to leverage the background operations... I just tried the following code on the Aerospike sandbox and it did update all the 'shape' entries in the 'report' map to updated shape
.我认为最好的方法是利用后台操作......我刚刚在Aerospike 沙箱上尝试了以下代码,它确实将“报告”map 中的所有“形状”条目更新为
updated shape
。 (Disclaimer: I am not a developer so there may be really bad ways of doing things in Java in general but this hopefully shows how to do a background operation scan in Aerospike) (免责声明:我不是开发人员,所以通常在 Java 中可能存在非常糟糕的做事方式,但这希望能展示如何在 Aerospike 中进行后台操作扫描)
Run the 'setup' tab and then copy paste this in the create tab (or in the blank tab) and run:运行“设置”选项卡,然后将其复制粘贴到创建选项卡(或空白选项卡)并运行:
import com.aerospike.client.task.ExecuteTask;
import com.aerospike.client.task.Task;
import com.aerospike.client.query.Statement;
import com.aerospike.client.query.Filter;
import com.aerospike.client.Operation;
import com.aerospike.client.exp.Exp;
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
try {
WritePolicy writePolicy = new WritePolicy();
MapPolicy mapPolicy = new MapPolicy();
String mapBinName = "report";
String mapKeyName = "shape";
Statement stmt = new Statement();
stmt.setNamespace("sandbox");
stmt.setSetName("ufodata");
ExecuteTask task = client.execute(
writePolicy, stmt, MapOperation.put(mapPolicy, mapBinName,
Value.get(mapKeyName), Value.get("updated shape")));
if(task != null) {
System.out.format("Task ID: %s\nTask Status: %s",
task.getTaskId(), task.queryStatus());
}
else {
System.out.format("Task seems null!");
}
}
catch (AerospikeException ae) {
System.out.format("Error: %s", ae.getMessage());
}
You can change this to a secondary index query (if defined) or add further filters on the map bin itself...您可以将其更改为二级索引查询(如果已定义)或在 map bin 本身上添加更多过滤器...
Here are a couple of tutorials that may help:以下是一些可能会有所帮助的教程:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.