Cassandra数据库与Cql实战笔记

打印 上一主题 下一主题

主题 984|帖子 984|积分 2952

启动数据库

  1. [zdaxctid@node3 bin]$ pwd
  2. /home/database/apache-cassandra-3.11.7/bin
  3. #启动数据库
  4. [zdaxctid@node3 bin]$ ./cassandra
复制代码
乐成标志:


查看数据库节点启动乐成状态

  1. [zdaxctid@node3 bin]$ ./nodetool status
  2. Datacenter: datacenter1
  3. =======================
  4. Status=Up/Down
  5. |/ State=Normal/Leaving/Joining/Moving
  6. --  Address         Load       Tokens       Owns (effective)  Host ID                               Rack
  7. DN  192.168.10.11   ?          256          100.0%            3240fa95-cc32-40f9-8c5d-ff53b9b86adc  rack1
  8. UN  192.168.10.141  405.56 KiB  256          100.0%            468c9c1c-a1d4-4eda-a59a-31793df44d10  rack1
  9. [zdaxctid@node3 bin]$
复制代码

关闭数据库

杀掉数据库进程相干的pid号即可!!

  1. [root@node3 ~]# kill -9 106206
  2. [root@node3 ~]# ps -ef | grep cassandra
  3. root     119880 103558  0 10:39 pts/3    00:00:00 grep --color=auto cassandra
  4. [root@node3 ~]#
复制代码
使用cqlsh工具


  1. [zdaxctid@node3 bin]$ ./cqlsh 192.168.10.141 9042
  2. Connected to Test Cluster at 192.168.10.141:9042.
  3. [cqlsh 5.0.1 | Cassandra 3.11.7 | CQL spec 3.4.4 | Native protocol v3]
  4. Use HELP for help.
  5. cqlsh>
复制代码
常见命令

查看集群信息

  1. cqlsh> describe cluster;Cluster: Test ClusterPartitioner: Murmur3Partitionercqlsh> DESCRIBE tables;Keyspace system_schema----------------------tables     triggers    views    keyspaces  dropped_columnsfunctions  aggregates  indexes  types      columns        Keyspace system_auth--------------------resource_role_permissons_index  role_permissions  role_members  rolesKeyspace system---------------available_ranges          peers               batchlog        transferred_rangesbatches                   compaction_history  size_estimates  hints             prepared_statements       sstable_activity    built_views   "IndexInfo"               peer_events         range_xfers   views_builds_in_progress  paxos               local         Keyspace system_distributed---------------------------repair_history  view_build_status  parent_repair_historyKeyspace system_traces----------------------events  sessionsKeyspace flowmonitoringsystem-----------------------------auth_28_tcp_alarm_value  auth_28_tcp_flow                auth_28_nodeauth_28_alarm_config     auth_28_business_number_index   user        auth_28_server           auth_ip_alarm_config          auth_28_link             auth_28_thrice_hand_shake     auth_28_second           auth_28_second_alarm_history  auth_28_mode_config      auth_28_tcp_flow_alarm_historyauth_28_second_history   ywzd_user                     cqlsh> use system_traces;Invalid syntax at line 1, char 18  use system_traces;                   ^cqlsh> use system_traces;cqlsh:system_traces> DESCRIBE tablesevents  sessionscqlsh:system_traces> describe sessionsCREATE TABLE system_traces.sessions (    session_id uuid PRIMARY KEY,    client inet,    command text,    coordinator inet,    coordinator_port int,    duration int,    parameters map<text, text>,    request text,    started_at timestamp) WITH bloom_filter_fp_chance = 0.01    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}    AND comment = 'tracing sessions'    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.0    AND default_time_to_live = 0    AND gc_grace_seconds = 0    AND max_index_interval = 2048    AND memtable_flush_period_in_ms = 3600000    AND min_index_interval = 128    AND read_repair_chance = 0.0    AND speculative_retry = '99PERCENTILE';cqlsh:system_traces> showImproper show command.cqlsh:system_traces> SHOW Improper SHOW command.cqlsh:system_traces> quit;[zdaxctid@node3 bin]$ ./cqlsh 192.168.10.141 9042
  2. Connected to Test Cluster at 192.168.10.141:9042.
  3. [cqlsh 5.0.1 | Cassandra 3.11.7 | CQL spec 3.4.4 | Native protocol v3]
  4. Use HELP for help.
  5. cqlsh>
  6. showImproper show command.cqlsh> showkey   ...    ... ;SyntaxException: line 1:0 no viable alternative at input 'showkey' ([showkey]...)cqlsh> cqlsh> show hostConnected to Test Cluster at 192.168.10.141:9042.cqlsh> show sessionImproper show command.cqlsh> show version[cqlsh 5.0.1 | Cassandra 3.11.7 | CQL spec 3.4.4 | Native protocol v3]cqlsh>
