Aerospike删除列表中的记录(如果bin的元素已知)



我有一个集,它有三个bin(PK、cat_id和data)。列表索引应用于cat_id。我可以通过查询选择记录:

SELECT * FROM test.myset IN LIST where cat_id = '1'

它对我来说很好。现在我需要按同样的条件删除这个记录。但正如我所读到的,PK是删除任何记录所必需的。在我的情况下,我有cat_id来删除这个记录。

帮助我通过使用bin元素而不是PK来删除此记录。我正在使用PHP。AQL也适用于我。

您将使用一个异步后台查询,该查询将一个微小的(记录UDF)Lua函数应用于该查询谓词匹配的每个记录。在PHP客户端中,您可以使用queryApply()方法来实现这一点。

这个Lua函数简单地说"死":

function del_rec(rec)
    aerospike:remove(rec)
end

对于这个"复杂"函数,我使用了Lua UDF API引用,主要是Aeropike对象引用。如果你想做更多的逻辑,比如检查其他垃圾箱,你应该使用记录方法。Aerospike网站上有更多关于UDF和开发Record UDF的信息。

在集群中注册UDF模块(包含此函数的文件)后,可以从PHP代码中调用它:

$where = Aerospike::predicateContains("cat_id", Aerospike::INDEX_TYPE_LIST, 1);
$status = $client->queryApply("test", "mytest", $where, "my_udfs", "del_rec", [], $job_id);
if ($status === Aerospike::OK) {
    var_dump("The background job ID is $job_id");
} else {
    echo "An error occured while initiating the background query [{$client->errorno()}] ".$client->error();
}

最新更新