CLOVER🍀

That was when it all began.

Ubuntu LinuxにApache Cassandra 3をインストールする

久しぶりに、Ubuntu LinuxApache Cassandraをインストールしてみようと思います。

Apache Cassandra

前に使った時は、まだバージョン 1系でしたが、今は3系なんですねー。現在は、3.9が最新の
バージョンみたいです。

なんと、Debian系にはパッケージが提供されているみたいなので、こちらを使います。
※どうしてyumでは入れられないんでしょう…?

ところで、ドキュメントについてはこちらを見ることになりそうです。

Apache Cassandra Documentation

もしくは、DataStaxのもの。

DataStax Docs

日本語版もありそう。

DataStaxドキュメント・ホーム

インストール

では、手順に従ってインストールしてみます。

Download

Installing Cassandra

$ echo "deb http://www.apache.org/dist/cassandra/debian 39x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
$ curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install cassandra

GPGのエラーが出る場合は、追加の手順が必要みたいです。

起動。

$ sudo service cassandra start

停止。

$ sudo service cassandra stop

動作状況の確認は、「nodetool」で。

$ nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address    Load       Tokens       Owns (effective)  Host ID                               Rack
UN  127.0.0.1  140.15 KiB  256          100.0%            b3c076ea-24d3-4582-8e3c-226f71b8c960  rack1

各種(デフォルト)ディレクト

設定は、「/etc/cassandra」ディレクトリに置かれます。

$ ls -l /etc/cassandra
total 100
-rw-r--r-- 1 root root 11854 Sep 26 15:05 cassandra-env.sh
-rw-r--r-- 1 root root  1200 Sep 26 14:55 cassandra-rackdc.properties
-rw-r--r-- 1 root root  1358 Sep 26 14:55 cassandra-topology.properties
-rw-r--r-- 1 root root 51739 Sep 26 15:05 cassandra.yaml
-rw-r--r-- 1 root root  2082 Sep 26 14:55 commitlog_archiving.properties
-rw-r--r-- 1 root root  9074 Sep 26 14:55 jvm.options
-rw-r--r-- 1 root root  1193 Sep 26 14:55 logback-tools.xml
-rw-r--r-- 1 root root  3785 Sep 26 14:55 logback.xml
drwxr-xr-x 2 root root  4096 Jan 11 14:00 triggers

ログは、「/var/log/cassandra」ディレクトリに出力されます。

$ ls -l /var/log/cassandra
total 200
-rw-r--r-- 1 cassandra cassandra 136219 Jan 11 14:02 debug.log
-rw-r--r-- 1 cassandra cassandra  22441 Jan 11 14:03 gc.log.0.current
-rw-r--r-- 1 cassandra cassandra  39783 Jan 11 14:02 system.log

データは、「/var/lib/cassandra」ディレクトリに配置されます、と。

$ ls -l /var/lib/cassandra
total 16
drwxr-xr-x 2 cassandra cassandra 4096 Jan 11 14:01 commitlog
drwxr-xr-x 7 cassandra cassandra 4096 Jan 11 14:01 data
drwxr-xr-x 2 cassandra cassandra 4096 Jan 11 14:01 hints
drwxr-xr-x 2 cassandra cassandra 4096 Jan 11 14:01 saved_caches

CQLで確認

最後に、CQLを使って確認してみましょう。

$ cqlsh localhost
Connected to Test Cluster at localhost:9042.
[cqlsh 5.0.1 | Cassandra 3.9 | CQL spec 3.4.2 | Native protocol v4]
Use HELP for help.
cqlsh>

CQLのドキュメントは、DataStaxのものを見た方が良さそうです。

CQL for Cassandra 2.2以降

CQL for Apache Cassandra 3.0 (Earlier version)

キースペースの作成。

cqlsh> CREATE KEYSPACE IF NOT EXISTS test_keyspace WITH REPLICATION = { 'class': 'SimpleStrategy', 'replication_factor': 3 };

作成したキースペースの利用。

cqlsh> USE test_keyspace;
cqlsh:test_keyspace>

テーブルの作成。今回は、書籍で。

cqlsh:test_keyspace> CREATE TABLE book ( isbn text PRIMARY KEY, title text, price int);

確認。

cqlsh:test_keyspace> DESCRIBE book;

CREATE TABLE test_keyspace.book (
    isbn text PRIMARY KEY,
    price int,
    title text
) WITH bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';

データの登録。

cqlsh:test_keyspace> INSERT INTO book ( isbn, title, price ) VALUES ('978-4873115290', 'Cassandra', 3672);
cqlsh:test_keyspace> INSERT INTO book ( isbn, title, price ) VALUES ('978-1449358549', 'Elasticsearch: The Definitive Guide', 5432);
cqlsh:test_keyspace> INSERT INTO book ( isbn, title, price ) VALUES ('978-1449305048', 'Redis Cookbook', 2505);

確認。

cqlsh:test_keyspace> SELECT * FROM book;

 isbn           | price | title
----------------+-------+-------------------------------------
 978-1449358549 |  5432 | Elasticsearch: The Definitive Guide
 978-4873115290 |  3672 |                           Cassandra
 978-1449305048 |  2505 |                      Redis Cookbook

(3 rows)

ソートは、このテーブル定義だとできません、と…。

いったん、ここまでで。