wiki:java/Cassandra

インストール

中身はJavaなので、ダウンロードして適当なディレクトリに解凍すればインストール完了。apache-cassandra-x.x.x/binディレクトリに移動して、

> cassandra.bat

で起動。こんな感じのメッセージが表示される。

Starting Cassandra Server
Listening for transport dt_socket at address: 8888
 INFO 13:17:34,504 Auto DiskAccessMode determined to be standard
 INFO 13:17:34,648 Replaying \var\lib\cassandra\commitlog\CommitLog-127501974808
4.log
 INFO 13:17:34,702 Creating new commitlog segment /var/lib/cassandra/commitlog\C
ommitLog-1275020254702.log
 INFO 13:17:34,735 Standard1 has reached its threshold; switching in a fresh Mem
table at CommitLogContext(file='/var/lib/cassandra/commitlog\CommitLog-127502025
4702.log', position=121)
 INFO 13:17:34,735 Enqueuing flush of Memtable(Standard1)@32110028
 INFO 13:17:34,736 Writing Memtable(Standard1)@32110028
 INFO 13:17:34,737 LocationInfo has reached its threshold; switching in a fresh
Memtable at CommitLogContext(file='/var/lib/cassandra/commitlog\CommitLog-127502
0254702.log', position=121)
 INFO 13:17:34,738 Enqueuing flush of Memtable(LocationInfo)@6292125
 INFO 13:17:35,010 Completed flushing E:\var\lib\cassandra\data\Keyspace1\Standa
rd1-1-Data.db
 INFO 13:17:35,019 Writing Memtable(LocationInfo)@6292125
 INFO 13:17:35,187 Completed flushing E:\var\lib\cassandra\data\system\LocationI
nfo-1-Data.db
 INFO 13:17:35,206 Log replay complete
 INFO 13:17:35,254 Saved Token found: 127433031769127593372296726511185514191
 INFO 13:17:35,254 Saved ClusterName found: Test Cluster
 INFO 13:17:35,264 Starting up server gossip
 INFO 13:17:35,297 Binding thrift service to localhost/127.0.0.1:9160
 INFO 13:17:35,301 Cassandra starting up...

デフォルトのポートは9160の模様。

接続

サーバを起動したら、クライアントから接続してみよう。コマンドラインのクライアントcassandra-cliが用意されているので、それを使ってみる。

cassandra-cli.bat -host localhost -port 9160
-username todd -keyspace Keyspace1  -password blah
Starting Cassandra Client
Connected to: "Test Cluster" on localhost/9160
Welcome to cassandra CLI.

Type 'help' or '?' for help. Type 'quit' or 'exit' to quit.
cassandra>

キースペース(RDBMSにおけるスキーマのようなもの)の一覧の確認。

cassandra> show keyspaces
Keyspace1
system

Keyspace1とsystemが利用できることが確認できる。次に、キースペースの内容を確認する。

cassandra> describe keyspace Keyspace1
Keyspace1.Super1
Column Family Type: Super
Columns Sorted By: org.apache.cassandra.db.marshal.BytesType@128215d

Column Family Type: Super
Column Sorted By: org.apache.cassandra.db.marshal.BytesType
flush period: null minutes
------
Keyspace1.Standard2
Column Family Type: Standard
Columns Sorted By: org.apache.cassandra.db.marshal.UTF8Type@1a697a1

Column Family Type: Standard
Column Sorted By: org.apache.cassandra.db.marshal.UTF8Type
flush period: null minutes
------
Keyspace1.Super2
Column Family Type: Super
Columns Sorted By: org.apache.cassandra.db.marshal.UTF8Type@2cd728

Column Family Type: Super
Column Sorted By: org.apache.cassandra.db.marshal.UTF8Type
flush period: null minutes
------
Keyspace1.Standard1
Column Family Type: Standard
Columns Sorted By: org.apache.cassandra.db.marshal.BytesType@1e1a0ee

Column Family Type: Standard
Column Sorted By: org.apache.cassandra.db.marshal.BytesType
flush period: null minutes
------
Keyspace1.StandardByUUID1
Column Family Type: Standard
Columns Sorted By: org.apache.cassandra.db.marshal.TimeUUIDType@c954e

Column Family Type: Standard
Column Sorted By: org.apache.cassandra.db.marshal.TimeUUIDType
flush period: null minutes
------
cassandra>

Super1,Super2,Standard1,Standard2,StandardByUUID1のカラムファミリ(RDBMSにおけるテーブル)が存在する。 Standard1に値の挿入と取得をやってみる。

cassandra> set Keyspace1.Standard1['aaaa']['bbb']='ccc'
Value inserted.
cassandra> get Keyspace1.Standard1['aaaa']
=> (column=626262, value=ccc, timestamp=1275020194179000)
Returned 1 results.

Standard2は、UTF8とみなしてソートしてくれるみたいなので、日本語を扱うならこっちの方がいいかも。

Keyspace1.Standard2
Column Family Type: Standard
Columns Sorted By: org.apache.cassandra.db.marshal.UTF8Type@1a697a1