我有一个 Aerospike 缓存,由具有 json 结构值的数据列表组成。
示例值:{"name": "John", "count": 10}
我想扫描所有记录并将计数重置为零。解决这个问题的好选择是什么?
最佳答案
我认为最好的方法是利用后台操作...我刚刚在 Aerospike sandbox 上尝试了以下代码它确实将“报告”映射中的所有“形状”条目更新为更新的形状
。 (免责声明:我不是开发人员,因此一般来说,在 Java 中做事的方法可能非常糟糕,但这希望展示如何在 Aerospike 中进行后台操作扫描)
运行“设置”选项卡,然后将其复制粘贴到创建选项卡(或空白选项卡)中并运行:
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());
}
您可以将其更改为二级索引查询(如果已定义)或在 map 箱本身上添加更多过滤器...
这里有一些教程可能会有所帮助:
- Working with maps .
- Expressions in Aerospike 。 (关于在 map 箱内更新什么/如何更新的更奇特的事情)
关于spring-boot - 在 Aerospike 中更新单个字段的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73282994/