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>

转载于:https://www.cnblogs.com/yun1233/p/10388047.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值