Apache Cassandra, a top level Apache project born at Facebook , is a distributed database for managing large amounts of structured data across many commodity servers, while providing highly available service and no single point of failure.
BASIC FLOW OF DATA INTO CASSANDRA TABLES
Installation
In a terminal window:
1. Check which version of Java is installed by running the following command:
$ java -version
It is recommended to use the latest version of Oracle Java 8 or OpenJDK 8 on all nodes.
2. Download Apache Cassandra 3.0:
$ curl -L http://downloads.datastax.com/community/dsc-cassandra-version_number-bin.tar.gz | tar xz
To view the available versions, see the DataStax Certified Cassandra download page.
3. If you download from this page, use the following command to untar:
$ tar -xvzf dsc-cassandra-version_number-bin.tar.gz
4. To configure Cassandra, go to the install/conf directory:
$ cd dsc-cassandra-version_number/conf
Additional Information :
In Ubuntu 16.x , while Running Cql , If an error like this pops :
//ERROR : Connection error: (‘Unable to connect to any servers’, {‘127.0.0.1’: TypeError(‘ref() does not take keyword arguments’,)})
Use these commands And try again :
$ sudo apt-get install python-pip
$ pip install cassandra-driver
$ export CQLSH_NO_BUNDLED=true
Accessing the Cassandra Database
The Cassandra Database Can be Queried By multiple Ways.We will discuss 2 Major ways :
- Directly through Cql terminal.
- Through a Scala Program.
In both cases we first need to run the Cassandra by going to the directory through the terminal where Cassandra is installed With the help of cd command :
$ cd install_location (path of the cassandra directory)
After that use the following command to start the Cassandra :
$ ./bin/cassandra ( use -f for running Cassandra in foreground)
Once the Cassandra is up and running We can now move to using it. Let us discuss the 2 ways we named earlier to use the Cassandra database :
Using Cassandra Through Cqlsh (CQL terminal)
Go to the directory through the terminal where Cassandra is installed with the help of cd command :
$ cd install_location (path of the cassandra directory)
Use the command to run the cql Terminal
$ ./bin/cqlsh
You will See Something like this :
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 2.2.7 | CQL spec 3.3.1 | Native protocol v4]
Use HELP for help.
cqlsh> // Start Here
After Getting this Screen You are ready to Go.
1. KEYSPACES :
- To Begin With We First Need a Keyspace :
Command : CREATE KEYSPACE WITH
Example : CREATE KEYSPACE Demo WITH replication = {‘class’:’SimpleStrategy’, ‘replication_factor’ : 1};
- Using the Keyspace :
Command : USE
Example : USE Demo ;
- NOTE : To List the available Keyspaces Use :
DESC KEYSPACES;
- Deleting a Keyspace :
Command : DROP KEYSPACE
Example :DROP KEYSPACE Demo;
2. TABLES :
- To Create a Table :
Command : CREATE TABLE (column1_name datatype,column2_name data type,column3_name data type,….,PRIMARY KEY(,))
Example : CREATE TABLE employee(emp_id int, emp_name text,emp_city text,emp_salary varint,emp_phone varint,PRIMARY KEY(emp_id,emp_salary));
- Inserting Data into the Table :
Command : INSERT INTO (coloumns) VALUES (values)
Example : INSERT INTO employee(emp_id, emp_name,emp_city ,emp_salary ,emp_phone) VALUES(1,’Name1′,’Delhi’,50000,9876543210);
- NOTE : To List the available Tables :
DESC TABLES;
- Updating a table :
Command : ALTER TABLE
Example 1 (Add Column) : ALTER TABLE employee ADD emp_email text;
Example 1 (Drop Column) : ALTER TABLE employee DROP emp_email;
- Deleting a Table :
Command : DROP TABLE
Example :DROP TABLE employee;
- Querying The Tables :
Command : SELECT FROM WHERE ;
Example : Select * from employee where emp_id=1 ;
Using Cassandra Through Scala Program
Note : Make Sure the Cassandra is Running
1. Create a new Sbt Project.
2. Add the following dependency in built.sbt :
libraryDependencies += “com.datastax.cassandra” % “cassandra-driver-core” %”2.1.10.3″
3. Import the following in the Program :
import com.datastax.driver.core.Cluster
4. Within the Object create the following :
// creating Cluster object
val cluster = Cluster.builder().addContactPoint(“localhost”).build()// Creating Session object
val session = cluster.connect()
5. Create a normal query in a variable and execute the query :
val createKeySpaceQuery = "CREATE KEYSPACE cassandraDemo WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};" session.execute(createKeySpaceQuery)
6. Similarly Execute all the queries.
For Other Important Commands. Refer this Link to Datastax site: http://docs.datastax.com/en/cql/3.1/cql/cql_reference/cqlCommandsTOC.html
For Further Reference Check out the Github Link: https://github.com/anmol2709/Cassandra-Scala-For-Beginners.git
Reblogged this on Bharat Singh and commented:
Cassandra Usage – Basics
Reblogged this on Site Title and commented:
Cassandra Database : The Next Big Thing