How TO Create Cassandra Cluster

IP 192.168.122.71   node1.example.com  
IP 192.168.122.244  node2.example.com  
IP 192.168.122.32   node3.example.com  


# cat  /etc/sysconfig/selinux ( All Nodes )

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

# systemctl stop firewalld   ( All Nodes )
# systemctl disable firewalld  ( All Nodes ) 

# vim /etc/yum.repos.d/cassandra.repo  ( All Nodes )
    [cassandra]  
    name=Apache Cassandra  
    baseurl=https://www.apache.org/dist/cassandra/redhat/311x/  
    gpgcheck=1  
    repo_gpgcheck=1  
    gpgkey=https://www.apache.org/dist/cassandra/KEYS  

# yum install cassandra -y  ( All Nodes )

# vim /etc/cassandra/conf/cassandra.yaml  ( All Nodes )

cluster_name: 'test Cluster'
num_tokens: 256
seed_provider:
    - class_name: org.apache.cassandra.locator.SimpleSeedProvider
        - seeds: "192.168.122.71, 192.168.122.155, 192.168.122.32"
listen_address: 192.168.122.71  ( Change The IP As per Node )
rpc_address: 192.168.122.71     ( Change The IP As per Node )
endpoint_snitch: GossipingPropertyFileSnitch

# vim /etc/cassandra/conf/cassandra-rackdc.properties  ( All Nodes )
dc=dc1
rack=rack1

# systemctl restart  cassandra
# /sbin/chkconfig cassandra on


# nodetool status
Datacenter: dc1
===============
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address          Load       Tokens       Owns (effective)  Host ID                               Rack
UN  192.168.122.244  36.33 KiB  256          65.0%             f4949327-3d5b-45c3-a745-cff67d677aaa  rack1
UN  192.168.122.32   70.88 KiB  256          66.0%             940413db-da5e-478b-a384-8da7b078b662  rack1
UN  192.168.122.71   70.9 KiB   256          69.0%             14e27820-a2f5-48b6-a5c5-5fb71c4e4fad  rack1

# cqlsh
[cqlsh 5.0.1 | Cassandra 3.11.13 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh>

How to Create Keyspace
cqlsh> create keyspace test with replication ={'class':'SimpleStrategy','replication_factor':3};
cqlsh> create keyspace student with replication ={'class':'SimpleStrategy','replication_factor':3};

How to Create Table
cqlsh> CREATE TABLE student.data (name text, dob  text, doj text, ma text, email text, remark text, PRIMARY KEY(name));
cqlsh> CREATE TABLE test.raju (id text, addr text, mobile text, name text, pin text, PRIMARY KEY (id));


# cat test.csv ( Create CSV )
"Rajeev"|"01-10-1984"|"01-10-2015"|"rajeev@yahoo.com"|"KO"|"this is test"
"Raju"|"01-10-1984"|"01-10-2016"|"raju@gmail.com"|"KO"|"this is test"
"Rahul"|"01-10-1986"|"01-10-2017"|"rahul@hotmail.com"|"KO"|"this is test"
"Reema"|"01-10-1986"|"01-10-2018"|"reema@indigo.in"|"KO"|"this is test"
"Kavita"|"01-10-1988"|"01-10-2019"|"kavita@tcs.com"|"KO"|"this is test"
"Samay"|"01-10-1886"|"01-10-2020"|"samay@nic.com"|"KO"|"this is test"
"Tashu"|"01-10-1887"|"01-10-2021"|"tashu@games.com"|"KO"|"this is test"
"Anshu"|"01-10-1888"|"01-10-2022"|"Anshu@freefire.com"|"KO"|"this is test"

How to insert data from Csv in Table
cqlsh> COPY student.data FROM 'test.csv' WITH  DELIMITER='|' AND HEADER=TRUE;

How to insert data in Table
cqlsh> INSERT INTO test.raju (name , dob , doj , email , ma , remark ) VALUES ( 'sumit','10-10-1990','01-12-2007','sumit@gmail.com','No','this is test');

How to update data
cqlsh> UPDATE student.data SET ma='OK' WHERE name='sumit';

How to delete Row in Table
cqlsh> DELETE from student.data WHERE name='sumit';

How to Check date in table
cqlsh> select * from Keyspace.table;


How to Backup and Restore keyspace data
1. take snapshot 
# nodetool  snapshot keyspace_name

2. take database structure
cqlsh 192.168.122.244 -e 'desc student' > student.cql
 
3. remove all the data from cassendra and restart cluster.
# cp -r /var/lib/cassandra /backup_folder
# systemctl stop cassandra.service
# rm -rf /var/lib/cassandra/
# chown -R  cassandra: /var/lib/cassandra/
# systemctl restart  cassandra.service


How to Restore keyspace data
1. create keyspace and table
# cqlsh 192.168.122.244
cqlsh> SOURCE student.cql;

2. copy snapshot in data Dir. 
# cp  /backup_folder/cassandra/data/student/data-e10b0140f3ad11edb94fb30522b6910a/snapshots/1684217874710/m* /var/lib/cassandra/data/student/data-f9a3a3a0f3b311eda16b7313798baeaf/
# cp  /backup_folder/cassandra/data/student/data-e10b0140f3ad11edb94fb30522b6910a/snapshots/1684217874710/schema.cql /var/lib/cassandra/data/student/data-f9a3a3a0f3b311eda16b7313798baeaf/

3. Refresh the keyspace without restart cassendra. 
# nodetool refresh student data

4. verify the data; 
# cqlsh 192.168.122.244
cqlsh> SELECT * FROM student.data ;

Comments

Popular posts from this blog

How to install and configure node js and PM2 in rhel7

PCS Corosync Pacemaker Cluster Mariadb using NFS

How to Create or Configure iSCSI Server and Clinet