aql工具提供了一个类似于sql的命令行接口,用于数据库,UDF和索引管理。aerospike不支持sql查询或管理语言,只是提供一个类似于sql的接口。
aql的使用方法
aql OPTIONS
可以通过aql --help来查看帮助文档,aql --help之后可以看到如下选项:
-h <host>服务器ip,默认为:127.0.0.1
-p <port>端口号。默认为:3000
-c <command>执行指定的命令
-f <filepath>执行的命令在指定的文件中
-v
启用详细输出
-e打开命令的反馈模式
-T <milliseconds>设置命令执行超时时间,单位为ms,默认为1000
-o (json | table)设置输出模型,默认为table
-u <path> 设置
UDF用户管理模块。
-s <path> 设置
系统UDF管理模块
--help帮助
aql使用基于SQL命令语言的语法,对于习惯了关系型数据库的人来说是一个不错的工具。
数据管理
查找所有的namespace
索引管理
创建一个索引
CREATE INDEX <index> ON <ns>[.<set>] (<bin>) <type>
<index>索引名称,在一个命名空间上索引名称必须唯一;
<ns>命名空间;
<set>设置是可选的,如果没有设置,则索引的记录不属于一个set;
<bin>根据bin建立索引
<type>索引存储的类型,必须是:
NUMERIC
or
STRING


