Quantcast
Channel: Document Store – Database Research & Development

NoSQL: Cassandra Automatic Data Expiration using Time to Live (ttl)

$
0
0

Cassandra Automatic Data Expiration:

The Automatic Data Expiration is one of the feature of NoSQL Cassandra Database.
You can set expiration time in second to expire a particular data.
You can set Time to Live (ttl) value in seconds to set an expiration time.

The defined TTL data are marked with a Tombstone Garbage collector parameter (deletion marker) and after that data automatically removed during the compaction process.

You can change TTL value by re-insert the data with new TTL value.
The actual insertion is kind of INSERT or UPDATE because it checks the previous data version and accordingly it performs the action.
You can set the TTL value in both INSERT and UPDATE statements.

Automation Expiration of The Data, real time application use like, any e-commerce product is declaring different offers on products and each offer should expire after some time intervals.
In this kind of application, we can use the feature like the automation expiration of the Cassandra.

Small demonstration:

Create a sample table:

CREATE TABLE IF NOT EXISTS tbl_Employee
(
  EmpID INT PRIMARY KEY
  ,EmpFirstName VARCHAR
  ,EmpLastName VARCHAR
  ,EmpSalary INT
);

Insert one row using TTL for 20 second:
After 20 second, data automatically will be removed.

INSERT INTO tbl_Employee
(EmpID,EmpFirstName,EmpLastName,EmpSalary)
VALUES
(1,'Anvesh','Patel',50000) USING TTL 20;

The Result:

NoSQL Cassandra Time To Live TTL


NoSQL: Cassandra Collection Data Types – List, Set, Map

$
0
0

In this post, I am sharing small demonstration of Cassandra Collection data types.

Basically, CQL provides three different types of collection data types, SET, LIST, MAP.
Using collection data types, you can store multiple values in a single column.

You can store multiple values in a single column, but try to keep your collection small otherwise it creates performance overhead.

Small demonstration of Collection data types:

Create a sample table with collectio data type
(SET, LIST, MAP):

CREATE TABLE IF NOT EXISTS tbl_Employee
(
    EmpID INT PRIMARY KEY
    ,EmpName VARCHAR
    ,Emails SET<text>
    ,Hobbies LIST<text>
    ,Address MAP<text,text>
) ;

Insert a sample record:

INSERT INTO tbl_Employee
(EmpID,EmpName,Emails,Hobbies,Address)
VALUES
(
    1
    ,'Anvesh'
    ,{'test1@gmail.com','test2@gmail.com'}
    ,['Blogging','Animation','Photography']
    ,{'home': 'Gujarat', 'office': 'Hyderabad'}
);

Update data on Collection type:

UPDATE tbl_Employee SET Emails = Emails + {'test3@gmail.com'} WHERE EmpID=1;
UPDATE tbl_Employee SET Hobbies = Hobbies + ['Watching Movies'] WHERE EmpID=1;
UPDATE tbl_Employee SET Address = Address + {'farm':'Bhavnagar'} WHERE EmpID=1;

Filter on Collection type:

SELECT * FROM tbl_Employee WHERE Address CONTAINS 'Gujarat' ALLOW FILTERING;
SELECT * FROM tbl_Employee WHERE Hobbies CONTAINS 'Blogging' ALLOW FILTERING;
SELECT * FROM tbl_Employee WHERE Emails CONTAINS 'test1@gmail.com' ALLOW FILTERING;

NoSQL: To Locate and Edit cassandra.yaml Configuration File of Cassandra

$
0
0

What is cassandra.yaml configuration file?

The cassandra.yaml file is the main configuration file for Cassandra. This file is very important for all kinds of Cassandra node and cluster level configurations.
After changing properties in the cassandra.yaml file, you must restart the node for the changes to take effect.

The default location of cassandra.yaml:

The default location in linux is: /etc/cassandra.
You can open this file with admin (sudo) user and can edit this.

Basic commands to locate this file:

-- to list out all folders and files
$ ls -a

-- change directory
$ cd /etc
$ cd cassandra

Cassandra Yaml File Location

Basic commands to open this file:

$ sudo nano cassandra.yaml

Cassandra Yaml File Configuration

After edit, Restart the cassandra service:

-- restart service
$ sudo service cassandra restart
-- after restart check the service status
$ sudo service cassandra status

NoSQL: “org.apache.cassandra.auth.CassandraRoleManager doesn’t support PASSWORD”

$
0
0

When I was creating my first database User and Role, I got this error message in Cassandra – InvalidRequest: code=2200 [Invalid query] message=”org.apache.cassandra.auth.CassandraRoleManager doesn’t support PASSWORD”.

For me, this is very important to resolve this problem because application requires different database User authentication information.

I have tried many options to solve this error and I have found that cassandra.yaml file require to edit.
It has one authenticator parameter which has by default allow all types of authentication.

You can find this parameter and can change as mentioned below steps.

Old value:

authenticator: AllowAllAuthenticator

Change with this new value:

authenticator: PasswordAuthenticator

Restart your Cassandra service:

