Creating a database in Cassandra:
Note: In Cassandra, a Database is named Keyspace.
Syntax)
with replication={‘class’: ‘SimpleStrategy’, ‘replication_factor ‘:4};
with replication = {‘class’:’NetworkTopologyStrategy’ ‘dc1′:3,’dc2’:2};
Syntax: DESRIBE KEYSPACES;
Syntax)
Use KeySpacename;
Ex_ Use Demo1;
Syntax) DESRIBE TABLES;
Syntax) Create table tablename(col1 datatype, col2 datatype, primary key(col1));
Ex) Create table dummy(id int, name text, date_of_birth timestamp, primary key(id));
Syntax) DESCRIBE TABLENAME;
Syntax)
Ex)
Insert into dummy(id,name, date_of_birth) values(1, “abc”, “22-02-2002 02:20:20”)
Ex)
copy dummy( home_id,datatime,event, code_used) from ‘/home/destop/event.csv’ with header=true and delimiter = ‘,’;
In Cassandra the retrieval operation is performed using partition key.
Partition key is nothing but using which Cassandra stores a record at particular location.
By default partitioned key is created using columns in primary key.
Ex) select * from dummy where id=1;
But if we try to retrieve records using columns other than primary key columns in the where clause, then it will display an error.
In order to retrieve records using columns other than primary key we need to set the Secondary Index values for the respected columns using which we need to perform the retrieving operations with where clause.
Syntax) create INDEX indexname ON tablename(columnname);
Ex) create INDEX name_index ON dummy(name);
You don’t need to remember the Index name, it’s only for the purpose of allocating the database with permissions to retrieve records as per a particular column in a table by indexing it.
Now you can perform select query with where clause over columns other than the primary key.
Ex) select * from dummy where name=”xyz”;
Syntax) Update tablename set colname=””where id= ‘’;
Ex) Update dummy set name= “abx” where id=2;
Syntax) Update tablename set col1=””, col21=”” where id= ‘’;
Ex) Update tablename set name=”axc”,date=”2012-02-22 02:02:02″ where id= 6;
Deleting according to particular column:
Syntax) delete col1 from tablename where id= ;
Ex) delete name from dummy where id= 1;
Delete the whole row from the table
Syntax) delete from tablename where id= ;
Ex) delete from dummy where id= 2;
Truncate: Used to the empty the entire table without deleting the schema of the table.
Syntax) truncate tablename;
Ex) truncate dummy;
Syntax) drop table tablename;
Ex) Drop table dummy;
Syntax) drop keyspace databasename;
Ex) drop keyspace demo;
Syntax: SELECT column name FROM table name ORDER BY identifier;
Ex) SELECT * FROM dummy ORDER BY name;
Syntax: HELP;
#Syntax) version
Syntax) COLOR (‘<colour name>’);
#Syntax) DEBUG;
Syntax) FILE (‘<file name>’);
#Syntax) U<“user name”>;
Syntax) P<“password”>;