IK.AM

@making's tech note


Cassandra0.7.0のインストールからCLIコマンドまとめまで

🗃 {Middleware/NoSQL/Cassandra}
🗓 Updated at 2010-12-13T19:35:51Z  🗓 Created at 2010-12-13T19:35:51Z   🌎 English Page

動作環境(OS)はWindows7 Professional 64bitです。 Cassandraバージョンはサーバ/クライアントともに0.7.0。**(2010/01/12更新)**

インストール

ここからバイナリをダウンロード。 Cドライブ直下に展開。

環境変数

必須ではないが、一応。 環境変数CASSANDRA_HOMEC:\apache-cassandra-0.7.0を設定。PATH%CASSANDRA_HOME%\binを追加。

JAVA_HOMEは設定しておくこと。

JMXのポート変更

デフォルト8080やめてほしい。。。 %CASSANDRA_HOME%\bin\cassandra.batの37行目あたりにある、-Dcom.sun.management.jmxremote.port=8080^の8080を8079(お好きなポート番号)に変更。 (読み取り専用のチェックをはずすこと。)

サーバ起動

%CASSANDRA_HOME%\bin\cassandra.batをダブルクリック。デフォルト設定だとC:\var\lib\cassandraにデータやコミットログ用のディレクトリ・ファイルが、C:\var\log\cassandraにシステムログが作成される。

変更したい場合は%CASSANDRA_HOME%\conf\cassandra.yamlの対応箇所を修正する。0.7系では設定ファイルがXMLからYAMLに変更されている。

クライアント起動

%CASSANDRA_HOME%\bin\cassandra-cli.batをダブルクリック。

Starting Cassandra Client
Welcome to cassandra CLI.

Type 'help;' or '?' for help. Type 'quit;' or 'exit;' to quit.
[default@unknown]

が表示される。

CLIコマンドまとめ

helpの表示

help;を実行するとコマンド一覧が表示される。(0.6系とは異なり、コマンドの末尾に;が必要となっていることに注意。)

[default@unknown] help;
List of all CLI commands:
?                                                          Display this message.
help;                                                          Display this help.
help <command>;                          Display detailed, command-specific help.
connect <hostname>/<port>;                             Connect to thrift service.
use <keyspace> [<username> 'password'];                     Switch to a keyspace.
describe keyspace <keyspacename>;                              Describe keyspace.
exit;                                                                   Exit CLI.
quit;                                                                   Exit CLI.
show cluster name;                                          Display cluster name.
show keyspaces;                                           Show list of keyspaces.
show api version;                                        Show server API version.
create keyspace <keyspace> [with <att1>=<value1> [and <att2>=<value2> ...]];
                Add a new keyspace with the specified attribute(s) and value(s).
update keyspace <keyspace> [with <att1>=<value1> [and <att2>=<value2> ...]];
                 Update a keyspace with the specified attribute(s) and value(s).
create column family <cf> [with <att1>=<value1> [and <att2>=<value2> ...]];
        Create a new column family with the specified attribute(s) and value(s).
update column family <cf> [with <att1>=<value1> [and <att2>=<value2> ...]];
            Update a column family with the specified attribute(s) and value(s).
drop keyspace <keyspace>;                                      Delete a keyspace.
drop column family <cf>;                                  Delete a column family.
get <cf>['<key>'];                                        Get a slice of columns.
get <cf>['<key>']['<super>'];                         Get a slice of sub columns.
get <cf> where <column> = <value> [and <column> > <value> and ...] [limit int];
get <cf>['<key>']['<col>'] (as <type>)*;                      Get a column value.
get <cf>['<key>']['<super>']['<col>'] (as <type>)*;       Get a sub column value.
set <cf>['<key>']['<col>'] = <value>;                               Set a column.
set <cf>['<key>']['<super>']['<col>'] = <value>;                Set a sub column.
del <cf>['<key>'];                                                 Delete record.
del <cf>['<key>']['<col>'];                                        Delete column.
del <cf>['<key>']['<super>']['<col>'];                         Delete sub column.
count <cf>['<key>'];                                     Count columns in record.
count <cf>['<key>']['<super>'];                  Count columns in a super column.
truncate <column_family>;                       Truncate specified column family.
assume <column_family> <attribute> as <type>;
 Assume one of the attributes of the given column family to match specified type.
list <cf>;                                    List all rows in the column family.
list <cf>[<startKey>:];
                        List rows in the column family beginning with <startKey>.
list <cf>[<startKey>:<endKey>];
         List rows in the column family in the range from <startKey> to <endKey>.
list ... limit N;                                    Limit the list results to N.
[default@unknown]

サーバに接続

connect <hostname>/<port>; で起動中のサーバに接続。

[default@unknown] connect localhost/9160;
Connected to: "Test Cluster" on localhost/9160

クライアント起動時に接続するホスト名、ポート番号を明示的に指定したい場合は、コマンドラインから

cassandra-cli --host <hostname> --port <portNo>

を実行する。

環境情報の表示

クラスタ名表示

[default@unknown] show cluster name;
Test Cluster

全キースペースの詳細表示

初期時はsystemキースペースのみ。

[default@unknown] show keyspaces;
Keyspace: system:
  Replication Strategy: org.apache.cassandra.locator.LocalStrategy
    Replication Factor: 1
  Column Families:
    ColumnFamily: HintsColumnFamily (Super)
    "hinted handoff data"
      Columns sorted by: org.apache.cassandra.db.marshal.BytesType/org.apache.cassandra.db.marshal.BytesType
      Row cache size / save period: 0.0/0
      Key cache size / save period: 0.01/3600
      Memtable thresholds: 0.2953125/63/60
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0
    ColumnFamily: IndexInfo
    "indexes that have been completed"
      Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type
      Row cache size / save period: 0.0/0
      Key cache size / save period: 0.01/3600
      Memtable thresholds: 0.2953125/63/60
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0
    ColumnFamily: LocationInfo
    "persistent metadata for the local node"
      Columns sorted by: org.apache.cassandra.db.marshal.BytesType
      Row cache size / save period: 0.0/0
      Key cache size / save period: 0.01/3600
      Memtable thresholds: 0.2953125/63/60
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0
    ColumnFamily: Migrations
    "individual schema mutations"
      Columns sorted by: org.apache.cassandra.db.marshal.TimeUUIDType
      Row cache size / save period: 0.0/0
      Key cache size / save period: 0.01/3600
      Memtable thresholds: 0.2953125/63/60
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0
    ColumnFamily: Schema
    "current state of the schema"
      Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type
      Row cache size / save period: 0.0/0
      Key cache size / save period: 0.01/3600
      Memtable thresholds: 0.2953125/63/60
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0

APIバージョン表示

[default@unknown] show api version;
19.4.0

キースペース作成

0.7からはCLIから動的にキースペースの作成ができます。

[default@unknown] create keyspace Keyspace1;
df57b271-06f7-11e0-bbf4-e700f669bcfc

replication_factorを指定したいときは

[default@unknown] create keyspace Keyspace1 with replication_factor=1;

キースペース選択

0.7からはCLIで作業中のキースペースを指定できるようになりました。これを指定することでカラムへのアクセスにキースペースを指定する必要がなくなります。

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

選択後はプロンプト文字列にキースペース名が表示されます。

カラムファミリ作成

カラムファミリも動的に作成できます。

[default@Keyspace1] create column family Users with comparator=UTF8Type;
e1c00a73-06f8-11e0-bbf4-e700f669bcfc

キースペース詳細表示

[default@Keyspace1] describe keyspace Keyspace1;
Keyspace: Keyspace1:
  Replication Strategy: org.apache.cassandra.locator.SimpleStrategy
    Replication Factor: 1
  Column Families:
    ColumnFamily: Users
      Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type
      Row cache size / save period: 0.0/0
      Key cache size / save period: 200000.0/3600
      Memtable thresholds: 0.2953125/63/60
      GC grace seconds: 864000
      Compaction min/max thresholds: 4/32
      Read repair chance: 1.0

データの書き込み

[default@Keyspace1] set Users[jsmith][first] = 'John';
Value inserted.
[default@Keyspace1] set Users[jsmith][last] = 'Smith';
Value inserted.
[default@Keyspace1] set Users[jsmith][age] = long(42);
Value inserted.

データの読み込み

[default@Keyspace1] get Users[jsmith];
=> (column=age, value=42, timestamp=1292341676501000)
=> (column=first, value=4a6f686e, timestamp=1292341664070000)
=> (column=last, value=536d697468, timestamp=1292341669705000)
Returned 3 results.

あれ、org.apache.cassandra.db.marshal.UTF8Typeのときって文字列が表示されなかったっけ。。。READMEでは文字列が表示されているけど。。

カラムの件数取得

[default@Keyspace1] count Users[jsmith];
3 columns

カラム削除

[default@Keyspace1] del Users[jsmith][first];
column removed.
[default@Keyspace1] get Users[jsmith];
=> (column=age, value=42, timestamp=1292341676501000)
=> (column=last, value=536d697468, timestamp=1292341669705000)
Returned 2 results.

レコード削除

[default@Keyspace1] del Users[jsmith];
row removed.
[default@Keyspace1] get Users[jsmith];
Returned 0 results.

カラムファミリ内データを列挙

[default@Keyspace1] list Users;
Using default limit of 100
-------------------
RowKey: jsmith
=> (column=age, value=42, timestamp=1292342768078000)
=> (column=first, value=4a6f686e, timestamp=1292342755365000)
=> (column=last, value=536d697468, timestamp=1292342763354000)
-------------------
RowKey: making
=> (column=age, value=26, timestamp=1292342810432000)
=> (column=first, value=546f736869616b69, timestamp=1292342786280000)
=> (column=last, value=4d616b69, timestamp=1292342796127000)

2 Rows Returned.

カラムファミリ内データを空にする

[default@Keyspace1] truncate Users;
Users truncated.
[default@Keyspace1] list Users;
Using default limit of 100

0 Row Returned.

カラムファミリ削除

[default@Keyspace1] drop column family Users;
7e03c178-079d-11e0-bbf4-e700f669bcfc
[default@Keyspace1] list Users;
Using default limit of 100
unconfigured columnfamily Users

キースペース削除

[default@Keyspace1] drop keyspace Keyspace1;
a7557419-079d-11e0-bbf4-e700f669bcfc
[default@Keyspace1] list Users;
Using default limit of 100
Keyspace Keyspace1 does not exist
Cassandra: The Definitive Guide
Eben Hewitt
Oreilly & Associates Inc
売り上げランキング: 4954

✒️️ Edit  ⏰ History  🗑 Delete