Its Soni
Its Soni

Reputation: 41

Cassandra CQL (cqlsh) docker container to docker Cassandra cluster

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:

Keyspace creation

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.

Upvotes: 0

Views: 7586

Answers (1)

Suhas NM
Suhas NM

Reputation: 1090

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).

Upvotes: 1

Related Questions