Groovy で Apache Cassandra を組み込み実行してみました。
今回のソースは http://github.com/fits/try_samples/tree/master/blog/20170227/
組み込み実行
まずは、設定ファイルを用意しておきます。
今回は実行に必要な最小限の設定を行っています。
embed.conf
cluster_name: 'Test Cluster' listen_address: localhost commitlog_sync: periodic commitlog_sync_period_in_ms: 10000 partitioner: org.apache.cassandra.dht.Murmur3Partitioner endpoint_snitch: SimpleSnitch seed_provider: - class_name: org.apache.cassandra.locator.SimpleSeedProvider parameters: - seeds: "127.0.0.1" # CQL クライアントで接続するために必要 start_native_transport: true
ここで、Apache Cassandra 3.10 では thrift クライアントで接続するためのポート 9160 はデフォルトで有効のようですが、CQL クライアント用のポート 9042 を有効化するには start_native_transport の設定が必要でした。
| ポート番号 | 用途 |
|---|---|
| 9042 | CQL クライアント用 |
| 9160 | thrift クライアント用 |
Cassandra を組み込み実行する Groovy スクリプトは以下の通りです。
cassandra.config で設定ファイル、cassandra.storagedir でデータディレクトリのパスを設定、CassandraDaemon をインスタンス化して activate します。(deactivate を実行すると停止します)
cassandra_embed.groovy
@Grab('org.apache.cassandra:cassandra-all:3.10') import org.apache.cassandra.service.CassandraDaemon def conf = 'embed.yaml' def dir = new File(args[0]) if (!dir.exists()) { dir.mkdirs() } System.setProperty('cassandra.config', conf) System.setProperty('cassandra-foreground', 'true') System.setProperty('cassandra.storagedir', dir.absolutePath) def cassandra = new CassandraDaemon() // 開始 cassandra.activate() System.in.read() // 終了 cassandra.deactivate()
実行結果は以下の通りで、特に問題なく起動できました。
実行
> groovy cassandra_embed.groovy data ・・・ 21:30:46.493 [main] INFO o.apache.cassandra.transport.Server - Starting listening for CQL clients on localhost/127.0.0.1:9042 (unencrypted)... ・・・ 21:30:46.790 [Thread-1] INFO o.a.cassandra.thrift.ThriftServer - Listening for thrift clients...
動作確認
CQL クライアントを使って Cassandra への接続確認を行います。
cqlsh 利用
まずは、Cassandra 3.10 に同梱されている cqlsh コマンドを使って、キースペースとテーブルを作成しデータ登録を行います。
ここで、cqlsh (本体は cqlsh.py) の実行には Python の実行環境が必要です。
cqlsh による操作結果
> cqlsh
・・・
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.10 | CQL spec 3.4.4 | Native protocol v4]
・・・
cqlsh> CREATE KEYSPACE sample WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor' : 1};
cqlsh> use sample;
cqlsh:sample> CREATE TABLE data (id text PRIMARY KEY, name text, value int);
cqlsh:sample> INSERT INTO data (id, name, value) values ('d1', 'sample1', 1);
cqlsh:sample> INSERT INTO data (id, name, value) values ('d2', 'sample2', 20);
cqlsh:sample> INSERT INTO data (id, name, value) values ('d3', 'sample3', 300);
cqlsh:sample> SELECT * FROM data;
id | name | value
----+---------+-------
d2 | sample2 | 20
d1 | sample1 | 1
d3 | sample3 | 300
(3 rows)
Datastax Java Driver for Apache Cassandra 利用
次に、登録したデータを Datastax Java Driver for Apache Cassandra を使って検索してみます。
netty と jffi モジュールで Error grabbing Grapes -- [download failed: ・・・] となったので、@GrabExclude を使って回避しています。
client_sample.groovy
@Grapes([
@Grab('com.datastax.cassandra:cassandra-driver-core:3.1.4'),
@GrabExclude('io.netty#netty-handler;4.0.37'),
@GrabExclude('com.github.jnr#jffi;1.2.10')
])
@Grab('io.netty:netty-all:4.0.44.Final')
@Grab('org.slf4j:slf4j-nop:1.7.23')
import com.datastax.driver.core.Cluster
Cluster.builder().addContactPoint('localhost').build().withCloseable { cluster ->
cluster.connect('sample').withCloseable { session ->
def res = session.execute('select * from data')
res.each {
println it
}
}
}
実行結果は以下の通りです。
実行結果
> groovy client_sample.groovy Row[d2, sample2, 20] Row[d1, sample1, 1] Row[d3, sample3, 300]