I want to create a simple cassandra db , insert some data and print all the data in the vector db
This is the structure of my exercise:
/cassandra_study
├── docker-compose.yml
├── Dockerfile
└── app.py
And that are code of the all the scripts:
Docker-compose.yml
services:
app:
build:
context: .
dockerfile: ./Dockerfile
volumes:
- .:/app
container_name: app-container
depends_on:
database:
condition: service_healthy
database:
image: cassandra:latest
ports:
- 9042:9042
container_name: cassandra-study-container
healthcheck:
test: ["CMD", "cqlsh", "-e", "descripe keyspace"]
interval: 30s
timeout: 10s
retries: 5
Dockerfile
FROM python:3.10-slim
WORKDIR /app
RUN pip install cassandra-driver
COPY . /app
CMD ["python", "app.py"]
In this script i create the keyspace, the table and insert and print some random data. App.py
from cassandra.cluster import Cluster
from cassandra.query import SimpleStatement
def create_keyspace_and_table(session):
keyspace_query = """
CREATE KEYSPACE IF NOT EXISTS store WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}
"""
session.execute(keyspace_query)
session.set_keyspace('store')
create_table = """
CREATE TABLE IF NOT EXISTS store.mytable (
id UUID PRIMARY KEY,
message TEXT
)
"""
session.execute(create_table)
def insert_data(session, message):
insert_query = """
INSERT INTO store.mytable (id, message) VALUES (uuid(), %s)
"""
session.execute(insert_query, (message))
def print_data(session):
select_query = "SELECT * FROM store.mytable"
rows = session.execute(select_query)
for row in rows:
print(f"ID: {row.id}, Message: {row.mesage}")
if __name__ == "__main__":
cluster = Cluster(['database'])
session = cluster.connect()
try:
create_keyspace_and_table(session)
data = [
("Ciao sono il primo"),
("Ciao sono il secondo"),
("Ciao sono il terzo")
]
for message in data:
insert_data(session, message)
print_data(session)
finally:
cluster.shutdown()
When i try to run the code:
The error that give me when i do docker compose up is this:
dependency failed to start: container cassandra-study-container is unhealthy
i think this error come out, because the container is starting up before the database is fully ready. Someone can give me advice how to resolve this problem
Looking on internet and doing some debug
2
Answers
I have change the healtcheck with this:
And now it runs , you need to attend a lot of time before the db goes up.
You can add a dependency on your docker-compose.yml that waits for cassandra to run.