$ sudo service cassandra restart

Login with default admin Cassandra user:

$ cqlsh -u cassandra -p cassandra

Additionally, you can also check role manager parameter is exists or not. If below mention parameter is not exist in cassandra.yaml, you can add it.

role_manager: CassandraRoleManager

NoSQL: Cassandra introduces Role Based Authentication

$
0
0

Prior to Cassandra 2.2, It supports only User base authentication, but now Cassandra introduces new Role based authentication.

In this post, I am sharing small demonstration to create a Role in Cassandra and How to assign different permissions to that role.
You can directly use this Role for authentication and you can also assign same Role permission to another role.

First, login with Cassandra super user.

cqlsh -u cassanddra -p cassandra

Create keyspace if not exists:

CREATE KEYSPACE IF NOT EXISTS dbrnd WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};

USE dbrnd;

Create few sample tables:

CREATE TABLE IF NOT EXISTS tbl_Employee
(
	EmpID INT PRIMARY KEY
	,EmpFirstName VARCHAR
	,EmpLastName VARCHAR
	,EmpSalary INT
);

CREATE TABLE IF NOT EXISTS tbl_Department
(
	DeptID INT PRIMARY KEY
	,DeptName VARCHAR
);

Create one sample Role without Superuser access:
If you get this error : “org.apache.cassandra.auth.CassandraRoleManager doesn’t support PASSWORD”, visit this solution.

CREATE ROLE ReportingRole WITH PASSWORD = 'pass123' AND LOGIN = true AND SUPERUSER = false;

Grant different permissions to this Role:

GRANT SELECT ON dbrnd.tbl_Employee TO ReportingRole;
GRANT MODIFY ON dbrnd.tbl_Department TO ReportingRole;

If you get any error like ” GRANT operation is not supported by AllowAllAuthorizer”, you have to modify the value of “authorizer” in cassandra.yaml file.

-- Old value:
 authorizer: AllowAllAuthorizer

 -- Change to this New value:
 authorizer: org.apache.cassandra.auth.CassandraAuthorizer

Grant over the all keyspaces:

GRANT SELECT ON ALL KEYSPACES TO ReportingRole;
GRANT MODIFY ON ALL KEYSPACES TO ReportingRole;

Create new test Role and make it a replica of the current role:

CREATE ROLE Test WITH PASSWORD = 'pass123' AND LOGIN = true AND SUPERUSER = false;

GRANT ReportingRole to Test;

List out all Roles and its permissions:

-- List out all Roles.
LIST ROLES;
-- List out all permissions of the Role.
LIST ALL PERMISSIONS OF ReportingRole;

Cassandra User Role

NoSQL: Cassandra default list of port usage

$
0
0

In this post, I am sharing an important list of ports which are used by the Cassandra for different purpose.
Cassandra community has to know about these ports because it is very important while they are configuring firewall for Cassandra server.

9160: Cassandra Thrift client API
9042: Cassandra native client port

7000: Cassandra Internode cluster communication (not used if TLS enabled)
7001: Cassandra SSL Internode cluster communication.
7199 / 8080: JMX monitoring port

61620: Cassandra OpsCenter monitoring port
61621: Cassandra OpsCenter agent port

8888: OpsCenter browser http request

NoSQL: Cassandra Important CQL shell commands

$
0
0

In this post, I am sharing a list of important CQL shell commands of the Cassandra.

Get help of all commands:

cassandra@cqlsh:dbrnd> help

Check host and version:

cassandra@cqlsh:dbrnd> show host
Connected to Test Cluster at 127.0.0.1:9042.
cassandra@cqlsh:dbrnd> show version
[cqlsh 5.0.1 | Cassandra 3.5 | CQL spec 3.4.0 | Native protocol v4]

Check cluster information:

cassandra@cqlsh:dbrnd> describe cluster;

Cluster: Test Cluster
Partitioner: Murmur3Partitioner

Range ownership:
                     7198801157164107876  [127.0.0.1]
                    -6388269565477727734  [127.0.0.1]
                      -13424558412243369  [127.0.0.1]

Check keyspaces:

cassandra@cqlsh:dbrnd> describe keyspaces;

tutorialspoint  system_auth  dbrnd               test
system_schema   system       system_distributed  system_traces

Check user defined tables:

cassandra@cqlsh:dbrnd> describe tables;

tbl_employee  tbl_department  users

Check user defined types:

cassandra@cqlsh:dbrnd> describe types

sports_details sport

Check consistency level:

cassandra@cqlsh:dbrnd> consistency
Current consistency level is ONE.

Make your table output expanded:

cassandra@cqlsh:dbrnd> expand on
Now Expanded output is enabled
cassandra@cqlsh:dbrnd> select *from tbl_employee;

@ Row 1
--------------+--------
 empid        | 1
 empfirstname | Anvesh
 emplastname  | Patel
 empsalary    | 50000

@ Row 2
--------------+--------
 empid        | 2
 empfirstname | Neevan
 emplastname  | Patel
 empsalary    | 150000

