11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

docker-composeでサクッとCassandraをたてる

Posted at

docker-composeでお手軽にCassandraを立ち上げるサンプルとその簡単な説明

まずは下記のようなディレクトリ構造でファイルを作成していく

sample
 ┣━ docker
 ┃   ┗ cassandra
 ┃       ┗ initdb
 ┃           ┗ schema.cql
 ┗ docker-compose.yml

次にdocker-compose.ymlを記述していく

docker-compose.yml
version: '3.3'
services:
  cassandra:
    image: cassandra:latest
    ports:
      - 9044:9042
    environment:
      - CASSANDRA_USER=user
      - CASSANDRA_PASSWORD=pass
      - MAX_HEAP_SIZE=256M
      - HEAP_NEWSIZE=128M
    container_name: cassandra
    restart: always
    volumes:
      - ./out/cassandra_data:/var/lib/cassandra

  cassandra-load-keyspace:
    container_name: cassandra-load-keyspace
    image: cassandra:latest
    depends_on:
      - cassandra
    volumes:
      - ./docker/cassandra/initdb/schema.cql:/schema.cql
    command: /bin/bash -c "sleep 60 && echo loading cassandra keyspace && cqlsh cassandra -f /schema.cql"

cassandraコンテナはそのままCassandraを立ち上げるコンテナ
cassandra-load-keyspaceコンテナは立ち上がったcassandraコンテナに対してテスト用schemaを流し込むコンテナ
cassandraは起動に時間がかかるので60秒まったあとにcqlをたたいている

schema.cql

DROP KEYSPACE IF EXISTS test;

CREATE KEYSPACE test WITH replication = {
  'class': 'SimpleStrategy',
  'replication_factor' : 1
};

USE test;

CREATE TABLE test_table (
                            id text PRIMARY KEY,
                            data text,
                            update_time timestamp
) WITH default_time_to_live = 31536000;

INSERT INTO test.test_table (id, data, update_time)
 VALUES('test_id', 'text', '2019-09-10T11:21:59.001+0000');

コンテナに直接アクセスしてcqlshを立ち上げるコマンド

docker exec -it cassandra cqlsh
11
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?