Skip to content

Instantly share code, notes, and snippets.

@sualeh
Last active January 22, 2023 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sualeh/ab87647dd56b1fa776504d55c402ba6a to your computer and use it in GitHub Desktop.
Save sualeh/ab87647dd56b1fa776504d55c402ba6a to your computer and use it in GitHub Desktop.
How to Document Your Cassandra Database with One Command (and Nothing to Install)

How to Document Your Cassandra Database with One Command (and Nothing to Install)

Ever wondered what the tables in your Cassandra database look like? Generate a text file of your Cassandra database with one command. You can run this on any system that has Docker installed.

For now, create a Cassandra database in a Docker container. Later you can use the same technique to conect to your own Cassandra database. Create two Docker Compose files for the Cassandra and SchemaCrawler containers. Also create one more with the CQL database creation script. You can find these files in the GitHub gist for this article.

Then open a command shell in the same folder that you created these files, and run:

docker-compose -f schemacrawler.yml -f cassandra.yml up -d

Wait for a while for the containers to start up, and then connect into the Cassandra container by running:

docker exec -it cassandra bash

In the Cassandra container shell, run the script to create a sample database, and exit the shell:

cqlsh -f /share/database.cql
exit

Now that the Cassandra database is created, connect into the SchemaCrawler container by running:

docker exec -it schemacrawler bash

In the SchemaCrawler container shell, run SchemaCrawler against the Cassandra database to see your table. Run:

schemacrawler \
  --url jdbc:cassandra://cassandra:9042/store \
  --user cassandra --password cassandra \
  --info-level minimum \
  --command list

After you have got this working, you can alter the command to show more details of the database. For example, you can run:

schemacrawler \
  --url jdbc:cassandra://cassandra:9042/store \
  --user cassandra --password cassandra \
  --info-level standard \
  --command schema

You can save the output with an additional --output-file /share/schema.txt argument, and the file will be created in your local directory. schemacrawler help will give you more information, as well as the SchemaCrawler website. Now you are ready to connect to your own database. If you need help on how to construct the connection URL, take a look at ing-bank/cassandra-jdbc-wrapper.

version: '3.7'
services:
cassandra:
image: cassandra
container_name: cassandra
ports:
- target: 9042
published: 9042
protocol: tcp
mode: host
healthcheck:
test: ["CMD-SHELL", "[ $$(nodetool statusgossip) = running ]"]
interval: 30s
timeout: 10s
retries: 5
volumes:
- type: bind
source: ./
target: /share
-- https://cassandra.apache.org/_/quickstart.html
-- Create a keyspace
CREATE KEYSPACE IF NOT EXISTS store WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : '1' };
-- Create a table
CREATE TABLE IF NOT EXISTS store.shopping_cart (
userid text PRIMARY KEY,
item_count int,
last_update_timestamp timestamp
);
-- Insert some data
INSERT INTO store.shopping_cart
(userid, item_count, last_update_timestamp)
VALUES ('9876', 2, toTimeStamp(now()));
INSERT INTO store.shopping_cart
(userid, item_count, last_update_timestamp)
VALUES ('1234', 5, toTimeStamp(now()));
version: '3.7'
services:
schemacrawler:
image: schemacrawler/schemacrawler
container_name: schemacrawler
stdin_open: true
tty: true
volumes:
- type: bind
source: ./
target: /home/schcrwlr/share
entrypoint: /bin/bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment