先週、Apache Cassandraをインストールしました。
で、そこでなぜか終わっていたので、続きを進めようと思います。いきなりプログラムから操作するとかじゃなくて、まずはコマンドラインツールからいくのがきっと無難ですよね。
とりあえず、Cassandraを起動して。
$ cassandra -f xss = -ea -javaagent:/usr/local/cassandra/current/lib/jamm-0.2.5.jar -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42 -Xms1024M -Xmx1024M -Xmn200M -XX:+HeapDumpOnOutOfMemoryError -Xss180k INFO 15:20:08,890 Logging initialized 〜省略〜 INFO 15:29:18,973 Using TFramedTransport with a max frame size of 15728640 bytes. INFO 15:29:18,981 Using synchronous/threadpool thrift server on localhost : 9160 INFO 15:29:18,983 Listening for thrift clients...
cassandra-cliで繋ぎます。
$ cassandra-cli Connected to: "Test Cluster" on 127.0.0.1/9160 Welcome to Cassandra CLI version 1.2.4 Type 'help;' or '?' for help. Type 'quit;' or 'exit;' to quit. [default@unknown]
まずは、ヘルプを見てみます。
[default@unknown] help; Getting around: ? Display this help. help; Display this help. help <command>; 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 its column families or column family in current keyspace. drop column family Remove a column family and its data. drop keyspace Remove a keyspace and its 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.
命令は、「;」で終わらせる必要があるみたいです。タブ補完も効きますね。
キースペースというものが、いわゆるRDBMSのデータベースに相当するものらしいので、これを見てみましょう。
[default@unknown] show keyspaces; WARNING: CQL3 tables are intentionally omitted from 'show keyspaces' output. See https://issues.apache.org/jira/browse/CASSANDRA-4377 for details. Keyspace: system: Replication Strategy: org.apache.cassandra.locator.LocalStrategy Durable Writes: true Options: [] Column Families: ColumnFamily: HintsColumnFamily (Super) "unused" 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 GC grace seconds: 0 Compaction min/max thresholds: 4/32 Read repair chance: 0.0 DC Local Read repair chance: 0.0 Populate IO Cache on flush: false Replicate on write: true Caching: KEYS_ONLY Bloom Filter FP chance: default Built indexes: [] Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy ColumnFamily: IndexInfo "indexes that have been completed" 〜省略〜
今は、systemという名前のキースペースがあるらしいです。まだ何も作っていないので、当然のことながら他にはありません。
じゃ、Getting Startedを見ながら、ちょっと触ってみます。
http://wiki.apache.org/cassandra/GettingStarted
[default@unknown] create keyspace DEMO ... with placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy' ... and strategy_options = {replication_factor:1}; c3095be3-7ff0-344e-9521-b12fbed3110f
DEMOという名前のキースペースを作りました。「;」を打たなければ、改行に続けて次の命令を記述できるみたいですね。
show keyspecesにも、現れます。
[default@unknown] show keyspaces; WARNING: CQL3 tables are intentionally omitted from 'show keyspaces' output. See https://issues.apache.org/jira/browse/CASSANDRA-4377 for details. Keyspace: DEMO: Replication Strategy: org.apache.cassandra.locator.SimpleStrategy Durable Writes: true Options: [replication_factor:1] Column Families: Keyspace: system: Replication Strategy: org.apache.cassandra.locator.LocalStrategy Durable Writes: true Options: [] Column Families:
カラムファミリーは、まだありませんけどね。
現在のキースペースを、たった今作成したDEMOに切り替えます。
[default@unknown] use DEMO; Authenticated to keyspace: DEMO
「use」で。
以降、コンソールの表示が変わります。
[default@DEMO]
続いて、Usersというカラムファミリーを作成します。カラムファミリーが、RDBMSでいうテーブルのようなもの?
[default@DEMO] create column family Users ... with key_validation_class = 'UTF8Type' ... and comparator = 'UTF8Type' ... and default_validation_class = 'UTF8Type'; 75f441be-8a53-3e13-9aad-f0d13437b1b9
指定しているパラメータは、デフォルトの文字コードらしいです。
ところで、コマンドとかカラムファミリーの大文字・小文字は区別していない気がします。
「describe」で、作成したカラムファミリーの定義情報が見れます。
[default@DEMO] describe Users; WARNING: CQL3 tables are intentionally omitted from 'describe' output. See https://issues.apache.org/jira/browse/CASSANDRA-4377 for details. ColumnFamily: Users Key Validation Class: org.apache.cassandra.db.marshal.UTF8Type Default column value validator: org.apache.cassandra.db.marshal.UTF8Type Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type GC grace seconds: 864000 Compaction min/max thresholds: 4/32 Read repair chance: 0.1 DC Local Read repair chance: 0.0 Populate IO Cache on flush: false Replicate on write: true Caching: KEYS_ONLY Bloom Filter FP chance: default Built indexes: [] Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy Compression Options: sstable_compression: org.apache.cassandra.io.compress.SnappyCompressor
では、カラムを登録してみます。「set」で行うようです。
[default@DEMO] set Users[1234][name] = scott; Value inserted. Elapsed time: 126 msec(s). [default@DEMO] set Users[1234][password] = tiger; Value inserted. Elapsed time: 7.16 msec(s).
scott/tigerって…。
ここで、
Users[1234][name] Users[1234][password]
の「1234」が行キーで、「name」「password」という2つのカラムを追加したってことになるみたいです。
取得は、「get」で。
[default@DEMO] get Users[1234]; => (column=name, value=scott, timestamp=1366440572283000) => (column=password, value=tiger, timestamp=1366440587081000) Returned 2 results. Elapsed time: 54 msec(s).
先の例では、カラムに設定する値は、特に引用符で囲いませんでしたが、通常はシングルクォートで囲むものでしょうか?
[default@DEMO] set Users[5678][name] = 'Kazuhira'; Value inserted. Elapsed time: 8.76 msec(s). [default@DEMO] set Users[5678][password] = 'password'; Value inserted. Elapsed time: 8.52 msec(s).
現在登録しているデータは、「list」で見れるようです。
[default@DEMO] list Users; Using default limit of 100 Using default column limit of 100 ------------------- RowKey: 5678 => (column=name, value=Kazuhira, timestamp=1366440922641000) => (column=password, value=password, timestamp=1366440935157000) ------------------- RowKey: 1234 => (column=name, value=scott, timestamp=1366440572283000) => (column=password, value=tiger, timestamp=1366440587081000) 2 Rows Returned. Elapsed time: 56 msec(s).
カラムを削除するには、「del」で。
[default@DEMO] del Users[5678][password]; column removed. Elapsed time: 64 msec(s).
行ごと削除する時も、「del」で。
[default@DEMO] del Users[1234]; row removed. Elapsed time: 5.45 msec(s).
カラムファミリーごと削除するには、「drop column family」で。
[default@DEMO] drop column family Users; c4bd1c0e-ddda-3366-b5df-0c9f9e13e69d
キースペースを削除するには、「drop keyspace」で。
[default@unknown] drop keyspace DEMO; f77771ff-d67d-32c4-bb7e-89a418411324
では、さようなら。
[default@unknown] exit;