AeroSpike基本应用
说明:分两篇介绍,篇为基本概念介绍,第二篇为实际应用;该篇为应用部分。
1、常用操作命令
Aerospike提供一套类SQL操作,和Mysql语法极其相似(命令解析参考注释),在安装aerospike的服务器上,通过aql --help 可以查看系统提供的帮助文档,很详细,里面还有很多DEMO语句。然后通过aql -h ip -p port 连上服务器(直接aql默认连接127.0.0.1 3000),进入下面操作:
--查看配置了哪些ns,和mysql中创建了哪个db对应
aql> show namespaces ;
+--------------+
| namespaces |
+--------------+
| "testMemory" |
| "testFile" |
+--------------+
2 rows in set (0.000 secs)
OK
--插入一条测试数据,demo为as中的set,系统会帮我们创建;类似于mysql中的table
aql> INSERT INTO testMemory.demo (PK, pass, name) VALUES ('key1', 123, 'abc')
OK, 1 record affected.
--bins列相当于mysql table中的column
aql> show bins ;
+-------+--------+-------+--------------+
| quota | bin | count | namespace |
+-------+--------+-------+--------------+
| 32768 | "pass" | 2 | "testMemory" |
| 32768 | "name" | 2 | "testMemory" |
+-------+--------+-------+--------------+
2 rows in set (0.000 secs)
OK
--sets 相当于mysql中的show tables
aql> show sets ;
+------------------+--------------+----------------+---------+-------------------+--------+-------------------+----------+
| disable-eviction | ns | set-enable-xdr | objects | stop-writes-count | set | memory_data_bytes | deleting |
+------------------+--------------+----------------+---------+-------------------+--------+-------------------+----------+
| "false" | "testMemory" | "use-default" | 1 | 0 | "demo" | 34 | "false" |
+------------------+--------------+----------------+---------+-------------------+--------+-------------------+----------+
1 row in set (0.001 secs)
OK
--查询该ns下的所有sets,这种mysql中是不支持的哦
aql> select * from testMemory
+------+-------+
| pass | name |
+------+-------+
| 123 | "abc" |
+------+-------+
1 row in set (0.071 secs)
--对上面的相比,该查询明确指定了set,相当于mysql中对表查询
aql> select * from testMemory.demo
+------+-------+
| pass | name |
+------+-------+
| 123 | "abc" |
+------+-------+
1 row in set (0.077 secs)
--再插入一个set节点对比下
aql> INSERT INTO testMemory.demo1 (PK, pass, name,age) VALUES ('key111', 123, 'abc',12)
OK, 1 record affected.
aql> select * from testMemory
+------+-------+-----+
| pass | name | age |
+------+-------+-----+
| 123 | "abc" | |
| 123 | "abc" | 12 |
+------+-------+-----+
2 rows in set (0.058 secs)
aql> select * from testMemory.demo1
+------+-------+-----+
| pass | name | age |
+------+-------+-----+
| 123 | "abc" | 12 |
+------+-------+-----+
1 row in set (0.080 secs)
--根据PK查询,相当于mysql表中的主键,AS还支持索引查询,但不支持不带索引的bin查询
aql> select * from testMemory.demo where PK='key1'
+------+-------+
| pass | name |
+------+-------+
| 123 | "abc" |
+------+-------+
1 row in set (0.001 secs)
--根据PK删除记录,AS中的更新其实就是覆盖
aql> delete from testMemory.demo where PK='key1'
OK, 1 record affected.
aql> select * from testMemory.demo
0 rows in set (0.082 secs)
--关于索引,没有建立索引时是不能根据bin查询的
aql> select * from testMemory.demo1 where name='abc'
0 rows in set (0.003 secs)
Error: (201) AEROSPIKE_ERR_INDEX_NOT_FOUND
aql> CREATE INDEX idx_demo1_name ON testMemory.demo1 (name) STRING
OK, 1 index added.
--建立索引后则可以
aql> select * from testMemory.demo1 where name='abc'
+------+-------+-----+
| pass | name | age |
+------+-------+-----+
| 123 | "abc" | 12 |
+------+-------+-----+
1 row in set (0.025 secs)
2、Java客户端操作
2.1 引入配置
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>aerospike-client</artifactId>
<version>3.2.2.bs</version>
</dependency>
2.2 Junit测试
public class ASTest {
AerospikeClient client = null;
@Before
public void init() {
// 参数是AS服务地址,第二参数是对外端口
client = new AerospikeClient("192.168.75.142", 3000);
}
/** 测试写AS */
@Test
public void testSave() {
// policy 是用来定义策略,AS中定义多种策略实现(比如超时,过期机制,事务等),也可设置为null
WritePolicy policy = new WritePolicy();
for (int i = 100; i < 110; i++) {
// Key个参数ns,第二个参数set,第三个参数用来标识的PK
Key key = new Key("testMemory", "demo", "zhangsan" + i);
// bin可以很多个,多到参考我们受限个数
Bin nameBin = new Bin("name", "张三" + i);
Bin passBin = new Bin("pass", "123456");
// expire这是一个伪的过期时间
Bin expireBin = new Bin("expire", System.currentTimeMillis() + 1 * 60 * 60 * 1000L);
// 这才是真的过期策略,小于105的100秒后会消失,可以通过命令查看
if (i < 105) {
policy.timeout = 50;
policy.expiration = 100; // 重要的事情只说一遍,单位(秒)
} else {
policy.expiration = -1; // 不过期
}
client.put(policy, key, nameBin, passBin, expireBin);
}
}
/** 测试读AS,而且是批量读 */
@Test
public void testQuery() {
String[] bin_names = { "name", "pass" }; // 查询字段
Key[] keys = new Key[10]; // 查询key
int index = 0;
for (int i = 100; i < 110; i++) {
keys[index++] = new Key("testMemory", "demo", "zhangsan" + i);
}
Record[] recordArr = client.get(null, keys, bin_names); // 批量查询
// client.get(null, keys); 后一个参数不传时,将把所有的bins全查出来
for (Record r : recordArr) {
if (r != null) {
System.out.println(">>>name:" + r.getString("name") + "---pass:" + r.getString("pass"));
}
}
}
}
2.3 查看测试结果
aql> select * from testMemory.demo
+-------------+----------+---------------+
| name | pass | expire |
+-------------+----------+---------------+
| "张三103" | "123456" | 1566466510845 |
| "张三107" | "123456" | 1566466510847 |
| "张三108" | "123456" | 1566466510847 |
| "张三106" | "123456" | 1566466510847 |
| "张三102" | "123456" | 1566466510845 |
| "张三101" | "123456" | 1566466510823 |
| "张三100" | "123456" | 1566466510813 |
| "张三104" | "123456" | 1566466510846 |
| "张三105" | "123456" | 1566466510846 |
| "张三109" | "123456" | 1566466510848 |
+-------------+----------+---------------+
10 rows in set (0.064 secs)
aql> select * from testMemory.demo
+-------------+----------+---------------+
| name | pass | expire |
+-------------+----------+---------------+
| "张三107" | "123456" | 1566466510847 |
| "张三108" | "123456" | 1566466510847 |
| "张三106" | "123456" | 1566466510847 |
| "张三105" | "123456" | 1566466510846 |
| "张三109" | "123456" | 1566466510848 |
+-------------+----------+---------------+
5 rows in set (0.056 secs)
————————————————
版权声明:本文为CSDN博主「两只小猴」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dycsos123/article/details/100050839
相关文章