复制代码
数据定义命令


数据操作命令


操作健空间

创建Keyspace

   语法
  1. cqlsh> create keyspace school with replication={'class':'SimpleStrategy','replication_factor':3};
  2. school         system_auth  system_distributed  flowmonitoringsystem
  3. system_schema  system       system_traces     
  4. cqlsh> describe school;
  5. CREATE KEYSPACE school WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}  AND durable_writes = true;
  6. cqlsh>
复制代码
连接健空间

  1. cqlsh> use school;
  2. cqlsh:school> alter keyspace school with replication={'class':'SimpleStrategy','replication_factor':1};
复制代码
删除健空间

  1. cqlsh:school> drop keyspace school;
  2. cqlsh:school>
  3. cqlsh:school>
  4. cqlsh:school> describe keyspaces;
  5. system_schema  system              system_traces      
  6. system_auth    system_distributed  flowmonitoringsystem
  7. cqlsh:school>
复制代码
创建表

创建语句类似于sql语句!
  1. create table student(
  2.         id int primary key,
  3.     name text,
  4.     age int,
  5.     gender tinyint,
  6.     address text,
  7.     interest set<text>,
  8.     phone list<text>,
  9.     education map<text,text>
  10. );
复制代码
  1. cqlsh:test> create table student(
  2.         ... id int primary key,
  3.         ...     name text,
  4.         ...     age int,
  5.         ...     gender tinyint,
  6.         ...     address text,
  7.         ...     interest set<text>,
  8.         ...     phone list<text>,
  9.         ...     education map<text,text>
  10.         ... );
  11. Warning: schema version mismatch detected; check the schema versions of your nodes in system.local and system.peers.
  12. cqlsh:test> describe test
  13. CREATE KEYSPACE test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}  AND durable_writes = true;
  14. CREATE TABLE test.student (
  15.     id int PRIMARY KEY,
  16.     address text,
  17.     age int,
  18.     education map<text, text>,
  19.     gender tinyint,
  20.     interest set<text>,
  21.     name text,
  22.     phone list<text>
  23. ) WITH bloom_filter_fp_chance = 0.01
  24.     AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
  25.     AND comment = ''
  26.     AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
  27.     AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
  28.     AND crc_check_chance = 1.0
  29.     AND dclocal_read_repair_chance = 0.1
  30.     AND default_time_to_live = 0
  31.     AND gc_grace_seconds = 864000
  32.     AND max_index_interval = 2048
  33.     AND memtable_flush_period_in_ms = 0
  34.     AND min_index_interval = 128
  35.     AND read_repair_chance = 0.0
  36.     AND speculative_retry = '99PERCENTILE';
  37. cqlsh:test> describe tables;
  38. student
  39. cqlsh:test>
复制代码
主键

  1. create table testTab(
  2.         key_part_one int,
  3.         key_part_two int,
  4.         key_clust_one int,
  5.         key_clust_two int,
  6.         key_clust_three uuid,
  7.         name text,
  8.         primary key((key_part_one,key_part_two),key_clust_one,key_clust_two,key_clust_three)
  9. );
