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' : }; 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
---------+-------+-------
| john | smith
| john | smith ( rows)
cqlsh:mykeyspace> SELECT * FROM users; user_id | fname | lname
---------+-------+-------
| john | smith
| john | doe
| john | smith ( 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--CompressionInfo.db
mykeyspace-users-jb--Data.db
mykeyspace-users-jb--Filter.db
mykeyspace-users-jb-1-Index.db
mykeyspace-users-jb--Statistics.db
mykeyspace-users-jb--Summary.db
mykeyspace-users-jb--TOC.txt
mykeyspace-users.users_lname_idx-jb--CompressionInfo.db
mykeyspace-users.users_lname_idx-jb--Data.db
mykeyspace-users.users_lname_idx-jb--Filter.db
mykeyspace-users.users_lname_idx-jb-1-Index.db
mykeyspace-users.users_lname_idx-jb--Statistics.db
mykeyspace-users.users_lname_idx-jb--Summary.db
mykeyspace-users.users_lname_idx-jb--TOC.txt

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

cqlsh:mykeyspace> SELECT * FROM users WHERE fname = 'smith';
InvalidRequest: Error from server: code= [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"
上一篇:PIC和PIE


下一篇:python学习第3天