さくらVPSでApache Cassandra 0.7の動的キースペースを作成してみる。
ちょうど、Apache Cassandra 0.7が2011年1月11日(火)にリリースされました。
The Apache Software Foundation Announces Apache Cassandra 0.7
昨日、「Cassandra0.7.0-beta1」をインストールしたばかりですが、せっかくなので
リリースバージョンに変更します。
若干、cassandra.yamlの記述が違うところがありましたが大枠Cassandra0.7.0-beta1と一緒なので
「さくらVPSで稼動しているCassandra0.6.5をCassandra0.7.0-beta1に変更してみる。」を参考にしてみてください。
動的キースペースを作成してみる
「cassandra-cli」でコネクション作成後、「help;」コマンドで利用できるコマンドの
一覧を確認することができます。
「help;」コマンドの一覧から動的キースペースを作成するコマンドだけ抜粋してみます。
抜粋 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.
上記のコマンドを元に、「create keyspace」と「create column family」を作成してみます。
[default@unknown] create keyspace Keyspace1; 04e0a64c-1df1-11e0-ac4c-73a24beda04e [default@unknown] use Keyspace1; Authenticated to keyspace: Keyspace1 [default@Keyspace1] [default@Keyspace1] create column family Standard1 with column_type = 'Standard' and comparator = 'BytesType' 5e890f9d-1e12-11e0-ac4c-73a24beda04e [default@Keyspace1] create column family Standard2 with column_type = 'Standard' and comparator = 'UTF8Type' 85c7247e-1e12-11e0-ac4c-73a24beda04e [default@Keyspace1] create column family Super1 with column_type = 'Super' and comparator = 'BytesType' a4573d3f-1e12-11e0-ac4c-73a24beda04e [default@Keyspace1] create column family Super2 with column_type = 'Super' and comparator = 'UTF8Type'; c00da920-1e12-11e0-ac4c-73a24beda04e
作成したキースペースは、「describe keyspace Keyspace1」で確認することができます。
[default@unknown] describe keyspace Keyspace1; Keyspace: Keyspace1: Replication Strategy: org.apache.cassandra.locator.SimpleStrategy (略)
また、「use Keyspace1;」で作業領域を「Keyspace1」に移動させることができます。
作成したキースペースを利用してみる
「さくらでcassandra Javaからアクセスしてみる」で「cassandra-0.6.5」のclientアプリの実装方法を
紹介しましたが、「Cassandra 0.7」でなにが変わったのか確認をしてみたいと思う。
必要なjarを準備する
apache-cassandra-0.7.0.jar libthrift-0.5.jar (環境によっては、「log4j」「slf4j」が必要になります)
テストデータを準備する
キースペース「Keyspace1」のカラムファミリー「Standard2」に下記のデータを
作成しました。
set Standard2['michibatajessica']['first'] = 'Jessica' set Standard2['michibatajessica']['last'] = 'Michibata' set Standard2['michibatajessica']['age'] = '25'
設定したデータを「cassandra-cli」で確認します。
[default@Keyspace1] get Standard2['michibatajessica']; => (column=age, value=3235, timestamp=1294815391760000) => (column=first, value=4a657373696361, timestamp=1294815379493000) => (column=last, value=4d6963686962617461, timestamp=1294815386590000) Returned 3 results.
「get」メソッドを利用する。
「Cassandra--The-Definitive-Guide」を参考に実装してみました。
TFramedTransportを利用して、通信をします。
TTransport port = new TFramedTransport(new TSocket("localhost", 9160)); TProtocol protocol = new TBinaryProtocol(port); Cassandra.Client client = new Cassandra.Client(protocol); port.open(); client.set_keyspace("Keyspace1");
Cassandra.Clientにset_keyspaceができたみたいです。
port.open()を呼出し後、設定しないとだめみたいです。(存在チェックをしている?)
「get」メソッドの利用の仕方に関しては、下記の様な感じになります。
前と引数の数が変わっているので若干違和感を感じましたが、すっきりして分かりやすいと
思いました。
final ByteBuffer key = ByteBuffer.wrap("michibatajessica".getBytes("UTF-8")); ColumnPath columnPath = new ColumnPath(columnFamily); columnPath.setColumn(columnName.getBytes("UTF8")); Column column = client.get(key, columnPath, ConsistencyLevel.ONE).getColumn();
「insert」メソッドを利用する。
String key = "jsmith"; String columnName = "foo1"; String value = "bar"; client.insert(ByteBuffer.wrap(key.getBytes("UTF-8")), cp, new Column(ByteBuffer.wrap(columnName.getBytes("UTF-8")), ByteBuffer.wrap(value.getBytes("UTF-8")), System.currentTimeMillis()), ConsistencyLevel.ONE);
「remove」メソッドを利用する。
String key = "jsmith"; String columnName = "foo1"; ColumnPath columnPath = new ColumnPath(columnFamily); columnPath.setColumn(columnName.getBytes("UTF8")); client.remove(ByteBuffer.wrap(key.getBytes("UTF-8")), columnPath, System.currentTimeMillis(), ConsistencyLevel.ALL);
とりあえず、CRUDのCRDは実行できるようになりました。
他にも使い方が違うものがあるので、引き続き調べてみたいとおもいます。