(2 rows)

NoSQL: Important System Tables of the Cassandra

$
0
0

In this post, I am sharing an important list of Cassandra System Tables.

Cassandra DBA can use these tables for a different investigation like, Schema information, Roles information, Tracing information and others.

There are different keyspaces are available like, system, system_auth, system_schema, system_traces.

system_auth Keyspace:

SELECT *FROM system_auth.roles;
SELECT *FROM system_auth.role_permissions;
system_schema Keyspace:
SELECT *FROM system_schema.views;
SELECT *FROM system_schema.types;
SELECT *FROM system_schema.triggers;
SELECT *FROM system_schema.tables;
SELECT *FROM system_schema.keyspaces;
SELECT *FROM system_schema.indexes;
SELECT *FROM system_schema.functions;
SELECT *FROM system_schema.columns;
SELECT *FROM system_schema.dropped_columns;
SELECT *FROM system_schema.aggregates;
system_traces Keyspace:
SELECT *FROM system_traces.sessions;
SELECT *FROM system_traces.events;
system Keyspace:
SELECT *FROM system.available_ranges;
SELECT *FROM system.peers;
SELECT *FROM system.peer_events;
SELECT *FROM system.batchlog;
SELECT *FROM system.batches;

NoSQL: Cassandra Batch to execute multiple Statements

$
0
0

In this post, I am sharing basic help to execute multiple statements using one single Cassandra batch.

Many times, it is required to execute multiple statements using single batch.
Cassandra CQL provides the BEGIN BATCH…APPLY BATCH command to execute multiple statements.

Below is a small demonstration:

Create Keyspace if not exists:

CREATE KEYSPACE IF NOT EXISTS dbrnd WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
Use a Keyspace:
USE dbrnd;
Create a Sample Table:
CREATE TABLE IF NOT EXISTS tbl_Employee
(
  EmpID INT PRIMARY KEY
  ,EmpFirstName VARCHAR
  ,EmpLastName VARCHAR
  ,EmpSalary INT
);
Insert few sample records:
INSERT INTO tbl_Employee
(EmpID,EmpFirstName,EmpLastName,EmpSalary)
VALUES
(1,'Anvesh','Patel',50000);

INSERT INTO tbl_Employee
(EmpID,EmpFirstName,EmpLastName,EmpSalary)
VALUES
(2,'Neevan','Patel',150000);
Execute INSERT, UPDATE, DELETE using one Single Batch command:
cassandra@cqlsh:dbrnd>
    begin batch
         INSERT INTO tbl_Employee
         (EmpID,EmpFirstName,EmpLastName,EmpSalary)
         VALUES
         (3,'Meera','Patel',80000);
         UPDATE tbl_Employee SET EmpFirstName ='Martin' WHERE EmpID=2;

         DELETE FROM tbl_Employee WHERE EmpID=3;
    apply batch;
Check the result:
cassandra@cqlsh:dbrnd> SELECT *FROM tbl_Employee;

@ Row 1
--------------+--------
 empid        | 1
 empfirstname | Anvesh
 emplastname  | Patel
 empsalary    | 50000

@ Row 2
--------------+--------
 empid        | 2
 empfirstname | Martin
 emplastname  | Patel
 empsalary    | 150000

(2 rows)

NoSQL: Create user defined type in Cassandra

$
0
0

In this post, I am sharing small demonstration to create CQL User Defined Data Type in Cassandra.

Using user defined data types, you can handle multiple fields into one user-defined column.
For example, You can create user-defined data type for Address related columns, in which you can specify a Street name, City name, Pincode, etc.

Create Keyspace if not exists:

CREATE KEYSPACE IF NOT EXISTS dbrnd WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};

Use a Keyspace:

USE dbrnd;

Create user defined data type:

CREATE TYPE dbrnd.EmployeeAddress
(
    StreetName TEXT
    ,CityName TEXT
    ,ZipCode INT
);

Create sample table:
frozen is require to define a column.

CREATE TABLE dbrnd.tbl_EmployeeInfo
(
    EmpID INT PRIMARY KEY
    ,EmpName VARCHAR
    ,EmpAddress frozen<EmployeeAddress>
);

Insert sample records:

INSERT INTO dbrnd.tbl_EmployeeInfo
(EmpID,EmpName,EmpAddress)
VALUES
(1,'Anvesh',{StreetName:'Kondapuar', CityName:'Hyderabad', ZipCode:580084});

INSERT INTO dbrnd.tbl_EmployeeInfo
(EmpID,EmpName,EmpAddress)
VALUES
(2,'Neevan',{StreetName:'Hilldrive', CityName:'Bhavnagar', ZipCode:364002});

Select properties of User defined data type column:

cassandra@cqlsh:dbrnd> SELECT EmpAddress.StreetName FROM dbrnd.tbl_EmployeeInfo;

@ Row 1
-----------------------+-----------
 empaddress.streetname | Kondapuar

@ Row 2
-----------------------+-----------
 empaddress.streetname | Hilldrive

(2 rows)




Latest Images