目录
- 前言
- 安装Cassandra
- 准备一张表和若干记录
- 模糊查询1(疑问)
- 模糊查询2
- 模糊查询3
前言
- 操作系统win10
- 时间2019年02月
- Python版本:Python 3.5.2
- Cassandra版本:cassandra 3.11.3
- 参考网址1
- 参考网址2
安装Cassandra
安装Cassandra数据库
- 官网下载Cassandra压缩包
- 解压,并配置环境变量:
操作 | 变量名 | 变量值 |
---|---|---|
新建 | CASSANDRA_HOME | 解压路径 |
增加 | PATH | 解压路径\bin; |
测试
C:\Users\wahaha>cassandra
准备一张表和若干记录
创建一张表
cqlsh> create table test.user (name text primary key, age int, email text)
cqlsh>
插入若干记录
cqlsh> insert into test.user (name, age, email) values ('aaa', 20, '[email protected]');
cqlsh> insert into test.user (name, age, email) values ('no_aaa', 21, '[email protected]');
cqlsh> insert into test.user (name, age, email) values ('bbb', 22, '[email protected]');
cqlsh> insert into test.user (name, age, email) values ('no_bbb', 21, '[email protected]');
cqlsh> insert into test.user (name, age, email) values ('ccc', 23, '[email protected]');
cqlsh>
查询所有记录
cqlsh> select * from test.user;
name | age | email
--------+-----+---------------
no_aaa | 21 | [email protected]
aaa | 20 | [email protected]
bbb | 22 | [email protected]
ccc | 23 | [email protected]
no_bbb | 21 | [email protected]
(5 rows)
cqlsh>
模糊查询1(疑问)
对某一列进行配置
需要对哪一列进行模糊查询,例如,本文就对email列进行模糊查询
本次配置可以模糊查询以xx字符开头的记录
疑问:无法对设置为primary key的字段进行模糊查询配置
cqlsh> create custom index user_name_idx ON test.user (name) USING 'org.apache.cassandra.index.sasi.SASIIndex' WITH OPTIONS = {'analyzer_class': 'org.apache.cassandra.index.sasi.analyzer.StandardAnalyzer', 'case_sensitive': 'false'};
InvalidRequest: Error from server: code=2200 [Invalid query] message="Cannot create secondary index on partition key column name"
cqlsh>
模糊查询2
对某一列进行配置
需要对哪一列进行模糊查询,例如,本文就对email列进行模糊查询
本次配置可以模糊查询以xx字符开头的记录
cqlsh> create custom index user_email_idx ON test.user (email) USING 'org.apache.cassandra.index.sasi.SASIIndex' WITH OPTIONS = {'analyzer_class': 'org.apache.cassandra.index.sasi.analyzer.StandardAnalyzer', 'case_sensitive': 'false'};
cqlsh>
测试模糊查询
模糊查询email字段以no开头的所有记录
cqlsh> select * from test.user where email like 'no%';
name | age | email
--------+-----+---------------
no_aaa | 21 | [email protected]
no_bbb | 21 | [email protected]
(2 rows)
cqlsh>
模糊查询3
对某一列进行配置
需要对哪一列进行模糊查询,例如,本文就对email列进行模糊查询
本次配置可以模糊查询包含xx字符的记录
cqlsh> create custom index user_email_idx ON test.user (email) USING 'org.apache.cassandra.index.sasi.SASIIndex' WITH OPTIONS = {'mode': 'CONTAINS', 'analyzer_class': 'org.apache.cassandra.index.sasi.analyzer.StandardAnalyzer', 'case_sensitive': 'false'};
cqlsh>
测试模糊查询
模糊查询email字段包含aaa的所有记录
cqlsh> select * from test.user where email like '%aaa';
name | age | email
--------+-----+---------------
no_aaa | 21 | [email protected]
aaa | 20 | [email protected]
(2 rows)
cqlsh>