0
votes

I am trying to create a new docker container for Cassandra CQL which connects to Cassandra cluster that is running as another docker container.

Current situation:

I have created a new Cassandra image "cassandra" and container named "container-node" by executing below commands:

  • docker pull cassandra
  • docker images | grep cassandra
  • docker run -d --name cassandra-node --publish 9042:9042 cassandra
  • Connect to the container: docker exec -it cassandra-node bash (Connected to Test Cluster at 127.0.0.1:9042)

Keyspace creation

  • Create Keyspace: CREATE KEYSPACE first_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1}
  • Connect to Keyspace: USE first_keyspace
  • Create table: CREATE TABLE first_keyspace.user (USERID int PRIMARY KEY, USERNAME text, CITY text); Insert data:
  • INSERT INTO USER (USERID, USERNAME, CITY) VALUES (1, 'JOHN','PARIS');
  • INSERT INTO USER (USERID, USERNAME, CITY) VALUES (2, 'ABD','SAFRICA');
  • Fetch data: select * from user;

Problem statement in details

Wants to create a new Docker Container (Using Dockerfile) for CQL (cqlsh) which would perform below tasks:

  1. Connect to Cassandra cluster which running as Docker container (cassandra-host)
  2. Perform all task Add / Update / Delete function reading from source.sql file.

I would like to know where I can start. Any help, pointers would be grateful.

1
why not just use docker exec -ti running_image cqlsh ? - Alex Ott
Alex, thanks for your reply. I need another container as cqlsh and that would connect to the external Cassandra cluster to perform keyspace execution. - Its Soni
Just use the same image, just override entry point - Alex Ott

1 Answers

1
votes

Not sure if cassandra supports .sql files. I am thinking it is a typo.

command to load data from a .cql file is this:

bin/cqlsh --file 'file_name'

All you need to do is run a command like this in a container. But before that you need to make sure that file is accessible inside the container. Two ways to do this is to copy the file inside the container during image creation (for init script kind of requirements) and volume mapping (for other requirements). Sample Dockerfile:

FROM cassandra
COPY some-file.cql /home/some-file.cql
CMD["cqlsh","<cassandra-docker-container-ref>","-f","/home/some-file.sql"]

here cassandra-docker-container-ref can be id or name of your cassandra docker server. I would suggest to go with name.

build the image using a command like this:

docker build -t cassandra-seed-script .

and run the image as a container and remove it immediately:

docker run -it --rm cassandra-seed-script 

If you need to execute this regularly and with different cql files, volume map a location in container with host and make the command dynamic (or pass the command while starting a container).