Cassandra两种登录方式cassandra-cli / csqlsh
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cassandra两种登录方式cassandra-cli / csqlsh相关的知识,希望对你有一定的参考价值。
(1)cassandra-cli
cassadnra-cli命令在cassandra2.2中被抛弃,以后登录访问cassandra可以使用cqlsh
[[email protected] 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
[[email protected]] create keyspace twissandra;
with placement_strategy = ‘org.apache.cassandra.locator.SimpleStrategy‘
and strategy_options = {replication_factor:1};
5200ec35-6f27-3f8f-a969-5f91b4308d40
[[email protected]] use twissandra;
Authenticated to keyspace: twissandra
[[email protected]] help create column family;
[[email protected]] create column family users with comparator = UTF8Type;
8eadec66-a110-32b4-b873-5bfaaac2ddf5
[[email protected]] 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
[[email protected]] assume users keys as utf8;
Assumption for column family ‘users‘ added successfully.
#插入数据
[[email protected]] set users[‘jsmith‘][‘first‘]=‘John‘;
[[email protected]] set users[‘jsmith‘][‘last‘]=‘Smith‘;
[[email protected]] set users[‘jsmith‘][‘age‘]=‘38‘;
#插入数据
[[email protected]] get users[‘jsmith‘];
=> (name=age, value=38, timestamp=1502764716526000)
=> (name=first, value=John, timestamp=1502764685559000)
=> (name=last, value=Smith, timestamp=1502764696653000)
#修改数据值
[[email protected]] set users[‘jsmith‘][‘first‘]=‘Jack‘;
[[email protected]] get users[‘jsmith‘];
=> (name=age, value=38, timestamp=1502764716526000)
=> (name=first, value=Jack, timestamp=1502764821855000)
=> (name=last, value=Smith, timestamp=1502764696653000)
[[email protected]] 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
[[email protected] ~]$ cqlsh -ucassandra -pcassandra localhost
#远程访问:
[[email protected] ~]$ cqlsh 172.16.101.59
#查看版本
[email protected]:system> show version;
[cqlsh 5.0.1 | Cassandra 3.11.1 | CQL spec 3.4.4 | Native protocol v4]
[email protected]:system> show host;
Connected to Test Cluster at 127.0.0.1:9042.
#system.local表
[email protected]: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的主要内容,如果未能解决你的问题,请参考以下文章