Cassandra3基本操作01

1、首先看一下各节点状态

neohope@debian8-node01:~/Deploy/apache-cassandra-3.0.1$ bin/nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address        Load       Tokens       Owns    Host ID                               Rack
UN  172.16.172.23  230.25 KB  256          ?       760879a3-e06b-43bb-97bf-b045a655da9c  rack1
UN  172.16.172.24  237.17 KB  256          ?       5bf3e77d-ccf4-4f81-9465-de0babe1e0cc  rack1
UN  172.16.172.25  241.55 KB  256          ?       5a7ed53f-c8e0-4658-af9a-41880dada5ef  rack1

Note: Non-system keyspaces don't have the same replication settings, effective ownership information is meaningless

2、然后用cqlsh连接cassandra

neohope@debian8-node01:~/Deploy/apache-cassandra-3.0.1$ bin/cqlsh debian8-node01
Connected to NeoCluster at debian8-node01:9042.
[cqlsh 5.0.1 | Cassandra 3.0.1 | CQL spec 3.3.1 | Native protocol v4]
Use HELP for help.

cqlsh:neokeyspace> help

Documented shell commands:
===========================
CAPTURE  CLS          COPY  DESCRIBE  EXPAND  LOGIN   SERIAL  SOURCE   UNICODE
CLEAR    CONSISTENCY  DESC  EXIT      HELP    PAGING  SHOW    TRACING

CQL help topics:
================
AGGREGATES               CREATE_KEYSPACE           DROP_TRIGGER      TEXT     
ALTER_KEYSPACE           CREATE_MATERIALIZED_VIEW  DROP_TYPE         TIME     
ALTER_MATERIALIZED_VIEW  CREATE_ROLE               DROP_USER         TIMESTAMP
ALTER_TABLE              CREATE_TABLE              FUNCTIONS         TRUNCATE 
ALTER_TYPE               CREATE_TRIGGER            GRANT             TYPES    
ALTER_USER               CREATE_TYPE               INSERT            UPDATE   
APPLY                    CREATE_USER               INSERT_JSON       USE      
ASCII                    DATE                      INT               UUID     
BATCH                    DELETE                    JSON            
BEGIN                    DROP_AGGREGATE            KEYWORDS        
BLOB                     DROP_COLUMNFAMILY         LIST_PERMISSIONS
BOOLEAN                  DROP_FUNCTION             LIST_ROLES      
COUNTER                  DROP_INDEX                LIST_USERS      
CREATE_AGGREGATE         DROP_KEYSPACE             PERMISSIONS     
CREATE_COLUMNFAMILY      DROP_MATERIALIZED_VIEW    REVOKE          
CREATE_FUNCTION          DROP_ROLE                 SELECT          
CREATE_INDEX             DROP_TABLE                SELECT_JSON     

3、查看并新建keyspace

cqlsh> create keyspace neokeyspace
   ... with replication={'class':'SimpleStrategy','replication_factor':2};
cqlsh> describe keyspaces

neokeyspace    system_auth  system_distributed
system_schema  system       system_traces     

cqlsh> use neokeyspace;

4、新建表

cqlsh:neokeyspace> create table patient(pid text primary key,pname text,psex text);
cqlsh:neokeyspace> describe tables;

patient

cqlsh:neokeyspace> describe patient;

CREATE TABLE neokeyspace.patient (
    pid text PRIMARY KEY,
    pname text,
    psex text
) WITH bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';

5、插入并查询数据

cqlsh:neokeyspace> insert into patient(pid,pname,psex)
               ... values('p001','zhangsan','male');

cqlsh:neokeyspace> insert into patient(pid,pname,psex)
               ... values('p002','lisi','male');

cqlsh:neokeyspace> insert into patient(pid,pname,psex)
               ... values('p002','wangwu','male');

cqlsh:neokeyspace> select * from patient;

 pid  | pname    | psex
------+----------+------
 p001 | zhangsan | male
 p002 |   wangwu | male

(2 rows)

发现了什么没?当插入两条key相同的数据后,第一条直接被第二条覆盖掉了。

6、更新并查询数据

cqlsh:neokeyspace> insert into patient(pid,pname) values('p002','lisi');
cqlsh:neokeyspace> insert into patient(pid,pname,psex)
               ... values('p003','wangwu','maile');
cqlsh:neokeyspace> select * from patient;

 pid  | pname    | psex
------+----------+-------
 p001 | zhangsan |  male
 p003 |   wangwu | maile
 p002 |     lisi |  male

(3 rows)


cqlsh:neokeyspace> select pid from patient;

 pid
------
 p001
 p003
 p002

(3 rows)

7、删除表

cqlsh:neokeyspace> drop table patient;

8、删除表空间

cqlsh:neokeyspace> drop namespace neokeyspace;

恩,到这里,基本的Cassandra操作就完成了。

Leave a Reply

Your email address will not be published. Required fields are marked *

*