ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Cassandra系列-2-基本功能测试

Cassandra系列-2-基本功能测试

原创 Linux操作系统 作者:redhouser 时间:2012-05-31 14:27:54 0 删除 编辑

4,使用cassandra-cli
4.1 基本命令
[nosql@rhel5 ~]$ cd $CASS_HOME
[nosql@rhel5 apache-cassandra-1.0.10]$ cd bin
[nosql@rhel5 bin]$ cassandra-cli -h 127.0.0.1 -p 9160
-bash: cassandra-cli: command not found
[nosql@rhel5 bin]$ ./cassandra-cli -h 127.0.0.1 -p 9160
Connected to: "MyCluster" on 127.0.0.1/9160
Welcome to Cassandra CLI version 1.0.10

Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.

[default@unknown] help;
Getting around:
?                       Display this help.
help;                   Display this help.
help ;         Display command-specific help.
exit;                   Exit this utility.
quit;                   Exit this utility.

Commands:
assume                  Apply client side validation.
connect                 Connect to a Cassandra node.
consistencylevel        Sets consisteny level for the client to use.
count                   Count columns or super columns.
create column family    Add a column family to an existing keyspace.
create keyspace         Add a keyspace to the cluster.
del                     Delete a column, super column or row.
decr                    Decrements a counter column.
describe cluster        Describe the cluster configuration.
describe                Describe a keyspace and it's column families or column family in current keyspace.
drop column family      Remove a column family and it's data.
drop keyspace           Remove a keyspace and it's data.
drop index              Remove an existing index from specific column.
get                     Get rows and columns.
incr                    Increments a counter column.
list                    List rows in a column family.
set                     Set columns.
show api version        Show the server API version.
show cluster name       Show the cluster name.
show keyspaces          Show all keyspaces and their column families.
show schema             Show a cli script. to create keyspaces and column families.
truncate                Drop the data in a column family.
update column family    Update the settings for a column family.
update keyspace         Update the settings for a keyspace.
use                     Switch to a keyspace.

[default@unknown] show schema
...     ;
create keyspace system
  with placement_strategy = 'LocalStrategy'
  and strategy_options = {replication_factor : 1}
  and durable_writes = true;

use system;

create column family HintsColumnFamily
  with column_type = 'Super'
  and comparator = 'BytesType'
  and subcomparator = 'BytesType'
  and default_validation_class = 'BytesType'
  and key_validation_class = 'BytesType'
  and rows_cached = 0.0
  and row_cache_save_period = 0
  and row_cache_keys_to_save = 2147483647
  and keys_cached = 0.01
  and key_cache_save_period = 0
  and read_repair_chance = 0.0
  and gc_grace = 0
  and min_compaction_threshold = 4
  and max_compaction_threshold = 32
  and replicate_on_write = true
  and row_cache_provider = 'ConcurrentLinkedHashCacheProvider'
  and compaction_strategy = 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'
  and comment = 'hinted handoff data';

  ...

[default@unknown] describe system;
Keyspace: system:
  Replication Strategy: org.apache.cassandra.locator.LocalStrategy
  Durable Writes: true
    Options: [replication_factor:1]
  Column Families:
    ColumnFamily: HintsColumnFamily (Super)
    "hinted handoff data"
      Key Validation Class: org.apache.cassandra.db.marshal.BytesType
      Default column value validator: org.apache.cassandra.db.marshal.BytesType
      Columns sorted by: org.apache.cassandra.db.marshal.BytesType/org.apache.cassandra.db.marshal.BytesType
      Row cache size / save period in seconds / keys to save : 0.0/0/all
      Row Cache Provider: org.apache.cassandra.cache.ConcurrentLinkedHashCacheProvider
      Key cache size / save period in seconds: 0.01/0
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0
      Replicate on write: true
      Bloom Filter FP chance: default
      Built indexes: []
      Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy
   ....
[default@unknown] show cluster name;
MyCluster

[default@unknown] help consistencylevel
...     ;
consistencylevel as

Sets the consistency level for the client to use. Defaults to One.

Required Parameters:
- level: Consistency level the client should use. Value is case
  insensitive.

  Supported values are:
    - ONE
    - TWO
    - THREE
    - QUORUM
    - ALL
    - LOCAL_QUORUM
    - EACH_QUORUM
    - ANY

  Note: Consistency level ANY can only be used for write operations.

