Thursday, May 14, 2020

How can I restore Cassandra snapshots?

Step-1: I created one table by using the below command
CREATE TABLE Cricket (
    PlayerID uuid,
    LastName varchar,
    FirstName varchar,  
    City varchar,
    State varchar,
    PRIMARY KEY (PlayerID));
Step-2: Insert 3 records by using below command
INSERT INTO Cricket (PlayerID, LastName, FirstName, City, State)
VALUES (now(), 'Pendulkar', 'Sachin', 'Mumbai','Maharastra');
INSERT INTO Cricket (PlayerID, LastName, FirstName, City, State)
VALUES (now(), 'Vholi', 'Virat', 'Delhi','New Delhi');
INSERT INTO Cricket (PlayerID, LastName, FirstName, City, State)
VALUES (now(), 'Sharma', 'Rohit', 'Berhampur','Odisha');
Step-3: Accidentally I deleted Cricket table
drop table Cricket;
Step-4: Need to recover that table by using auto snapshotbackup Note: auto_snapshot (Default: true ) Enable or disable whether a snapshot is taken of the data before keyspace truncation or dropping of tables. To prevent data loss, using the default setting is strongly advised.
Step-5: Find the snapshot locations and files
cassandra@node1:~/data/students_details$ cd cricket-88128dc0960d11ea947b39646348bb4f
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f$ ls -lrth
total 0
drwxrwxr-x 2 cassandra cassandra  6 May 14 18:05 backups
drwxrwxr-x 3 cassandra cassandra 43 May 14 18:06 snapshots
Step-6: You will get one .cql file in that snapshot location which having tables DDL.
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$ ls -lrth
total 44K
-rw-rw-r-- 1 cassandra cassandra   92 May 14 18:06 md-1-big-Summary.db
-rw-rw-r-- 1 cassandra cassandra   61 May 14 18:06 md-1-big-Index.db
-rw-rw-r-- 1 cassandra cassandra   16 May 14 18:06 md-1-big-Filter.db
-rw-rw-r-- 1 cassandra cassandra  179 May 14 18:06 md-1-big-Data.db
-rw-rw-r-- 1 cassandra cassandra   92 May 14 18:06 md-1-big-TOC.txt
-rw-rw-r-- 1 cassandra cassandra 4.7K May 14 18:06 md-1-big-Statistics.db
-rw-rw-r-- 1 cassandra cassandra    9 May 14 18:06 md-1-big-Digest.crc32
-rw-rw-r-- 1 cassandra cassandra   43 May 14 18:06 md-1-big-CompressionInfo.db
-rw-rw-r-- 1 cassandra cassandra  891 May 14 18:06 schema.cql
-rw-rw-r-- 1 cassandra cassandra   31 May 14 18:06 manifest.json
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$

more schema.cql
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$ more schema.cql
CREATE TABLE IF NOT EXISTS students_details.cricket (
        playerid uuid PRIMARY KEY,
        city text,
        firstname text,
        lastname text,
        state text)
        WITH ID = 88128dc0-960d-11ea-947b-39646348bb4f
        AND bloom_filter_fp_chance = 0.01
        AND dclocal_read_repair_chance = 0.1
        AND crc_check_chance = 1.0
        AND default_time_to_live = 0
        AND gc_grace_seconds = 864000
        AND min_index_interval = 128
        AND max_index_interval = 2048
        AND memtable_flush_period_in_ms = 0
        AND read_repair_chance = 0.0
        AND speculative_retry = '99PERCENTILE'
        AND comment = ''
        AND caching = { 'keys': 'ALL', 'rows_per_partition': 'NONE' }
        AND compaction = { 'max_threshold': '32', 'min_threshold': '4', 'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy' }
        AND compression = { 'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor' }
        AND cdc = false
        AND extensions = {  };
Step-7: Login to the database and create table using that DDL.
    apiadmin@cqlsh:coopersdev> use students_details;
    apiadmin@cqlsh:students_details> CREATE TABLE IF NOT EXISTS students_details.cricket (
                        ...         playerid uuid PRIMARY KEY,
                        ...         city text,
                        ...         firstname text,
                        ...         lastname text,
                        ...         state text)
                        ...         WITH ID = 88128dc0-960d-11ea-947b-39646348bb4f
                        ...         AND bloom_filter_fp_chance = 0.01
                        ...         AND dclocal_read_repair_chance = 0.1
                        ...         AND crc_check_chance = 1.0
                        ...         AND default_time_to_live = 0
                        ...         AND gc_grace_seconds = 864000
                        ...         AND min_index_interval = 128
                        ...         AND max_index_interval = 2048
                        ...         AND memtable_flush_period_in_ms = 0
                        ...         AND read_repair_chance = 0.0
                        ...         AND speculative_retry = '99PERCENTILE'
                        ...         AND comment = ''
                        ...         AND caching = { 'keys': 'ALL', 'rows_per_partition': 'NONE' }
                        ...         AND compaction = { 'max_threshold': '32', 'min_threshold': '4', 'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy' }
                        ...         AND compression = { 'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor' }
                        ...         AND cdc = false
                        ...         AND extensions = {  };
