设为首页 加入收藏

TOP

AerospikeC客户端手册―――数据扫描―记录变更
2015-07-24 11:21:13 来源: 作者: 【 】 浏览:3
Tags:AerospikeC 客户端 手册 数据 扫描 记录 变更

记录变更

Aerospike提供能力来扫描 数据库中的记录并通过用户定义函数(UDF)变更每条记录数据。
在SQL中,不带条件变更表中所有记录的UPDATE语句是:
UPDATE test.demo
SET a = 1, b = 2, c = 3
Aeropike提供与上面SQL类似的功能,允许应用一个函数到每条记录,通过函数来变更记录数据。这个函数,被称作UDF(用户定义函数),被应用到单条记录。它可以提供参数,能读取和写入记录bin值与执行计算。
目前,扫描并应用UDF到每条记录只能在后台执行,就是说客户端把扫描请求发给数据库,但不用等待结果。扫描请求被放入队列,并且数据库中执行,没有结果返回到客户端。客户端会得到一个id,用来标识所发送扫描请求对应的任务,可用这个id检查扫描的状态。

定义扫描

除了能定义一个应用到每条记录的UDF,用于变更记录内容的扫描定义与正常的扫描定义相同,并且是使用后台扫描操作:aerospike_scan_background()。
我们象在【记录扫描】章节中所做的类似,先构建一个扫描对象,但不需要去选择bin,而且增加一个as_scan_apply_each()操作:
as_scan scan; as_scan_init(&scan, "test", "demo"); as_scan_apply_each(&scan, "mymodule", "mytransform", NULL);
解释:
第1行 — 在栈上创建一个as_scan对象。
第2行 — 初始化扫描,被扫描数据所在的namespace名称为“test”、set名称为“demo“
第4行 — 增加一个用户定义函数(UDF),此函数被应用到每条扫描到的记录。UDF函数mytransform()定义于模块mymodule。在此例中,UDF不需要任何参数。

Defining the Record UDF 定义记录UDF

The following is the mytransform" Record UDF, defined inmymodule`. It is a pretty simple Record UDF, mimicking the SQL statement above.

下面所示是定义于模块mymodule中的UDF,名称是mytransform,它相当简单,模仿上述SQL语句的行为。

function mytransform(rec) rec['a'] = 1 rec['b'] = 2 rec['c'] = 3 aerospike:update(rec) end

A more elaborate function might be to increment "a", calculate "b" based on "a", and calculate "c" based on the values of "a" and "b":

稍微复杂点儿的函数可增加"a"、基于”a”计算”b",并基于"a"和“b“的数值计算”c”:

function mytransform(rec) rec['a'] = rec['a'] + 1 rec['b'] = rec['a'] * 2 rec['c'] = rec['a'] + rec['b'] aerospike:update(rec) end

执行扫描

此扫描可使用areospike_scan_background()执行:

uint64_t scan_id = 0; if (aerospike_scan_background(&as, &err, NULL, &scan, &scan_id) != AEROSPIKE_OK) { fprintf(stderr, "error(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line); }

scan_id用来查询扫描的状态。scand_id为0是未定义,所以扫描操作将生成并设置scan_id的值。

检查扫描的状态

可使用aerospike_scan_info()查询扫描的状态,状态信息将填充到一个as_scan_info对象中。

as_scan_info scan_info; if (aerospike_scan_info(&as, &err, NULL, scan_id, &scan_info) != AEROSPIKE_OK) { fprintf(stderr, "error(%d) %s at [%s:%d]\n", err.code, err.message, err.file, err.line); }

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇"log file sync"等待事.. 下一篇iReport+jasperreport创建子表的..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何在 C 语言中管理 (2025-12-25 03:20:14)
·C语言和内存管理有什 (2025-12-25 03:20:11)
·为什么C语言从不被淘 (2025-12-25 03:20:08)
·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)