复制代码
  1. cqlsh:test> use test01;
  2. cqlsh:test01> create table testTab(
  3.           ... key_part_one int,
  4.           ... key_part_two int,
  5.           ... key_clust_one int,
  6.           ... key_clust_two int,
  7.           ... key_clust_three uuid,
  8.           ... name text,
  9.           ... primary key((key_part_one,key_part_two),key_clust_one,key_clust_two,key_clust_three)
  10.           ... );
  11. Warning: schema version mismatch detected; check the schema versions of your nodes in system.local and system.peers.
  12. cqlsh:test01>
  13. cqlsh:test01>
  14. cqlsh:test01> describe tables;
  15. testtab
  16. cqlsh:test01> select * from testab
  17.           ... ;
  18. InvalidRequest: Error from server: code=2200 [Invalid query] message="unconfigured table testab"
  19. cqlsh:test01> select * from testtab;
  20. key_part_one | key_part_two | key_clust_one | key_clust_two | key_clust_three | name
  21. --------------+--------------+---------------+---------------+-----------------+------
  22. (0 rows)
  23. cqlsh:test01>
复制代码
表修改

添加列

cqlsh:test> alter table testtab add email text;
  1. cqlsh:test> describe keyspaces;
  2. system_schema  system              test           test01              
  3. system_auth    system_distributed  system_traces  flowmonitoringsystem
  4. cqlsh:test> describe tables;
  5. testtab  student
  6. cqlsh:test> alter table testtab add email text;
  7. Warning: schema version mismatch detected; check the schema versions of your nodes in system.local and system.peers.
  8. cqlsh:test>
  9. cqlsh:test> describe testtab;
  10. CREATE TABLE test.testtab (
  11.     key_part_one int,
  12.     key_part_two int,
  13.     key_clust_one int,
  14.     key_clust_two int,
  15.     key_clust_three uuid,
  16.     email text,
  17.     name text,
  18.     PRIMARY KEY ((key_part_one, key_part_two), key_clust_one, key_clust_two, key_clust_three)
  19. ) WITH CLUSTERING ORDER BY (key_clust_one ASC, key_clust_two ASC, key_clust_three ASC)
  20.     AND bloom_filter_fp_chance = 0.01
  21.     AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
  22.     AND comment = ''
  23.     AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
  24.     AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
  25.     AND crc_check_chance = 1.0
  26.     AND dclocal_read_repair_chance = 0.1
  27.     AND default_time_to_live = 0
  28.     AND gc_grace_seconds = 864000
  29.     AND max_index_interval = 2048
  30.     AND memtable_flush_period_in_ms = 0
  31.     AND min_index_interval = 128
  32.     AND read_repair_chance = 0.0
  33.     AND speculative_retry = '99PERCENTILE';
  34. cqlsh:test>
复制代码
删除列

cqlsh:test> alter table testtab drop email;
  1. cqlsh:test> alter table testtab drop email;
  2. Warning: schema version mismatch detected; check the schema versions of your nodes in system.local and system.peers.
  3. cqlsh:test> describe testtab;
  4. CREATE TABLE test.testtab (
  5.     key_part_one int,
  6.     key_part_two int,
  7.     key_clust_one int,
  8.     key_clust_two int,
  9.     key_clust_three uuid,
  10.     name text,
  11.     PRIMARY KEY ((key_part_one, key_part_two), key_clust_one, key_clust_two, key_clust_three)
  12. ) WITH CLUSTERING ORDER BY (key_clust_one ASC, key_clust_two ASC, key_clust_three ASC)
  13.     AND bloom_filter_fp_chance = 0.01
  14.     AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
  15.     AND comment = ''
  16.     AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
  17.     AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
  18.     AND crc_check_chance = 1.0
  19.     AND dclocal_read_repair_chance = 0.1
  20.     AND default_time_to_live = 0
  21.     AND gc_grace_seconds = 864000
  22.     AND max_index_interval = 2048
  23.     AND memtable_flush_period_in_ms = 0
  24.     AND min_index_interval = 128
  25.     AND read_repair_chance = 0.0
  26.     AND speculative_retry = '99PERCENTILE';
  27. cqlsh:test>
复制代码
删除表

cqlsh:test> drop table testtab;
  1. cqlsh:test> drop table testtab;
  2. Warning: schema version mismatch detected; check the schema versions of your nodes in system.local and system.peers.
  3. cqlsh:test>
  4. cqlsh:test> describe tables;
  5. student
  6. cqlsh:test> select * from testtab;
  7. InvalidRequest: Error from server: code=2200 [Invalid query] message="unconfigured table testtab"
  8. cqlsh:test>