apiadmin@cqlsh:students_details>
Step-8: copy all the files on snapshot folder to existing cricket table folder
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$ pwd
/home/cassandra/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$ cp * /home/cassandra/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$ cd /home/cassandra/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f$ ls -lrth
total 44K
drwxrwxr-x 2 cassandra cassandra    6 May 14 18:05 backups
drwxrwxr-x 3 cassandra cassandra   43 May 14 18:06 snapshots
-rw-rw-r-- 1 cassandra cassandra  891 May 14 18:11 schema.cql
-rw-rw-r-- 1 cassandra cassandra   92 May 14 18:11 md-1-big-TOC.txt
-rw-rw-r-- 1 cassandra cassandra   92 May 14 18:11 md-1-big-Summary.db
-rw-rw-r-- 1 cassandra cassandra 4.7K May 14 18:11 md-1-big-Statistics.db
-rw-rw-r-- 1 cassandra cassandra   61 May 14 18:11 md-1-big-Index.db
-rw-rw-r-- 1 cassandra cassandra   16 May 14 18:11 md-1-big-Filter.db
-rw-rw-r-- 1 cassandra cassandra    9 May 14 18:11 md-1-big-Digest.crc32
-rw-rw-r-- 1 cassandra cassandra  179 May 14 18:11 md-1-big-Data.db
-rw-rw-r-- 1 cassandra cassandra   43 May 14 18:11 md-1-big-CompressionInfo.db
-rw-rw-r-- 1 cassandra cassandra   31 May 14 18:11 manifest.json
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f$
Step-9: start restore table data using sstableloader by using below command
cassandra@node1:~$ sstableloader -d 10.213.61.21 -username cassandra --password cassandra /home/cassandra/data/students_details/cricket-d3576f60960f11ea947b39646348bb4f/snapshots
    Established connection to initial hosts
    Opening sstables and calculating sections to stream

    Summary statistics:
       Connections per host    : 1
       Total files transferred : 0
       Total bytes transferred : 0.000KiB
       Total duration          : 2920 ms
       Average transfer rate   : 0.000KiB/s
       Peak transfer rate      : 0.000KiB/s
Step-10: The table restored successfully. Please verify.
 playerid                             | city      | firstname | lastname  | state
--------------------------------------+-----------+-----------+-----------+------------
 d7b12c90-960f-11ea-947b-39646348bb4f | Berhampur |     Rohit |    Sharma |     Odisha
 d7594890-960f-11ea-947b-39646348bb4f |     Delhi |     Virat |     Vholi |  New Delhi
 d7588540-960f-11ea-947b-39646348bb4f |    Mumbai |    Sachin | Pendulkar | Maharastra

Restore cassandra cluster data when accidentally drop table.

Step-1: I created one table by using the below command
CREATE TABLE Cricket (
    PlayerID uuid,
    LastName varchar,
    FirstName varchar,  
    City varchar,
    State varchar,
    PRIMARY KEY (PlayerID));
Step-2: Insert 3 records by using below command
INSERT INTO Cricket (PlayerID, LastName, FirstName, City, State)
VALUES (now(), 'Pendulkar', 'Sachin', 'Mumbai','Maharastra');
INSERT INTO Cricket (PlayerID, LastName, FirstName, City, State)
VALUES (now(), 'Vholi', 'Virat', 'Delhi','New Delhi');
INSERT INTO Cricket (PlayerID, LastName, FirstName, City, State)
VALUES (now(), 'Sharma', 'Rohit', 'Berhampur','Odisha');
Step-3: Accidentally I deleted Cricket table
drop table Cricket;
Step-4: Need to recover that table by using auto snapshotbackup Note: auto_snapshot (Default: true ) Enable or disable whether a snapshot is taken of the data before keyspace truncation or dropping of tables. To prevent data loss, using the default setting is strongly advised.
Step-5: Find the snapshot locations and files
cassandra@node1:~/data/students_details$ cd cricket-88128dc0960d11ea947b39646348bb4f
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f$ ls -lrth
total 0
drwxrwxr-x 2 cassandra cassandra  6 May 14 18:05 backups
drwxrwxr-x 3 cassandra cassandra 43 May 14 18:06 snapshots
Step-6: You will get one .cql file in that snapshot location which having tables DDL.
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$ ls -lrth
total 44K
-rw-rw-r-- 1 cassandra cassandra   92 May 14 18:06 md-1-big-Summary.db
-rw-rw-r-- 1 cassandra cassandra   61 May 14 18:06 md-1-big-Index.db
-rw-rw-r-- 1 cassandra cassandra   16 May 14 18:06 md-1-big-Filter.db
-rw-rw-r-- 1 cassandra cassandra  179 May 14 18:06 md-1-big-Data.db
-rw-rw-r-- 1 cassandra cassandra   92 May 14 18:06 md-1-big-TOC.txt
-rw-rw-r-- 1 cassandra cassandra 4.7K May 14 18:06 md-1-big-Statistics.db
-rw-rw-r-- 1 cassandra cassandra    9 May 14 18:06 md-1-big-Digest.crc32
-rw-rw-r-- 1 cassandra cassandra   43 May 14 18:06 md-1-big-CompressionInfo.db
-rw-rw-r-- 1 cassandra cassandra  891 May 14 18:06 schema.cql
-rw-rw-r-- 1 cassandra cassandra   31 May 14 18:06 manifest.json
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$

