Java Code to Connect Cassandra Database
Here, I am going to make you aware of how to connect and fire commands from Java Code to the Cassandra Database which is a NoSQL Database.
you can download hector-core library for Communicating to cassandra database from the following url:-
https://github.com/hector-client/hector/downloads
and download the "hector-core-1.0-2.tgz" as I had chosen this in my example.
After you download the jar file.
Unzip in some folder say (d:/hector)
Open Eclipse IDE (I used eclipse Indigo).
Choose your prefered worspace.
Create a new Java Project with some name (say; ConnectingToCassandra)
Right Click on your project and select "Configure Build Path..." under "Build Path" Menu.
Goto "Libraries" tab and Select "Add External Jars" button.
Browse to the location where you have hector-core api (d:/hector).
Select all the jars with "ctrl+A" key.
Click “Open” button
Click "OK".
Now,
Right click on project and create new class with “public static void main(String args[])”.
In the main() method write the following code :-
------------------------------------------------------------------------------------------------------------
Cluster cluster = HFactory.createCluster("TestCluster", new CassandraHostConfigurator("localhost:9160"));
import the required packages.
the above line gives you a reference pointing to the cluster "TestCluster" which is default cluster created at the time of installation of cassandra database. it is similar to your connection object in JDBC.
--------------------------------------------------------------------------------------------------------
String keyspaceName="Animal";
Keyspace keyspace=HFactory.createKeyspace(keyspaceName, cluster);
try {
cluster.addKeyspace(HFactory.createKeyspaceDefinition(keyspaceName));
System.out.println("Keyspace added successfully in the cluster");
}catch (HInvalidRequestException e) {
System.out.println("Keyspace : "+keyspaceName+" already exists");
}catch (Exception e) {
e.printStackTrace();
}
The above code is creating a keyspace which is similar to a database in RDBMS.
The code above also throws a Runtime Exception "HInvalidRequestException", if the keyspace you are creating is already exist in the Cluster.
---------------------------------------------------------------------------------------------------------
String columnFamilyName="Humans";
try {
cluster.addColumnFamily(HFactory.createColumnFamilyDefinition(keyspaceName,columnFamilyName));
} catch (HInvalidRequestException e) {
System.out.println("Column Family : "+columnFamilyName+" already exists");
}catch (Exception e) {
e.printStackTrace();
}
The above code creates a Column family similar to table in RDBMS.
It also throws the Runtime Exception "HInvalidRequestException", if Column family already exist.
----------------------------------------------------------------------------------------------------------
StringSerializer stringSerializer=new StringSerializer();
Mutator<String> mutator=HFactory.createMutator(keyspace, stringSerializer);
mutator.insert("001", columnFamilyName,HFactory.createStringColumn("Name", "Satyen"));
IntegerSerializer integerSerializer=new IntegerSerializer();
HColumn< String, Integer> hc=HFactory.createColumn("Age", 23, stringSerializer, integerSerializer);
mutator.insert("001", columnFamilyName,hc);
The above code inserts a record with ID "001" as Key and "Name" as Column Name and "Satyen" as column value. similarly, "Age" as Column name and "23" as Column Value.
---------------------------------------------------------------------------------------------------------
Now you can open your CQL Shell or CLI to check if the records got entered in the database.
After you fire command to see the records you will find that the data inserted in the column family are stored in Hexadecimal. This is because we haven't specified the "Comparator" when we created column family.
Thats It!!! for the moment I will come up with some more commands Soon......
Thanks!!!!
you can download hector-core library for Communicating to cassandra database from the following url:-
https://github.com/hector-client/hector/downloads
and download the "hector-core-1.0-2.tgz" as I had chosen this in my example.
After you download the jar file.
Unzip in some folder say (d:/hector)
Open Eclipse IDE (I used eclipse Indigo).
Choose your prefered worspace.
Create a new Java Project with some name (say; ConnectingToCassandra)
Right Click on your project and select "Configure Build Path..." under "Build Path" Menu.
Goto "Libraries" tab and Select "Add External Jars" button.
Browse to the location where you have hector-core api (d:/hector).
Select all the jars with "ctrl+A" key.
Click “Open” button
Click "OK".
Now,
Right click on project and create new class with “public static void main(String args[])”.
In the main() method write the following code :-
------------------------------------------------------------------------------------------------------------
Cluster cluster = HFactory.createCluster("TestCluster", new CassandraHostConfigurator("localhost:9160"));
import the required packages.
the above line gives you a reference pointing to the cluster "TestCluster" which is default cluster created at the time of installation of cassandra database. it is similar to your connection object in JDBC.
--------------------------------------------------------------------------------------------------------
String keyspaceName="Animal";
Keyspace keyspace=HFactory.createKeyspace(keyspaceName, cluster);
try {
cluster.addKeyspace(HFactory.createKeyspaceDefinition(keyspaceName));
System.out.println("Keyspace added successfully in the cluster");
}catch (HInvalidRequestException e) {
System.out.println("Keyspace : "+keyspaceName+" already exists");
}catch (Exception e) {
e.printStackTrace();
}
The above code is creating a keyspace which is similar to a database in RDBMS.
The code above also throws a Runtime Exception "HInvalidRequestException", if the keyspace you are creating is already exist in the Cluster.
---------------------------------------------------------------------------------------------------------
String columnFamilyName="Humans";
try {
cluster.addColumnFamily(HFactory.createColumnFamilyDefinition(keyspaceName,columnFamilyName));
} catch (HInvalidRequestException e) {
System.out.println("Column Family : "+columnFamilyName+" already exists");
}catch (Exception e) {
e.printStackTrace();
}
The above code creates a Column family similar to table in RDBMS.
It also throws the Runtime Exception "HInvalidRequestException", if Column family already exist.
----------------------------------------------------------------------------------------------------------
StringSerializer stringSerializer=new StringSerializer();
Mutator<String> mutator=HFactory.createMutator(keyspace, stringSerializer);
mutator.insert("001", columnFamilyName,HFactory.createStringColumn("Name", "Satyen"));
IntegerSerializer integerSerializer=new IntegerSerializer();
HColumn< String, Integer> hc=HFactory.createColumn("Age", 23, stringSerializer, integerSerializer);
mutator.insert("001", columnFamilyName,hc);
The above code inserts a record with ID "001" as Key and "Name" as Column Name and "Satyen" as column value. similarly, "Age" as Column name and "23" as Column Value.
---------------------------------------------------------------------------------------------------------
Now you can open your CQL Shell or CLI to check if the records got entered in the database.
After you fire command to see the records you will find that the data inserted in the column family are stored in Hexadecimal. This is because we haven't specified the "Comparator" when we created column family.
Thats It!!! for the moment I will come up with some more commands Soon......
Thanks!!!!
Comments
Post a Comment