Apache Cassandra 0.7で動的キースペース作成
Apache Cassandraの0.6.xまではキースペースとカラムファミリ(RDBでいうデータベースとテーブルのような単位)を設定ファイルに記述していたため、設定変更には再起動が必要だった。しかし、0.7ではこれを動的に作成や変更ができるようになっているとのことだったので試してみた。
0.7.0-beta1をダウンロードして展開する。
$ wget ftp://ftp.riken.jp/net/apache//cassandra/0.7.0/apache-cassandra-0.7.0-beta1-bin.tar.gz $ tar xzvf apache-cassandra-0.7.0-beta1-bin.tar.gz $ cd apache-cassandra-0.7.0-beta1/
設定ファイルを書き換える。0.6.xまではconf/storage-conf.xmlに設定していたが、0.7ではconf/cassandra.yamlになっている。bin/config-converterで変換できるようだが今回は試していない。設定項目はいくつかあるが、設定ファイルのコメントを読めば理解するのは難しくないだろう。とりあえず今回は簡単なテストを行うだけなので、data_file_directoriesとcommitlog_directoryをホームディレクトリ以下に設定しておいた。
$ vim conf/cassandra.yaml # directories where Cassandra should store data on disk. data_file_directories: # - /var/lib/cassandra/data - /home/y_tag/tmp/cassandra/data # commit log #commitlog_directory: /var/lib/cassandra/commitlog commitlog_directory: /home/y_tag/tmp/cassandra/commitlog
今回はログの出力先もホームディレクトリ以下にしておく。
$ vim conf/log4j-server.properties # Edit the next line to point to your logs directory #log4j.appender.R.File=/var/log/cassandra/system.log log4j.appender.R.File=/home/y_tag/tmp/cassandra/log/system.log
cassandraの起動。
$ bin/cassandra -f
別の端末で起動していることを確認。
$ jps 2057 CassandraDaemon 2172 Jps $ bin/nodetool --host localhost ring Address Status State Load Token 127.0.0.1 Up Normal 385 bytes 24466155087284623924305384460698872313
コマンドラインインターフェースを起動。起動してからconnectでも接続先サーバを指定できる。
$ bin/cassandra-cli --host localhost --port 9160
キースペースを確認する。0.6.xではデフォルトでconf/storage-conf.xmlに記述されていたキースペースやキーファミリが存在したが、0.7では動的に作成できるようになったので、初めはsystemしかない。
[default@unknown] show keyspaces system
キースペースを作成する。helpに詳しい記述があるのでそれに従う。属性を省略すると初期値が用いられるようだ。今回は1ノードしかないのでreplication_factorは1を指定した。ちなみにplacement_strategyを指定するとエラーが出てうまくいかなかった(クラスパスが通っていない?)。
[default@unknown] help create keyspace create keyspace <keyspace> create keyspace <keyspace> with <att1>=<value1> create keyspace <keyspace> with <att1>=<value1> and <att2>=<value2> ... Create a new keyspace with the specified values for the given set of attributes. valid attributes are: replication_factor: to how many nodes should entries to this keyspace be replicated. Valid entries are integers greater than 0. placement_strategy: the fully qualified class used to place replicas in this keyspace. Valid values are org.apache.cassandra.locator.RackUnawareStrategy, org.apache.cassandra.locator.DatacenterShardStrategy, and org.apache.cassandra.locator.RackAwareStrategy example: create keyspace foo with replication_factor = 3 and placement_strategy = 'org.apache.cassandra.locator.RackUnawareStrategy' [default@unknown] create keyspace Keyspace1 with replication_factor = 1 e044d8f4-c18a-11df-8319-e700f669bcfc
キースペースが作成できたことを確認。
[default@Keyspace1] show keyspaces Keyspace1 system
キースペースを選択する。MySQLでデータベースを選択するように、use <キースペース名>という形。
[default@unknown] use Keyspace1 Authenticated to keyspace: Keyspace1
カラムファミリを作成する。helpを見てスタンダードなカラムとスーパーカラムをそれぞれ2つ作ってみた。
[default@unknown] help create column family create column family Bar create column family Bar with <att1>=<value1> create column family Bar with <att1>=<value1> and <att2>=<value2>... Create a new column family with the specified values for the given set of attributes. Note that you must be using a keyspace. valid attributes are: - column_type: One of Super or Standard - clock_type: Timestamp - comparator: The class used as a comparator when sorting column names. Valid options include: AsciiType, BytesType, LexicalUUIDType, LongType, TimeUUIDType, and UTF8Type - subcomparator: Name of comparator used for subcolumns (when column_type=Super only). Valid options are identical to comparator above. - reconciler: Name of reconciler class that determines what to do with conflicting versions of a column. Timestamp is currently the only valid value. - comment: Human-readable column family description. Any string is valid. - rows_cached: Number of rows to cache - preload_row_cache: Set to true to automatically load the row cache - key_cache_size: Number of keys to cache - read_repair_chance: Valid values for this attribute are any number between 0.0 and 1.0 example: create column family bar with column_type = 'Super' and comparator = 'AsciiType' and rows_cached = 10000 create column family baz with comparator = 'LongType' and rows_cached = 10000 [default@Keyspace1] create column family Standard1 with column_type = 'Standard' and comparator = 'BytesType' a5fb2a95-c18b-11df-8319-e700f669bcfc [default@Keyspace1] create column family Standard2 with column_type = 'Standard' and comparator = 'UTF8Type' 21dd7dc6-c18c-11df-8319-e700f669bcfc [default@Keyspace1] create column family Super1 with column_type = 'Super' and comparator = 'BytesType' 72c79a47-c18c-11df-8319-e700f669bcfc [default@Keyspace1] create column family Super2 with column_type = 'Super' and comparator = 'UTF8Type' 8c5ea898-c18c-11df-8319-e700f669bcfc
カラムファミリが作成できていることを確認。
[default@Keyspace1] describe Keyspace1 line 1:9 missing K_TABLE at 'Keyspace1' Keyspace: Keyspace1 Column Family Name: Standard2 Column Family Type: Standard Column Sorted By: org.apache.cassandra.db.marshal.UTF8Type flush period: null minutes ------ Column Family Name: Super1 Column Family Type: Super Column Sorted By: org.apache.cassandra.db.marshal.BytesType flush period: null minutes ------ Column Family Name: Super2 Column Family Type: Super Column Sorted By: org.apache.cassandra.db.marshal.UTF8Type flush period: null minutes ------ Column Family Name: Standard1 Column Family Type: Standard Column Sorted By: org.apache.cassandra.db.marshal.BytesType flush period: null minutes ------
データの挿入と確認。
[default@Keyspace1] set Standard2['jsmith']['first'] = 'John' Value inserted. [default@Keyspace1] set Standard2['jsmith']['last'] = 'Smith' Value inserted. [default@Keyspace1] set Standard2['jsmith']['age'] = '42' Value inserted. [default@Keyspace1] get Standard2['jsmith'] => (column=last, value=Smith, timestamp=1284639927152000) => (column=first, value=John, timestamp=1284639914008000) => (column=age, value=42, timestamp=1284639934760000) Returned 3 results.