Apache Cassandra 快速入门指南
下载、安装并启动 Cassandra
$ wget http://mirror.bit.edu.cn/apache/cassandra/3.11.4/apache-cassandra-3.11.4-bin.tar.gz$ tar -zxf apache-cassandra-3.11.4-bin.tar.gz$ cd apache-cassandra-3.11.4
[iteblog@www.iteblog.com apache-cassandra-3.11.4]# lltotal 528drwxr-xr-x 2 iteblog iteblog 4096 Apr 2 21:12 bin-rw-r--r-- 1 iteblog iteblog 4832 Feb 3 06:09 CASSANDRA-14092.txt-rw-r--r-- 1 iteblog iteblog 366951 Feb 3 06:09 CHANGES.txtdrwxr-xr-x 3 iteblog iteblog 4096 Apr 2 21:12 confdrwxr-xr-x 4 iteblog iteblog 4096 Apr 2 21:12 docdrwxr-xr-x 2 iteblog iteblog 4096 Apr 2 21:12 interfacedrwxr-xr-x 3 iteblog iteblog 4096 Apr 2 21:12 javadocdrwxr-xr-x 4 iteblog iteblog 4096 Apr 2 21:12 lib-rw-r--r-- 1 iteblog iteblog 11609 Feb 3 06:09 LICENSE.txt-rw-r--r-- 1 iteblog iteblog 112586 Feb 3 06:09 NEWS.txt-rw-r--r-- 1 iteblog iteblog 2811 Feb 3 06:09 NOTICE.txtdrwxr-xr-x 3 iteblog iteblog 4096 Apr 2 21:12 pylibdrwxr-xr-x 4 iteblog iteblog 4096 Apr 2 21:12 tools
启动 Cassandra
[iteblog@www.iteblog.com apache-cassandra-3.11.4]# bin/cassandra
[iteblog@www.iteblog.com apache-cassandra-3.11.4]# bin/nodetool statusDatacenter: datacenter1=======================Status=Up/Down|/ State=Normal/Leaving/Joining/Moving-- Address Load Tokens Owns (effective) Host ID RackUN 127.0.0.1 160.88 KiB 256 100.0% 49f4470f-396b-4d50-bcd6-3da2d9370167 rack1
使用 CQL Shell
[iteblog@www.iteblog.com apache-cassandra-3.11.4]# bin/cqlshConnected to Test Cluster at 127.0.0.1:9042.[cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]Use HELP for help.cqlsh>
[iteblog@www.iteblog.com apache-cassandra-3.11.4]# bin/cqlsh localhost 9042
基本的 cqlsh 命令
cqlsh> HELP Documented shell commands:===========================CAPTURE CLS COPY DESCRIBE EXPAND LOGIN SERIAL SOURCE UNICODECLEAR CONSISTENCY DESC EXIT HELP PAGING SHOW TRACING CQL help topics:================AGGREGATES CREATE_KEYSPACE DROP_TRIGGER TEXTALTER_KEYSPACE CREATE_MATERIALIZED_VIEW DROP_TYPE TIMEALTER_MATERIALIZED_VIEW CREATE_ROLE DROP_USER TIMESTAMPALTER_TABLE CREATE_TABLE FUNCTIONS TRUNCATEALTER_TYPE CREATE_TRIGGER GRANT *ALTER_USER CREATE_TYPE INSERT UPDATEAPPLY CREATE_USER INSERT_JSON USEASCII DATE INT UUIDBATCH DELETE JSONBEGIN DROP_AGGREGATE KEYWORDSBLOB DROP_COLUMNFAMILY LIST_PERMISSIONSBOOLEAN DROP_FUNCTION LIST_ROLESCOUNTER DROP_INDEX LIST_USERSCREATE_AGGREGATE DROP_KEYSPACE PERMISSIONSCREATE_COLUMNFAMILY DROP_MATERIALIZED_VIEW REVOKECREATE_FUNCTION DROP_ROLE SELECTCREATE_INDEX DROP_TABLE SELECT_JSON
cqlsh> CONSISTENCYCurrent consistency level is ONE.cqlsh> EXPANDExpanded output is currently disabled. Use EXPAND ON to enable.cqlsh> PAGINGQuery paging is currently enabled. Use PAGING OFF to disablePage size: 100
在 cqlsh 里面查看环境变量
cqlsh> DESCRIBE CLUSTER; Cluster: Test ClusterPartitioner: Murmur3Partitioner
cqlsh> DESCRIBE KEYSPACES; system_traces system_schema system_auth system system_distributed
cqlsh> SHOW VERSION;[cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]
通过 cqlsh 创建 keyspace
cqlsh> CREATE KEYSPACE iteblog_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};cqlsh>
cqlsh> DESCRIBE KEYSPACE iteblog_keyspace; CREATE KEYSPACE iteblog_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
cqlsh> USE iteblog_keyspace;cqlsh:iteblog_keyspace>
通过 cqlsh 创建表
cqlsh> use iteblog_keyspace;cqlsh:iteblog_keyspace> CREATE TABLE iteblog_user (first_name text , last_name text, PRIMARY KEY (first_name)) ;
cqlsh> CREATE TABLE iteblog_keyspace.iteblog_user(first_name text , last_name text, PRIMARY KEY (first_name)) ;
cqlsh:iteblog_keyspace> DESCRIBE TABLE iteblog_user; CREATE TABLE iteblog_keyspace.iteblog_user ( first_name text PRIMARY KEY, last_name 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 = AND gc_grace_seconds = 864000 AND max_index_interval = 2048 AND memtable_flush_period_in_ms = AND min_index_interval = 128 AND read_repair_chance = 0.0 AND speculative_retry = '99PERCENTILE'; cqlsh:iteblog_keyspace>
通过 cqlsh 往表里面读写数据
cqlsh:iteblog_keyspace> INSERT INTO iteblog_user (first_name, last_name) VALUES ('iteblog', 'Hadoop');cqlsh:iteblog_keyspace> INSERT INTO iteblog_user (first_name, last_name) VALUES ('Zhang', 'San');cqlsh:iteblog_keyspace> INSERT INTO iteblog_user (first_name) VALUES ('Wu');
cqlsh:iteblog_keyspace> SELECT COUNT(*) FROM iteblog_user; count------- 3 (1 rows) Warnings :Aggregation query used without partition key
cqlsh:iteblog_keyspace> SELECT * FROM iteblog_user; first_name | last_name------------+----------- iteblog | Hadoop Wu | null Zhang | San (3 rows)cqlsh:iteblog_keyspace> SELECT * FROM iteblog_user WHERE first_name='iteblog'; first_name | last_name------------+----------- iteblog | Hadoop (1 rows)cqlsh:iteblog_keyspace>
cqlsh:iteblog_keyspace> DELETE last_name FROM iteblog_user WHERE first_name='iteblog';cqlsh:iteblog_keyspace> SELECT * FROM iteblog_user WHERE first_name='iteblog'; first_name | last_name------------+----------- iteblog | null (1 rows)cqlsh:iteblog_keyspace>
cqlsh:iteblog_keyspace> DELETE FROM iteblog_user WHERE first_name='iteblog';cqlsh:iteblog_keyspace> SELECT * FROM iteblog_user WHERE first_name='iteblog'; first_name | last_name------------+----------- ( rows)cqlsh:iteblog_keyspace>
insert/update 相当于 upsert
cqlsh:iteblog_keyspace> INSERT INTO iteblog_user (first_name, last_name) VALUES ('Wu', 'Shi');cqlsh:iteblog_keyspace> SELECT * FROM iteblog_user; first_name | last_name------------+----------- Wu | Shi Zhang | San (2 rows)
cqlsh:iteblog_keyspace> SELECT * FROM iteblog_user; first_name | last_name------------+----------- Wu | Shi Zhang | San (2 rows)cqlsh:iteblog_keyspace> UPDATE iteblog_user SET last_name = 'Si' WHERE first_name = 'Li';cqlsh:iteblog_keyspace> SELECT * FROM iteblog_user; first_name | last_name------------+----------- Wu | Shi Zhang | San Li | Si (3 rows)cqlsh:iteblog_keyspace>
清空或删除表
cqlsh:iteblog_keyspace> TRUNCATE iteblog_user;cqlsh:iteblog_keyspace> DROP TABLE iteblog_user;
相关文章