天天看點

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

=> (name=first, value=Jack, timestamp=1502764821855000)

[default@twissandra] get users where age='38';

-------------------

RowKey: jsmith

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

注意:

如果是通過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 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