Cassandra兩種登錄方式cassandra-cli / csqlsh
(1)cassandra-cli
cassadnra-cli命令在cassandra2.2中被拋棄,以後登錄訪問cassandra可以使用cqlsh
[tnuser@sht-sgmhadoopdn-02 cassandra]$ cassandra-cli -h 172.16.101.59 -p 9160
Connected to: "mycluster" on 172.16.101.59/9160
Welcome to Cassandra CLI version 2.1.18
[default@unknown] create keyspace twissandra;
with placement_strategy = ‘org.apache.cassandra.locator.SimpleStrategy‘
and strategy_options = {replication_factor:1};
5200ec35-6f27-3f8f-a969-5f91b4308d40
[default@unknown] use twissandra;
Authenticated to keyspace: twissandra
[default@twissandra] help create column family;
[default@twissandra] create column family users with comparator = UTF8Type;
8eadec66-a110-32b4-b873-5bfaaac2ddf5
[default@twissandra] update column family users with
column_metadata =
[
{column_name: first, validation_class: UTF8Type},
{column_name: last, validation_class: UTF8Type},
{column_name: age, validation_class: UTF8Type, index_type: KEYS}
];
ccdbe046-183a-307f-8e38-c1f44400f9fa
[default@twissandra] assume users keys as utf8;
Assumption for column family ‘users‘ added successfully.
#插入數據
[default@twissandra] set users[‘jsmith‘][‘first‘]=‘John‘;
[default@twissandra] set users[‘jsmith‘][‘last‘]=‘Smith‘;
[default@twissandra] set users[‘jsmith‘][‘age‘]=‘38‘;
#插入數據
[default@twissandra] get users[‘jsmith‘];
=> (name=age, value=38, timestamp=1502764716526000)
=> (name=first, value=John, timestamp=1502764685559000)
=> (name=last, value=Smith, timestamp=1502764696653000)
#修改數據值
[default@twissandra] set users[‘jsmith‘][‘first‘]=‘Jack‘;
[default@twissandra] get users[‘jsmith‘];
=> (name=age, value=38, timestamp=1502764716526000)
=> (name=first, value=Jack, timestamp=1502764821855000)
=> (name=last, value=Smith, timestamp=1502764696653000)
[default@twissandra] get users where age=‘38‘;
-------------------
RowKey: jsmith
=> (name=age, value=38, timestamp=1502764716526000)
=> (name=first, value=Jack, timestamp=1502764821855000)
=> (name=last, value=Smith, timestamp=1502764696653000)
(2)cqlsh
[tnuser@sht-sgmhadoopdn-02 ~]$ cqlsh -ucassandra -pcassandra localhost
#遠程訪問:
[tnuser@sht-sgmhadoopnn-02 ~]$ cqlsh 172.16.101.59
#查看版本
cassandra@cqlsh:system> show version;
[cqlsh 5.0.1 | Cassandra 3.11.1 | CQL spec 3.4.4 | Native protocol v4]
cassandra@cqlsh:system> show host;
Connected to Test Cluster at 127.0.0.1:9042.
#system.local表
cassandra@cqlsh:system> select key,cluster_name,listen_address from system.local;
key | cluster_name | listen_address
-------+--------------+-----------------
local | Test Cluster | 192.168.163.102
#創建一個keyspace:
cqlsh> create keyspace mykeyspace with replication = {‘class‘:‘SimpleStrategy‘,‘replication_factor‘:1} AND durable_writes = true;
#查看總共有哪些keyspace:
cqlsh> desc keyspaces
system_traces twissandra system mykeyspace
#查看指定keyspace的詳細信息:
cqlsh> desc mykeyspace
CREATE KEYSPACE mykeyspace WITH replication = {‘class‘: ‘SimpleStrategy‘, ‘replication_factor‘: ‘1‘} AND durable_writes = true;
#進入某個keyspace:
cqlsh> use mykeyspace;
#創建一個users表:
cqlsh:mykeyspace> create table users(user_id int primary key,fname text,lname text);
cqlsh:mykeyspace> desc tables;
users
#查看users表信息:
cqlsh:mykeyspace> desc users;
CREATE TABLE mykeyspace.users (
user_id int PRIMARY KEY,
fname text,
lname 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‘}
AND compression = {‘sstable_compression‘: ‘org.apache.cassandra.io.compress.LZ4Compressor‘}
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 = ‘99.0PERCENTILE‘;
#插入數據
cqlsh:mykeyspace> insert into users (user_id,fname,lname) values(1744,‘john‘,‘doe‘);
cqlsh:mykeyspace> insert into users (user_id,fname,lname) values(1745,‘john‘,‘smith‘);
cqlsh:mykeyspace> insert into users (user_id,fname,lname) values(1746,‘john‘,‘jack‘);
cqlsh:mykeyspace> select * from users;
user_id | fname | lname
---------+-------+-------
1745 | john | smith
1744 | john | doe
1746 | john | jack
#創建索引:
cqlsh:mykeyspace> create index users_lname_idx on users(lname);
cqlsh:mykeyspace> select * from users where lname=‘smith‘;
user_id | fname | lname
---------+-------+-------
1745 | john | smith
註意:
如果是通過cassandra-cli登錄創建的數據然後通過再cli查看,可能會不兼容:
cqlsh:mykeyspace> desc twissandra;
CREATE KEYSPACE twissandra WITH replication = {‘class‘: ‘NetworkTopologyStrategy‘, ‘datacenter1‘: ‘1‘} AND durable_writes = true;
/*
Warning: Table twissandra.users omitted because it has constructs not compatible with CQL (was created via legacy API). # 這裏發出警告,不兼容
Approximate structure, for reference:
(this should not be used to reproduce this schema)
CREATE TABLE twissandra.users (
key blob,
column1 text,
value blob,
age text,
first text,
last text,
PRIMARY KEY (key, column1)
) WITH COMPACT STORAGE
AND CLUSTERING ORDER BY (column1 ASC)
AND caching = ‘{"keys":"ALL", "rows_per_partition":"NONE"}‘
AND comment = ‘‘
AND compaction = {‘class‘: ‘org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy‘}
AND compression = {‘sstable_compression‘: ‘org.apache.cassandra.io.compress.LZ4Compressor‘}
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 = ‘NONE‘;
CREATE INDEX users_age_idx ON twissandra.users (age);
*/
cqlsh:mykeyspace> select * from twissandra.users;
key | column1 | value | age | first | last
----------------+---------+--------------+-------+-------+-------
0x6a736d697468 | age | 0x3338 | 38 | 38 | 38
0x6a736d697468 | first | 0x4a61636b | Jack | Jack | Jack
0x6a736d697468 | last | 0x536d697468 | Smith | Smith | Smith
本文出自 “Darren Memos” 博客,請務必保留此出處http://darrenmemos.blog.51cto.com/10979687/1980944
Cassandra兩種登錄方式cassandra-cli / csqlsh