4.2 创建测试schema
[default@unknown] create keyspace test;
e2998350-e468-11e3-0000-242d50cf1fff
Waiting for schema agreement...
... schemas agree across the cluster
[default@unknown] use test;
Authenticated to keyspace: test
[default@test] create column family sessioninfo;
060ae350-e46a-11e3-0000-242d50cf1fff
Waiting for schema agreement...
... schemas agree across the cluster

[default@test] show schema test;
create keyspace test
  with placement_strategy = 'NetworkTopologyStrategy'
  and strategy_options = {datacenter1 : 1}
  and durable_writes = true;

use test;

create column family sessioninfo
  with column_type = 'Standard'
  and comparator = 'BytesType'
  and default_validation_class = 'BytesType'
  and key_validation_class = 'BytesType'
  and rows_cached = 0.0
  and row_cache_save_period = 0
  and row_cache_keys_to_save = 2147483647
  and keys_cached = 200000.0
  and key_cache_save_period = 14400
  and read_repair_chance = 1.0
  and gc_grace = 864000
  and min_compaction_threshold = 4
  and max_compaction_threshold = 32
  and replicate_on_write = true
  and row_cache_provider = 'ConcurrentLinkedHashCacheProvider'
  and compaction_strategy = 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'

4.3 插入/删除/查询数据
为了在查询时正确显示列名称,需要设置,参考附录1。
[default@test] assume sessioninfo comparator as utf8;
Assumption for column family 'sessioninfo' added successfully.
[default@test] assume sessioninfo sub_comparator as utf8;
Assumption for column family 'sessioninfo' added successfully.
[default@test] assume sessioninfo validator as utf8;
Assumption for column family 'sessioninfo' added successfully.
[default@test] assume sessioninfo keys as utf8;
Assumption for column family 'sessioninfo' added successfully.

[default@test] set sessioninfo[utf8('1234')][utf8('name')]=utf8('mm');
Value inserted.
Elapsed time: 3 msec(s).
[default@test] set sessioninfo[utf8('1234')][utf8('security')]=utf8('etoken');
Value inserted.
Elapsed time: 2 msec(s).
[default@test] set sessioninfo[utf8('1234')][utf8('cust_id')]=utf8('1234');
Value inserted.
Elapsed time: 1 msec(s).
[default@test] get sessioninfo[utf8('1234')];
=> (column=cust_id, value=1234, timestamp=1401065057114000)
=> (column=name, value=mm, timestamp=1401065057088000)
=> (column=security, value=etoken, timestamp=1401065057095000)
Returned 3 results.
Elapsed time: 4 msec(s).

[default@test] del sessioninfo[utf8('1234')];
row removed.
[default@test] get sessioninfo[utf8('1234')];
Returned 0 results.
Elapsed time: 2 msec(s).

4.4自动清理过期数据
在设置列值是可以设置数据时效TTL(Time To Live),单位为秒,缺省无限制。

[default@test] set sessioninfo[utf8('1234')][utf8('name')]=utf8('mm') with ttl=180;
Value inserted.
Elapsed time: 2 msec(s).
[default@test] set sessioninfo[utf8('1234')][utf8('security')]=utf8('etoken') with ttl=120;
Value inserted.
Elapsed time: 2 msec(s).
[default@test] set sessioninfo[utf8('1234')][utf8('cust_id')]=utf8('1234') with ttl=60;
Value inserted.
Elapsed time: 2 msec(s).
[default@test]
[default@test] get sessioninfo[utf8('1234')];
=> (column=cust_id, value=1234, timestamp=1401066418714000, ttl=60)
=> (column=name, value=mm, timestamp=1401066418691000, ttl=180)
=> (column=security, value=etoken, timestamp=1401066418697000, ttl=120)
Returned 3 results.
Elapsed time: 4 msec(s).

60秒后查询:
[default@test] get sessioninfo[utf8('1234')];
=> (column=name, value=mm, timestamp=1401066418691000, ttl=180)
=> (column=security, value=etoken, timestamp=1401066418697000, ttl=120)
Returned 2 results.
Elapsed time: 4 msec(s).

120秒后查询:
[default@test] get sessioninfo[utf8('1234')];
=> (column=name, value=mm, timestamp=1401066418691000, ttl=180)
Returned 1 results.
Elapsed time: 3 msec(s).

180秒后查询:
[default@test] get sessioninfo[utf8('1234')];
Returned 0 results.
Elapsed time: 2 msec(s).


4.5 在动态增加列上使用二级索引
4.5.1 创建column family
[nosql@rhel5 ~]$ cat cass_cli.sh
/nosql/cassandra/apache-cassandra-1.0.10/bin/cassandra-cli -h 127.0.0.1 -p 9160