删除一个索引
索引状态同步
sync_state
Value | Description |
---|---|
synced | 次级索引和主索引保持一致 |
need_sync | 次级索引和主索引保存不一致,需要同步 |
如果
sync_state=
need_sync and state=RW,则需要进行同步处理
修复索引的方法如下:aql不能提供索引修复的功能,需要用asinfo来处理
[root@mobiead-06 server]# asinfo -v "sindex-repair:ns=test;indexname=numindex;set=testset;"
索引状态
Value | Description |
---|---|
WO | 次级索引只写模式, 一般情况下只能进行更新次级索引,但不能进行查询 |
RW | 次级索引读写模式. 这种模式下可以进行查询 |
查询和扫描管理
启用查询跟踪
关闭查询跟踪
正在运行的查询
查询运行的命令列表:
Managing Queries
正在运行的扫描
结束正在运行的任务
KILL_QUERY <id>
KILL_SCAN <id>
查询记录
SELECT * FROM <ns>[.<set>]
select * from test.myset
SELECT <bin>[, <bin>[, ...]] FROM <ns>[.<set>]
select name, age from test.myset
SELECT <bin>[, <bin>[, ...]] FROM <ns>[.<set>] WHERE [<predicate>]
<bin> BETWEEN <lower> AND <upper>
<bin> = <value>
aql> CREATE INDEX user_age_idx ON test.myset (age) STRING
当age为数字型时:
aql> CREATE INDEX user_age_idx ON test.myset (age)
NUMERIC
聚合查询或扫描结果
touch profile_aggregator.lua
vim profile_aggregator.lua
添加如下内容:(得到最年长的女性)
functionavg_age(stream)localfunctionfemale(rec)return rec.gender == "F"endlocalfunctionname_age(rec)return map{ name=rec.name, age=rec.age }
endlocalfunctioneldest(p1, p2)if p1.age > p2.age thenreturn p1
elsereturn p2
endendreturn stream : filter(female) : map(name_age) : reduce(eldest)
end
ascli udf-put profile_aggregator.lua
AGGREGATE <module>.<function>([<arg>[,...]]) ON <ns>[.<set>] WHERE <predicate>
aql> AGGREGATE profile_aggregator.avg_age() ON users.profiles WHERE age BETWEEN 20 and 29
+--------------------------------------+
+ avg_age |
+--------------------------------------+
+ { "name": "Annie Black", "age": 28 } |
+--------------------------------------+
PS:需要关注一下lua这门小脚本语言,将来可以依靠它在aerospike上进行一些统计,当然用户数据的统计如何进行也要进行详细而有远虑的设计。
记录操作
插入一条记录
INSERTINTO <ns>[.<set>] (PK, <bins>) VALUES (<key>, <values>)
得到一条记录
DELETEFROM <ns>[.<set>] WHERE PK=<key>
所有set的值和属性只能保持在当前的session里面
aql> get <setting>
aql> get output
OUTPUT = TABLE
aql> set <setting> <value>
aql> set output json
可用的设置:
VIEW
输出模型.
VIEW ( TABLE | JSON )
例如
aql> set output table
VERBOSE
启用详细输出
VERBOSE ( TRUE | FALSE )
ECHO
Echo commands.
ECHO ( TRUE | FALSE )
TIMEOUT
查询过期时间,单位是毫秒
TIMEOUT <milliseconds>
RECORD_TTL
以秒为单位的时间,随后创建或更新记录服务器将会在设置的这个时间内有效。
RECORD_TTL <seconds>
LUA_USERPATH
用户管理的lua文件的路径
LUA_USERPATH <path>
LUA_SYSPATH
Aerospike管理的lua文件路径
LUA_SYSPATH <path>
统计
系统统计
aql> stat system
+---------------------------------+--------------------+
| name | value |
+---------------------------------+--------------------+
| "cluster_size" | 1 |
| "cluster_key" | "AA47E9295DD25BC2" |
| "cluster_integrity" | "true" |
| "objects" | 12 |
...
179 rows in set (0.000 secs)
二级索引统计信息
aql> stat index test numindex
+--------------------------+-------+
| name | value |
+--------------------------+-------+
| "keys" | 6 |
| "objects" | 11 |
| "data_memory_used" | 1320 |
| "load_pct" | 100 |
| "loadtime" | 6 |
| "stat_write_reqs" | 11 |
| "stat_write_success" | 11 |
| "stat_write_errs" | 0 |
| "stat_delete_reqs" | 0 |
| "stat_delete_success" | 0 |
| "stat_delete_errs" | 0 |
| "stat_defrag_recs" | 0 |
| "stat_defrag_time" | 0 |
| "n_query" | 6 |
| "avg_selectivity" | 2 |
| "avg_record_size" | 42 |
| "n_aggregation" | 5 |
| "agg_avg_selectivity" | 2 |
| "agg_avg_record_size" | 8 |
| "n_lookups" | 1 |
| "lookup_avg_selectivity" | 6 |
| "lookup_avg_record_size" | 106 |
+--------------------------+-------+
22 rows in set (0.000 secs)
查询统计
aql> stat query
+--------------------------+-------+
| name | value |
+--------------------------+-------+
| "n_query_reqs" | 6 |
| "n_query_success" | 6 |
| "n_query_abort" | 0 |
| "n_query_fail" | 0 |
| "avg_selectivity" | 2 |
| "queue_full" | 0 |
| "n_aggregations" | 5 |
| "n_agg_success" | 5 |
| "n_agg_abort" | 0 |
| "avg_agg_selectivity" | 2 |
| "n_lookups" | 1 |
| "n_lookup_success" | 1 |
| "n_lookup_abort" | 0 |
| "avg_lookup_selectivity" | 6 |
+--------------------------+-------+
14 rows in set (0.000 secs)
UDF管理
注册一个模块
REGISTER MODULE '<filepath>'
aql> register module '~/tmp/my_udf.lua'
aql> show modules
+---------------------------+-------+------------------------+
| module | type | hash |
+---------------------------+-------+------------------------+
| "example1.lua" | "lua" | "033671e05067888fce09" |
| "example2.lua" | "lua" | "07b42082cca8e73a96b2" |
+---------------------------+-------+------------------------+
2 rows in set (0.000 secs)
查看模块信息
DESC MODULE <module>
aql> desc module example2.lua
+----------------------+-------+-------------------------+
| gen | type | content |
+----------------------+-------+-------------------------+
| "1t0IoVM2B/zwAb3QMa" | "LUA" | "CmxvY2FsIGZ1bmN0aW9uIG |
+----------------------+-------+-------------------------+
1 row in set (0.000 secs)
删除一个模块
REMOVE MODULE <module>
aql> remove module example2.lua