Tuesday, 11 June 2019

Cassandra Database Setup

Cassandra Database Setup

Cassandra DB can be set up easily by using the docker image. If you do not have docker installed in your machine, please do it and follow the steps.

 #Pull the docker image
docker pull cassandra
 #Run the docker image in a container
docker run -p 9042:9042 --name my-cassandra -d cassandra:latest

 #Stop the docker container
docker stop my-cassandra
 #Start the docker container
docker start my-cassandra
 #Stop & remove the container
docker stop my-cassandra
docker rm my-cassandra

The docker exec command allows you to run commands inside a Docker container. The following command line will give you a bash shell inside your cassandra container.

docker exec -it my-cassandra bash  

Now you your inside the container. Type cqlsh which will allow you enter into the cassandra query language shell Where you can execute the below queries.

 #This will list the available keyspaces
describe keyspaces;

 #Create a new namespace with the name arun_keyspace.
CREATE KEYSPACE arun_keyspace
WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};

 #If list the keyspaces again, you can see newly created keyspace arun_keyspace
describe keyspaces;
 #Command to Use the keyspace to create/update & manage tables
use arun_keyspace;


 #Create a new table, products
create table products (
          productid uuid ,
          productname text,
          productdescription text,
          PRIMARY KEY(productid)
      );

 #Insert
insert into products(productid, productname, productdescription)
values (now() , 'Fitbit' , 'Fitbit versa');

 #Select the table
select * from products;


No comments:

Post a Comment