CQL中默認忽略大小寫,若須要大小寫敏感,可以使用雙引號將對象包起來,引用的時候也要用雙引號包住正則表達式
tips: 使用CQL須要預裝Python環境算法
這邊和關係型數據庫相近的就不列出來了,只列出Cassandra特點的,重要的數據庫
CQL類型 | 經常使用類型 | 說明 |
---|---|---|
list(T) | n/a | 有序集合,T能夠是任意分集合CQL數據類型,例如,int,text等 |
map(K,V) | n/a | 哈希表,K和V能夠是任意非集合CQL數據類型,例如,int,text等 |
set(T) | n/a | 無序集合,T能夠是任意分集合CQL數據類型,例如,int,text等 |
字母或數字開頭,知足正則表達式[a-zA-Z0-9_]*緩存
定義column和keyspace時候不能使用關鍵字和保留字,必定要用可使用雙引號包起來,但不建議這麼用app
具體的關鍵字和保留字見官網表格ide
語法:create keyspace (if not exists)? <identifier> with <properties>; 注意:identifier長度須要小於等於32,默認大小寫不敏感,可使用雙引號讓它對大小寫敏感 create keyspace test with replication = {'class':'SimpleStrategy', 'replication_factor': 3} and durable_writes = true; 查看當前全部KeySpace desc keyspaces; 查看KeySpace的建立語句 desc <identifier>; 修改KeySpace alter keyspace <identifier> with <properties>; 切換KeySpace use <identifier>; 刪除名爲teset的KeySpace drop keyspace (if exsits)? <identifier>;
create keyspace的兩個屬性函數
複製策略(SimpleStrategy(單一數據中心,測試用),NetworkTopologyStrategy(默認,強烈推薦,方便數據擴展),OldNetworkTopologyStrategy(官方已棄用))
這個屬性是強制的,至少包括class屬性,其餘屬性依class改變,replication決定了多節點的狀況下,新寫入的數據如何在節點之間複製保存
replication_factor屬性,他是SimpleStrategy這種策略的一個屬性,叫作副本因子,決定了每一個row有多少個副本,這個值不能夠超過節點數post
是否使用commit log持久化寫入,默認爲true測試
語法:create (table|columnfamily) (if not exists)? <tablename> '('<column-definition>(','<column-definiton>)*')' (with<option>(and <option>)*)?; 通常把第一個column做爲primary key,看成行的標識,也就是row key,也能夠指定多個列組成複合鍵 create table timeline( userid uuid, posted_month int, poster_time uuid, body text, poster_by text, primary key(userid, posted_month, posted_time) )with compaction = {'class':'LeveldCompactionStrategy'}; 查看column family的建立語句 desc <tablename>; 修改column family alter (table|columnfamily)<tablename><instruction>; alter table table_name add columnname varchar; alter table table_name drop columnname; alter table table_name with comment = 'xxx' and read_repair_chance = 0.2; alter table table_name rename old_column_name to new_column_name; 下面這種修改column數據類型的語法新版本已再也不支持 alter table table_name alter column_name type uuid; 刪除column family drop table (if exists)? <tablename>; 清空column family truncate <tablename>;
column family的屬性ui
接在with後面,瞭解便可
屬性 | 說明 |
---|---|
commnet | 對column family的描述信息 |
bloom_filter_fp_chance | 指定bloom_filter算法的容錯率,通常設置爲0.01或者0.1 |
caching | 設置緩存方案 |
compactioin | 數據壓縮策略 |
compression | 數據壓縮算法 |
default_time_to_live | 存活時間,單位是秒,默認0(永久存活) |
memtable_flush_period_in_ms | 內存數據刷新時間間隔 |
read_repair_chance | 0-1之間的數值,與數據一致性有關 |
注意
語法:create (custom)? index (if not exists)? (<indexname>)? on <tablename> '('<index-identifier>')' (using<string>(with options=<map-literal>)?)?; create index idx_name on columnfamily_name(column_name); create index on columnfamily_name(column_name); 給一個默認idx_name create index on columnfamily_name(keys(column_name)); 針對map類型column的鍵值進行索引 create custom index on columnfamily_name(column_name) using 'path.to.the.IndexClass'; create custom index on columnfamily_name(column_name) using 'path.to.the.IndexClass' with options = {'storage':'/mnt/ssd/indexes'}; 刪除index drop index(if exsists)?(<keyspace>'.')?<identifier>;
語法:create type (if not exists)? <typename> '('<field-definition>(',' <field-definiton>)*')'; 修改type alter type <typename> <instruction>; alter type type_name alter zip type varint; alter type type_name add xxx text; alter type type_name rename old_name to new_name and old2_name to new2_name; 查看當前全部自定義type desc types; 查看指定type信息 desc type type_name; 刪除type drop type (if exsists)? <typename>; 注意:若是type還在使用,drop會報錯
INSERT insert into <tablename> '('<identifier>(',' <identifier>)*')' values '('<term-or-literal>(',' <term-or-literal>)*')' (if not exists)? (using <option> (AND <option>)*)? using ttl 86400 表示這行數據過了86400秒自動刪除 cassandra中沒有duplicate,插入數據的主鍵已經存在,則會將老數據直接覆蓋 UPDATE update <tablename> (using <option> (AND <option>*))? set <assignment> (',' <assignment>)* where <where-clause> (if <condition> (AND condition)*)? update的using語句是放在set以前,不在最後,這點要和insert區別 DELETE delete (<selection> (',' <selection>)*)? from <tablename> (using timestamp <integer>)? where <where-clause> (if (exists|(<condition> (AND <condition>)*)))? delete from column_family_name where column_name = xxx; delete column_name from column_family_name where column_name = xxx; BATCH 批量操做,要麼所有成功,要麼所有失敗 begin (unlogged|counter) batch (using <option> (and <options>)*)? <modification_statement> (';' <modification_statement>)* apply batch
語法: select <select-clause> from <tablename> (where <where-clause>)? (order by <order-by>)? (limit <integer>)? (allow filtering)? select column_name from column_family_name where column_name in (xxx,xxx,xxx); select column_name as xxx from column_family_name; select column_name from column_family_name where column_name = 'xxx' and column_name > 'xxx'; select count(*) from column_family_name; select count(*) as xxx from column_family_name;
select注意點