初识Cassandra数据库

394 阅读2分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

一、Cassandra介绍

Cassandra是一个混合型的非关系的数据库,类似于Google的BigTable。其主要功能比Dynamo (分布式的Key-Value存储系统)更丰富,但支持度却不如文档存储MongoDB(介于关系数据库和非关系数据库之间的开源产品,是非关系数据库当中功能最丰富,最像关系数据库的。支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型)。

二、下载Cassandra

到官网下载www.apache.org/dyn/closer.…

[root@localhost hbk] wget mirrors.hust.edu.cn/apache/cass…

解压

[root@localhost hbk]# tar -zxvf apache-cassandra-3.11.3-bin.tar.gz

[root@localhost hbk]# cd apache-cassandra-3.11.3

[root@localhost apache-cassandra-3.11.3]# ls

bin CASSANDRA-14092.txt CHANGES.txt conf doc interface javadoc lib LICENSE.txt NEWS.txt NOTICE.txt pylib tools

三、启动服务端

[root@localhost apache-cassandra-3.11.3]# cd bin/

[root@localhost bin]# ./cassandra -R

四、启用CQL

[root@localhost bin]# ./cqlsh

Connected to Test Cluster at 127.0.0.1:9042.

[cqlsh 5.0.1 | Cassandra 3.11.3 | CQL spec 3.4.4 | Native protocol v4]

Use HELP for help.

cqlsh> 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 CREATEKEYSPACE DROPTRIGGER TEXT

ALTERKEYSPACE CREATEMATERIALIZEDVIEW DROPTYPE TIME

ALTERMATERIALIZEDVIEW CREATEROLE DROPUSER TIMESTAMP

ALTERTABLE CREATETABLE FUNCTIONS TRUNCATE

ALTERTYPE CREATETRIGGER GRANT TYPES

ALTERUSER CREATETYPE INSERT UPDATE

APPLY CREATEUSER INSERTJSON 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 DROPMATERIALIZEDVIEW REVOKE

CREATEFUNCTION DROPROLE SELECT

CREATEINDEX DROPTABLE SELECT_JSON

五、相关操作

1、建立keyspace

cqlsh:hbk_space> CREATE KEYSPACE "hbk_space" WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3} AND durable_writes = 'true';

cqlsh:hbk_space> DESCRIBE KEYSPACES

system_schema system_auth system system_distributed system_traces hbk_space

cqlsh:hbk_space> USE hbk_space ;

cqlsh:hbk_space> CREATE TABLE hbk_table(id int PRIMARY KEY ,name text ,age int,phone varint );

cqlsh:hbk_space> select * from hbk_table ;

id | age | name | phone

----+-----+------+-------

(0 rows)

cqlsh:hbk_space> ALTER TABLE hbk_table ADD emp_address varchar ;

cqlsh:hbk_space> DESCRIBE TABLE hbk_table ;

CREATE TABLE hbk_space.hbk_table (

id int PRIMARY KEY,

age int,

emp_address text,

name text,

phone varint

) 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';

cqlsh:hbk_space> insert into hbk_table (id,age, emp_address , name , phone ) values(1,20,'nankang','hbk',18679758769);

cqlsh:hbk_space> select * from hbk_table ;

id | age | emp_address | name | phone

----+-----+-------------+------+-------------

1 | 20 | nankang | hbk | 18679758769

(1 rows)

cqlsh:hbk_space> UPDATE hbk_table set emp_address = 'ganzhou' where id=1;

cqlsh:hbk_space> select * from hbk_table ;

id | age | emp_address | name | phone

----+-----+-------------+------+-------------

1 | 20 | ganzhou | hbk | 18679758769

(1 rows)

cqlsh:hbk_space> DELETE from hbk_table where id=1;

cqlsh:hbk_space> select * from hbk_table ;

id | age | emp_address | name | phone

----+-----+-------------+------+-------