Sunday, July 17, 2022

Apache Cassandra with Docker

For this post I get steps  from an official documentation by Apache Cassandra check the link. 

1. Install Apache Cassandra.

docker pull cassandra:latest

2. A Docker network allows us to access the container’s ports without exposing them on the host.

docker network create cassandra

docker run --rm -d --name cassandra --hostname cassandra --network cassandra cassandra

3. Create a file named data.cql. This script will create a keyspace, the layer at which Cassandra replicates its data, a table to hold the data, and insert some data into that table:

-- Create a keyspace
CREATE KEYSPACE IF NOT EXISTS test_keyspace WITH REPLICATION={ 'class' : 'SimpleStrategy', 'replication_factor' : '1' };

-- Create a table
CREATE TABLE IF NOT EXISTS test_keyspace.users (
    email varchar primary key,
    name varchar,
    age int
);

-- Insert some data
INSERT INTO test_keyspace.users
(email, name, age)
VALUES ('test@gmail.com', 'henry', 30);

INSERT INTO test_keyspace.users
(email, name, age)
VALUES ('test2@gmail.com', 'henry123', 30);


4. Load date with CQLSH

docker run --rm --network cassandra -v "YOUR_PATH/data.cql:/scripts/data.cql" 
-e CQLSH_HOST=cassandra -e CQLSH_PORT=9042 -e CQLVERSION=3.4.5 nuvo/docker-cqlsh

5. Interactive CQLSH

docker run --rm -it --network cassandra nuvo/docker-cqlsh cqlsh cassandra 9042 --cqlversion='3.4.5'

6.  Read some  data.

SELECT * FROM test_keyspace.users;











7. Clean Up

docker kill cassandra
docker network rm cassandra









References: https://cassandra.apache.org/_/quickstart.html






No comments:

Post a Comment

Virtual Threads in Java 21: Simplified Concurrency for Modern Applications

  With Java 21, Virtual Threads have redefined how we approach concurrency, offering a lightweight and efficient way to handle parallel and ...