more schema.cql
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$ more schema.cql
CREATE TABLE IF NOT EXISTS students_details.cricket (
        playerid uuid PRIMARY KEY,
        city text,
        firstname text,
        lastname text,
        state text)
        WITH ID = 88128dc0-960d-11ea-947b-39646348bb4f
        AND bloom_filter_fp_chance = 0.01
        AND dclocal_read_repair_chance = 0.1
        AND crc_check_chance = 1.0
        AND default_time_to_live = 0
        AND gc_grace_seconds = 864000
        AND min_index_interval = 128
        AND max_index_interval = 2048
        AND memtable_flush_period_in_ms = 0
        AND read_repair_chance = 0.0
        AND speculative_retry = '99PERCENTILE'
        AND comment = ''
        AND caching = { 'keys': 'ALL', 'rows_per_partition': 'NONE' }
        AND compaction = { 'max_threshold': '32', 'min_threshold': '4', 'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy' }
        AND compression = { 'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor' }
        AND cdc = false
        AND extensions = {  };
Step-7: Login to the database and create table using that DDL.
    apiadmin@cqlsh:coopersdev> use students_details;
    apiadmin@cqlsh:students_details> CREATE TABLE IF NOT EXISTS students_details.cricket (
                        ...         playerid uuid PRIMARY KEY,
                        ...         city text,
                        ...         firstname text,
                        ...         lastname text,
                        ...         state text)
                        ...         WITH ID = 88128dc0-960d-11ea-947b-39646348bb4f
                        ...         AND bloom_filter_fp_chance = 0.01
                        ...         AND dclocal_read_repair_chance = 0.1
                        ...         AND crc_check_chance = 1.0
                        ...         AND default_time_to_live = 0
                        ...         AND gc_grace_seconds = 864000
                        ...         AND min_index_interval = 128
                        ...         AND max_index_interval = 2048
                        ...         AND memtable_flush_period_in_ms = 0
                        ...         AND read_repair_chance = 0.0
                        ...         AND speculative_retry = '99PERCENTILE'
                        ...         AND comment = ''
                        ...         AND caching = { 'keys': 'ALL', 'rows_per_partition': 'NONE' }
                        ...         AND compaction = { 'max_threshold': '32', 'min_threshold': '4', 'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy' }
                        ...         AND compression = { 'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor' }
                        ...         AND cdc = false
                        ...         AND extensions = {  };
apiadmin@cqlsh:students_details>
Step-8: copy all the files on snapshot folder to existing cricket table folder
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$ pwd
/home/cassandra/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$ cp * /home/cassandra/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f/snapshots/dropped-1589479603749-cricket$ cd /home/cassandra/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f$ ls -lrth
total 44K
drwxrwxr-x 2 cassandra cassandra    6 May 14 18:05 backups
drwxrwxr-x 3 cassandra cassandra   43 May 14 18:06 snapshots
-rw-rw-r-- 1 cassandra cassandra  891 May 14 18:11 schema.cql
-rw-rw-r-- 1 cassandra cassandra   92 May 14 18:11 md-1-big-TOC.txt
-rw-rw-r-- 1 cassandra cassandra   92 May 14 18:11 md-1-big-Summary.db
-rw-rw-r-- 1 cassandra cassandra 4.7K May 14 18:11 md-1-big-Statistics.db
-rw-rw-r-- 1 cassandra cassandra   61 May 14 18:11 md-1-big-Index.db
-rw-rw-r-- 1 cassandra cassandra   16 May 14 18:11 md-1-big-Filter.db
-rw-rw-r-- 1 cassandra cassandra    9 May 14 18:11 md-1-big-Digest.crc32
-rw-rw-r-- 1 cassandra cassandra  179 May 14 18:11 md-1-big-Data.db
-rw-rw-r-- 1 cassandra cassandra   43 May 14 18:11 md-1-big-CompressionInfo.db
-rw-rw-r-- 1 cassandra cassandra   31 May 14 18:11 manifest.json
cassandra@node1:~/data/students_details/cricket-88128dc0960d11ea947b39646348bb4f$
Step-9: start restore table data using sstableloader by using below command
cassandra@node1:~$ sstableloader -d 10.213.61.21 -username cassandra --password cassandra /home/cassandra/data/students_details/cricket-d3576f60960f11ea947b39646348bb4f/snapshots
    Established connection to initial hosts
    Opening sstables and calculating sections to stream

    Summary statistics:
       Connections per host    : 1
       Total files transferred : 0
       Total bytes transferred : 0.000KiB
       Total duration          : 2920 ms
       Average transfer rate   : 0.000KiB/s
       Peak transfer rate      : 0.000KiB/s