复制代码
清空表

  1. cqlsh:test> truncate student;
复制代码
添加数据

  1. INSERT INTO student (id,address,age,gender,name,interest, phone,education) VALUES (1011,'中山路21号',16,1,'Tom',{'游泳', '跑步'},['010-88888888','13888888888'],{'小学' : '城市第一小学', '中学' : '城市第一中学'})
复制代码

  1. INSERT INTO student (id,address,age,gender,name,interest, phone,education) VALUES (1012,'朝阳路19号',17,2,'Jerry',{'看书', '电影'},['020-66666666','13666666666'],{'小学' :'城市第五小学','中学':'城市第六中学'});
复制代码

数据过期时间

using ttl 30
数据30秒以后清空!
  1. INSERT INTO student (id,address,age,gender,name,interest, phone,education) VALUES (1013,'朝阳路19号',17,2,'Linghu',{'看书', '电影'},['020-66666666','13666666666'],{'小学' :'城市第五小学','中学':'城市第六中学'})using ttl 30;
复制代码

查询数据

  1. cqlsh:test> select * from student where id = 1011;
复制代码

更新数据

更新简单数据

更新表中的数据:
  1. cqlsh:test> update student set gender = 0 where id = 1011;
复制代码

更新set范例数据

   在student表中,interest列是set范例
  添加一个元素

   update和+
  1. cqlsh:test> update student set interest = interest+{'打太极'} where id = 1011;
复制代码

删除一个元素

   update和-
  1. cqlsh:test> update student set interest = interest-{'打太极'} where id = 1011;
复制代码

删除全部元素

   update或者delete命令
  1. UPDATE student SET interest = {} WHERE student_id = 1012;
  2. DELETE interest FROM student WHERE student_id = 1012;
复制代码

更新list范例数据

   在student中phone就是list范例
  使用UPDATA命令向list插入值

cqlsh:test> UPDATE student SET phone = [‘020-66666666’, ‘13666666666’,‘1714873054’] WHERE id = 1012;
在list前面插入值

cqlsh:test> UPDATE student SET phone = [ ‘030-55555555’ ] + phone WHERE id = 1012;
在list反面插入值

cqlsh:test> UPDATE student SET phone = phone + [ ‘040-33333333’ ] WHERE id = 1012;
  1. #使用UPDATA命令向list插入值
  2. cqlsh:test> UPDATE student SET phone = ['020-66666666', '13666666666','1714873054'] WHERE id = 1012;
  3. cqlsh:test> select * from student;
  4. id   | address    | age | education                                        | gender | interest         | name  | phone
  5. ------+------------+-----+--------------------------------------------------+--------+------------------+-------+-----------------------------------------------
  6. 1011 | 中山路21号 |  16 | {'中学': '城市第一中学', '小学': '城市第一小学'} |      0 | {'游泳', '跑步'} |   Tom |               ['010-88888888', '13888888888']
  7. 1012 | 朝阳路19号 |  17 | {'中学': '城市第六中学', '小学': '城市第五小学'} |   null | {'电影', '看书'} | Jerry | ['020-66666666', '13666666666', '1714873054']
  8. (2 rows)
  9. #在list前面插入值
  10. cqlsh:test> UPDATE student SET phone = [ '030-55555555' ] + phone WHERE id = 1012;
  11. cqlsh:test> select * from student;
  12. id   | address    | age | education                                        | gender | interest         | name  | phone
  13. ------+------------+-----+--------------------------------------------------+--------+------------------+-------+---------------------------------------------------------------
  14. 1011 | 中山路21号 |  16 | {'中学': '城市第一中学', '小学': '城市第一小学'} |      0 | {'游泳', '跑步'} |   Tom |                               ['010-88888888', '13888888888']
  15. 1012 | 朝阳路19号 |  17 | {'中学': '城市第六中学', '小学': '城市第五小学'} |   null | {'电影', '看书'} | Jerry | ['030-55555555', '020-66666666', '13666666666', '1714873054']
  16. (2 rows)
  17. #在list后面插入值
  18. cqlsh:test> UPDATE student SET phone = phone + [ '040-33333333' ]  WHERE id = 1012;
  19. cqlsh:test> select * from student;
  20. id   | address    | age | education                                        | gender | interest         | name  | phone
  21. ------+------------+-----+--------------------------------------------------+--------+------------------+-------+-------------------------------------------------------------------------------
  22. 1011 | 中山路21号 |  16 | {'中学': '城市第一中学', '小学': '城市第一小学'} |      0 | {'游泳', '跑步'} |   Tom |                                               ['010-88888888', '13888888888']
  23. 1012 | 朝阳路19号 |  17 | {'中学': '城市第六中学', '小学': '城市第五小学'} |   null | {'电影', '看书'} | Jerry | ['030-55555555', '020-66666666', '13666666666', '1714873054', '040-33333333']
  24. (2 rows)
  25. cqlsh:test>
