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注意点