Step-10: The table restored successfully. Please verify.
 playerid                             | city      | firstname | lastname  | state
--------------------------------------+-----------+-----------+-----------+------------
 d7b12c90-960f-11ea-947b-39646348bb4f | Berhampur |     Rohit |    Sharma |     Odisha
 d7594890-960f-11ea-947b-39646348bb4f |     Delhi |     Virat |     Vholi |  New Delhi
 d7588540-960f-11ea-947b-39646348bb4f |    Mumbai |    Sachin | Pendulkar | Maharastra

Tuesday, October 15, 2019

How to check keyspace replication strategy and replication factor

By using below command you will get replication strategy and factor

cqlsh> SELECT * FROM system_schema.keyspaces;

 keyspace_name      | durable_writes | replication
--------------------+----------------+-------------------------------------------------------------------------------------
        system_auth |           True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '1'}
      system_schema |           True |                             {'class': 'org.apache.cassandra.locator.LocalStrategy'}
 system_distributed |           True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '3'}
             system |           True |                             {'class': 'org.apache.cassandra.locator.LocalStrategy'}
          reaper_db |           True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '3'}
      system_traces |           True | {'class': 'org.apache.cassandra.locator.SimpleStrategy', 'replication_factor': '2'}


How to get cassandra keyspace DDL

By Using below command you will get particular keyspace DDL

cqlsh> desc demodb

CREATE KEYSPACE demodb WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}  AND durable_writes = true;

CREATE TABLE demodb.reptab (
    personid int PRIMARY KEY,
    address text,
    city text,
    firstname text,
    lastname text
) WITH bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';

CREATE TABLE demodb.testload (
    personid int PRIMARY KEY,
    address text,
    city text,
    firstname text,
    lastname text
) WITH bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';



To get list of keyspaces in cassandra cluster

Question : How to get To get list of keyspaces in cassandra cluster ?

Answers: By Using below command you will get list of keyspaces in cassandra cluster.

cqlsh> desc keyspaces

reaper_db      system_auth  system_distributed
system_schema  system       system_traces

cqlsh> describe keyspaces

reaper_db      system_auth  system_distributed
system_schema  system       system_traces

Friday, May 10, 2019

How to unlock oracle user on 18c database ?

Question : How to unlock oracle user on 18c database ?
Answer :
Step-1:Use below query to get user account details
SQL>  select USERNAME,ACCOUNT_STATUS from dba_users where username='ABCD_DEV';

USERNAME             ACCOUNT_STATUS
-------------------- --------------------------------
ABCD_DEV       LOCKED(TIMED)
Step-2 Use this query user account unlock
SQL> alter user ABCD_DEV account unlock;

User altered.
Step-3 Check user account status using below query
SQL> select USERNAME,ACCOUNT_STATUS from dba_users where username='ABCD_DEV';

USERNAME             ACCOUNT_STATUS
-------------------- --------------------------------
ABCD_DEV       OPEN

How to lock oracle user on 18c database ?

Question : How to lock oracle user on 18c database ?
Answer :
Step-1:Use below query to get user account details
SQL> select USERNAME,ACCOUNT_STATUS from dba_users where username='ABCD_DEV';

USERNAME             ACCOUNT_STATUS
-------------------- --------------------------------
ABCD_DEV       OPEN
Step-2 Use this query user account lock
SQL> alter user ABCD_DEV account lock;

User altered.
Step-3 Check user account status using below query
SQL>  select USERNAME,ACCOUNT_STATUS from dba_users where username='ABCD_DEV';

USERNAME             ACCOUNT_STATUS
-------------------- --------------------------------
ABCD_DEV       LOCKED(TIMED)

How can I restore Cassandra snapshots?