复制代码
更新map范例数据

map输出顺序取决于map范例。
使用insert和update命令

  1. cqlsh:test> update student set education = {'中学':'桐梓一中','小学':'南天门'} where id = 1012;
  2. cqlsh:test> select * from student;
  3. id   | address    | age | education                                        | gender | interest         | name  | phone
  4. ------+------------+-----+--------------------------------------------------+--------+------------------+-------+-------------------------------------------------------------------------------
  5. 1011 | 中山路21号 |  16 | {'中学': '城市第一中学', '小学': '城市第一小学'} |      0 | {'游泳', '跑步'} |   Tom |                                               ['010-88888888', '13888888888']
  6. 1012 | 朝阳路19号 |  17 |           {'中学': '桐梓一中', '小学': '南天门'} |   null | {'电影', '看书'} | Jerry | ['030-55555555', '020-66666666', '13666666666', '1714873054', '040-33333333']
  7. (2 rows)
  8. cqlsh:test>
复制代码
删除元素

可以用DELETE 和 UPDATE 删除Map范例中的数据

使用DELETE删除数据可以用DELETE 和 UPDATE 删除Map范例中的数据
使用DELETE删除数据
  1. cqlsh:test> delete education['中学'] from student where id = 1012;
  2. cqlsh:test> select * from student;
  3. id   | address    | age | education                                        | gender | interest         | name  | phone
  4. ------+------------+-----+--------------------------------------------------+--------+------------------+-------+-------------------------------------------------------------------------------
  5. 1011 | 中山路21号 |  16 | {'中学': '城市第一中学', '小学': '城市第一小学'} |      0 | {'游泳', '跑步'} |   Tom |                                               ['010-88888888', '13888888888']
  6. 1012 | 朝阳路19号 |  17 |                               {'小学': '南天门'} |   null | {'电影', '看书'} | Jerry | ['030-55555555', '020-66666666', '13666666666', '1714873054', '040-33333333']
  7. (2 rows)
  8. cqlsh:test>
复制代码
删除行

删除student中student_id=1012 的数据
  1. cqlsh:test> delete from student where id = 1012;
  2. cqlsh:test> select * from student;
  3. id   | address    | age | education                                        | gender | interest         | name | phone
  4. ------+------------+-----+--------------------------------------------------+--------+------------------+------+---------------------------------
  5. 1011 | 中山路21号 |  16 | {'中学': '城市第一中学', '小学': '城市第一小学'} |      0 | {'游泳', '跑步'} |  Tom | ['010-88888888', '13888888888']
  6. (1 rows)
  7. cqlsh:test>
复制代码
create table authtype(
​ id int primary key,
​ auth_type text,
​ foreing key (auth_type) references hostinfo (id)
);
delete from student where id = 1012;
cqlsh:test> select * from student;
id | address | age | education | gender | interest | name | phone
------±-----------±----±-------------------------------------------------±-------±-----------------±-----±--------------------------------
1011 | 中山路21号 | 16 | {‘中学’: ‘城市第一中学’, ‘小学’: ‘城市第一小学’} | 0 | {‘游泳’, ‘跑步’} | Tom | [‘010-88888888’, ‘13888888888’]
(1 rows)
cqlsh:test>

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

缠丝猫

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表