cassandra CQL 常用操作


1. CQL客戶端鏈接

   bin/cqlsh ip username password 

 

2. 

(1)建立keyspace語句,keyspace類似於 mysql 中的數據庫,一個數據庫中可以有很多表;

CREATE KEYSPACE mykeyspace WITH REPLICATION = { 'class' : 'SimpleStrategy','replication_factor' : 2 } ,replication_factor 表示 數據被復制幾份

(2)建表語句:

CREATE TABLE task ( row_split text, column_split text, username text, parent_user text, priority int, url text,  status int, is_dir boolean, channel_code text, is_multilayer boolean, action_type int, create_time bigint, finish_time bigint, id text, subtask_result text, reason text, retry_time int, PRIMARY KEY (row_split, column_split) ) ;  primary key 的第一個元素是rowkey,第二個元素的是cluster key;
 
(3)建索引語句:
create index task_status on task(status);  cassandra的索引不支持范圍查找,類似於 位圖索引 或者 哈希索引,支持 = 操作,不支持< 或 > 之類的范圍查找;
 
(4)查詢語句,
select * from task where status=2;
注意,cassandra where 子句里面的,除了rowkey以外,其他字段如果要使用 = 操作,必須建立二級索引,而且cassandra里面的二級索引 不支持范圍查詢,類似於位圖索引,不同於 BTREE索引;
 
(5)刪除語句,
單獨刪除某個column 或者 某行;
delete column1,column2 from table where rowkey = 'xxxx' 
or 
delete column1,column2 from table where rowkey in ('x','xx','xxx'...)
其中where子句是不能省略的。
 
(6)刪除表中的所有數據
如果要想實現 類似 mysql中 delete from table的效果,可以使用truncate;
 
truncate table


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM