Cassandra 单机入门例子——有索引

入门例子:

http://wiki.apache.org/cassandra/GettingStarted

添加环境变量并source生效,使得可以在任意位置执行cassandra/bin安装目录下的命令

1
2
export CASSANDRA_HOME="/Users/zhengqh/Soft/apache-cassandra-2.0.16"
export PATH="$CASSANDRA_HOME/bin:$PATH"

前台启动Cassandra进程, sudo cassandra -f

启动一个新的终端, 启动客户端查询: cqlsh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE KEYSPACE mykeyspace  
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

USE mykeyspace;
CREATE TABLE users (
user_id int PRIMARY KEY,
fname text,
lname text
);

INSERT INTO users (user_id, fname, lname) VALUES (1745, 'john', 'smith');
INSERT INTO users (user_id, fname, lname) VALUES (1744, 'john', 'doe');
INSERT INTO users (user_id, fname, lname) VALUES (1746, 'john', 'smith');

CREATE INDEX ON users (lname);
SELECT * FROM users WHERE lname = 'smith';

 

输出:

cqlsh:mykeyspace> SELECT * FROM users WHERE lname = 'smith';

 user_id | fname | lname
---------+-------+-------
    1745 |  john | smith
    1746 |  john | smith

(2 rows)
cqlsh:mykeyspace>  SELECT * FROM users;

 user_id | fname | lname
---------+-------+-------
    1745 |  john | smith
    1744 |  john |   doe
    1746 |  john | smith

(3 rows)

 

观察服务端的输出: 

1
2
3
4
INFO 09:29:21,981 Create new Keyspace: mykeyspace, rep strategy:SimpleStrategy{}, strategy_options: {replication_factor=1}, durable_writes: true
INFO 09:29:45,822 Initializing mykeyspace.users
INFO 09:31:21,102 Initializing mykeyspace.users.users_lname_idx
INFO 09:31:21,230 Index build of users.users_lname_idx complete

然后看目录文件:

ls mykeyspace/users

结果:

 mykeyspace-users-jb-1-CompressionInfo.db
 mykeyspace-users-jb-1-Data.db
 mykeyspace-users-jb-1-Filter.db
 mykeyspace-users-jb-1-Index.db
 mykeyspace-users-jb-1-Statistics.db
 mykeyspace-users-jb-1-Summary.db
 mykeyspace-users-jb-1-TOC.txt
 mykeyspace-users.users_lname_idx-jb-1-CompressionInfo.db
 mykeyspace-users.users_lname_idx-jb-1-Data.db
 mykeyspace-users.users_lname_idx-jb-1-Filter.db
 mykeyspace-users.users_lname_idx-jb-1-Index.db
 mykeyspace-users.users_lname_idx-jb-1-Statistics.db
 mykeyspace-users.users_lname_idx-jb-1-Summary.db
 mykeyspace-users.users_lname_idx-jb-1-TOC.txt

如果是针对没有建立索引的字段搜索则会提示:

cqlsh:mykeyspace> SELECT * FROM users WHERE fname = 'smith';
InvalidRequest: Error from server: code=2200 [Invalid query] message="Cannot execute this query as it might involve data filtering and thus may have unpredictab
le performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING"

 

posted @ 2017-01-12 15:29  bonelee  阅读(1747)  评论(2编辑  收藏  举报