Cassandra数据库模糊查询



前言

安装Cassandra

安装Cassandra数据库

  1. 官网下载Cassandra压缩包
  2. 解压,并配置环境变量:
操作 变量名 变量值
新建 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, 'aaa@20.com');
cqlsh> insert into test.user (name, age, email) values ('no_aaa', 21, 'no_aaa@21.com');
cqlsh> insert into test.user (name, age, email) values ('bbb', 22, 'bbb@22.com');
cqlsh> insert into test.user (name, age, email) values ('no_bbb', 21, 'no_bbb@21.com');
cqlsh> insert into test.user (name, age, email) values ('ccc', 23, 'ccc@23.com');
cqlsh>

查询所有记录

cqlsh> select * from test.user;

 name   | age | email
--------+-----+---------------
 no_aaa |  21 | no_aaa@21.com
    aaa |  20 |    aaa@20.com
    bbb |  22 |    bbb@22.com
    ccc |  23 |    ccc@23.com
 no_bbb |  21 | no_bbb@21.com

(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 | no_aaa@21.com
 no_bbb |  21 | no_bbb@21.com

(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 | no_aaa@21.com
    aaa |  20 |    aaa@20.com

(2 rows)
cqlsh>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM