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