[nosql@rhel5 ~]$ ./cass_cli.sh

[default@unknown] use test;
Authenticated to keyspace: test

[default@test] create column family users with comparator=UTF8Type                  
...     and column_metadata=[{column_name:full_name,validation_class:UTF8Type},
...     {column_name:birth_date,validation_class:LongType,index_type:KEYS}];
37900280-e478-11e3-0000-242d50cf1fff
Waiting for schema agreement...
... schemas agree across the cluster

4.5.2 插入数据
[default@test] set users[utf8('usera')][utf8('full_name')]='Brandon Sanderson';
Value inserted.
Elapsed time: 12 msec(s).
[default@test] set users[utf8('usera')][utf8('birth_date')]=1975;
Value inserted.
Elapsed time: 5 msec(s).
[default@test] set users[utf8('userb')][utf8('full_name')]='Patrick Rothfuss';
Value inserted.
Elapsed time: 3 msec(s).
[default@test] set users[utf8('userb')][utf8('birth_date')]=1973;
Value inserted.
Elapsed time: 3 msec(s).
[default@test] set users[utf8('userc')][utf8('full_name')]='Howard Tayler';
Value inserted.
Elapsed time: 2 msec(s).
[default@test] set users[utf8('userc')][utf8('birth_date')]=1968;
Value inserted.
Elapsed time: 3 msec(s).
[default@test]
[default@test] get users[utf8('userc')];
=> (column=birth_date, value=1968, timestamp=1401069384512000)
=> (column=full_name, value=Howard Tayler, timestamp=1401069384497000)
Returned 2 results.
Elapsed time: 28 msec(s).


4.5.3 使用二级索引等值条件查询
[default@test] get users where birth_date=1973;
-------------------
RowKey: 7573657262
=> (column=birth_date, value=1973, timestamp=1401069384480000)
=> (column=full_name, value=Patrick Rothfuss, timestamp=1401069384461000)

1 Row Returned.
Elapsed time: 59 msec(s).
[default@test] get users where birth_date>1973;
No indexed columns present in index clause with operator EQ

4.5.4 直接新增列
[default@test] set users[utf8('usera')][utf8('state')]=utf8('FL');
Value inserted.
Elapsed time: 2 msec(s).
[default@test] set users[utf8('userb')][utf8('state')]=utf8('WD');
Value inserted.
Elapsed time: 4 msec(s).
[default@test] get users where birth_date=1973 and state='WD';
-------------------
RowKey: 7573657262
=> (column=birth_date, value=1973, timestamp=1401069384480000)
=> (column=full_name, value=Patrick Rothfuss, timestamp=1401069384461000)
=> (column=state, value=WD, timestamp=1401069751560000)

1 Row Returned.
Elapsed time: 9 msec(s).
[default@test] get users where birth_date=1973 and state>'WD';

0 Row Returned.
Elapsed time: 6 msec(s).

4.5.5 在新增列上加索引
[default@test] get users where state='WD';
No indexed columns present in index clause with operator EQ
[default@test]

[default@test] update column family users with comparator=UTF8Type
...     and column_metadata=[{column_name:full_name,validation_class:UTF8Type},
...     {column_name:birth_date,validation_class:LongType,index_type:KEYS},
...     {column_name:state,validation_class:UTF8Type,index_type:KEYS},
...     ];
23e610b0-e47a-11e3-0000-242d50cf1fff
Waiting for schema agreement...
... schemas agree across the cluster
[default@test] get users where state='WD';
-------------------
RowKey: 7573657262
=> (column=birth_date, value=1973, timestamp=1401069384480000)
=> (column=full_name, value=Patrick Rothfuss, timestamp=1401069384461000)
=> (column=state, value=WD, timestamp=1401069751560000)

1 Row Returned.
Elapsed time: 15 msec(s).
[default@test]

附录:
1,在没有设置column family字符集时,查询结果中列名称无法解读:

[default@test] get sessioninfo[utf8('1234')];
=> (column=637573745f6964, value=1234, timestamp=1401064283701000)
=> (column=6e616d65, value=mm, timestamp=1401064283667000)
=> (column=7365637572697479, value=etoken, timestamp=1401064283682000)
Returned 3 results.
Elapsed time: 4 msec(s).

单独设置column family字符集,可能导致查询不到希望的结果,所以需要在插入时指定字符集。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/18922393/viewspace-731551/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2011-05-26

  • 博文量
    211
  • 访问量
    788732