Cassandra basic operations using cqlsh

本文是公司使用一段时间Cassandra后的总结,主要是一些基础操作,比较难理解的是Cassandra的where操作。我并没有详细列出,而是给了Datastax的一篇文章链接。

Basic

  • Cassandra 2.2.5
    docker run -d --net host --name cca cassandra:2.2.5
    
  • pip install cqlsh

login

cqlsh cca_ip
cqlsh -u cassandra -p cassandra cca_ip

keyspace

  • show keyspaces
cassandra@cqlsh:raw> describe keyspaces;
raw  system_distributed  system_auth  system  system_traces
  • use keyspace
use raw;
  • create keyspace
CREATE KEYSPACE IF NOT EXISTS raw WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': 1 }

指定了负责备份数据的类SimpleStrategy,和备份因子replication_factor(可以根据集群中node的数量调整)。

  • drop keyspace
drop keyspace keyspace_name;
drop keyspace raw;
  • alter(change) keyspace
ALTER KEYSPACE “KeySpace Name”
WITH replication = {'class': ‘Strategy name’, 'replication_factor' : ‘No.Of  replicas’};

reference: alter keyspace

table

  • show tables
cassandra@cqlsh:raw> describe tables;   
process_info_v1  cpu_usage_v1  container_info_v1  server_info_v1  mem_usage_v1
  • create table
CREATE TABLE tablename(
   column1 name datatype PRIMARYKEY,
   column2 name data type,
   column3 name data type.
   )

reference: create table

  • alter table
  • add column
ALTER TABLE table name
ADD  new column datatype;
drop table table_name;

CQL (like SQL)

select

select * from table

insert

insert into table (column1,...,column4) values (v1,...,v4)

update

UPDATE <tablename>
SET <column name> = <new value>
<column name> = <value>....
WHERE <condition>

reference

detele

DELETE FROM <identifier> WHERE <condition>;
cqlsh:tutorialspoint> DELETE FROM emp WHERE emp_id=3;

reference

where clause

Cassandra是分布式列式NoSQL数据库,和SQL数据库在许多方面都有区别。CQL中的主键Primary key和SQL中的Primary key有本质区别,这导致在where子句上会有区别。

A deep look at the CQL WHERE clause

comments powered by Disqus