Fetching single Column from Cassandra database


package com.examples;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.KeySlice;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class CassandraFeatchHInfo {
public static final String UTF8 = “UTF8”;

public static void main(String[] args) throws UnsupportedEncodingException,
InvalidRequestException, UnavailableException, TimedOutException,
TException, NotFoundException {

TTransport tr = new TSocket(“192.168.1.204”, 9160);
TProtocol proto = new TBinaryProtocol(tr);
Cassandra.Client client = new Cassandra.Client(proto);

tr.open();

String keyspace = “Historical_Info”;
String columnFamily = “Historical_Info_Column”;
//String keyUserID = “3”;

// read entire row
SlicePredicate predicate = new SlicePredicate();
SliceRange sliceRange = new SliceRange();
sliceRange.setStart(new byte[0]);
sliceRange.setFinish(new byte[0]);
predicate.setSlice_range(sliceRange);

KeyRange keyrRange = new KeyRange();
keyrRange.setStart_key(“1”);
keyrRange.setEnd_key(“”);
//keyrRange.setCount(100);

ColumnParent parent = new ColumnParent(columnFamily);

List<KeySlice> ls = client.get_range_slices(keyspace, parent, predicate, keyrRange, ConsistencyLevel.ONE);

for (KeySlice result : ls) {
List<ColumnOrSuperColumn> column = result.columns;
for (ColumnOrSuperColumn result2 : column) {
Column column2 = result2.column;
System.out.println(new String(column2.name, UTF8) + ” ->  ” + new String(column2.value, UTF8));
}
}

tr.close();
}
}

Fetching all columns example in Cassandra


package com.examples;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.KeySlice;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class CassandraFeatchHInfo {
public static final String UTF8 = “UTF8”;

public static void main(String[] args) throws UnsupportedEncodingException,
InvalidRequestException, UnavailableException, TimedOutException,
TException, NotFoundException {

TTransport tr = new TSocket(“192.168.1.204”, 9160);
TProtocol proto = new TBinaryProtocol(tr);
Cassandra.Client client = new Cassandra.Client(proto);

tr.open();

String keyspace = “Historical_Info”;
String columnFamily = “Historical_Info_Column”;
//String keyUserID = “3”;

// read entire row
SlicePredicate predicate = new SlicePredicate();
SliceRange sliceRange = new SliceRange();
sliceRange.setStart(new byte[0]);
sliceRange.setFinish(new byte[0]);
predicate.setSlice_range(sliceRange);

KeyRange keyrRange = new KeyRange();
keyrRange.setStart_key(“1”);
keyrRange.setEnd_key(“”);
//keyrRange.setCount(100);

ColumnParent parent = new ColumnParent(columnFamily);

List<KeySlice> ls = client.get_range_slices(keyspace, parent, predicate, keyrRange, ConsistencyLevel.ONE);

for (KeySlice result : ls) {
List<ColumnOrSuperColumn> column = result.columns;
for (ColumnOrSuperColumn result2 : column) {
Column column2 = result2.column;
System.out.println(new String(column2.name, UTF8) + ” ->  ” + new String(column2.value, UTF8));
}
}

tr.close();
}
}

Cassandra Insert Example


package com.example;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class CassandraInsertExample {
public static final String UTF8 = “UTF8”;

private static Long hiId = new Long(1) ;

public static void main(String[] args) throws UnsupportedEncodingException,
InvalidRequestException, UnavailableException, TimedOutException,
TException, NotFoundException {

CassandraInsertExample cassandraInsertHInfo = new CassandraInsertExample();
cassandraInsertHInfo.insertHistoricalInfo(hiId);
}

public void insertHistoricalInfo(Long hiId) {
System.out.println(“Stating of class…………………………..”);
try {

TTransport tr = new TSocket(“localhost”, 9160);
TProtocol proto = new TBinaryProtocol(tr);
Cassandra.Client client = new Cassandra.Client(proto);
tr.open();

String keyspace = “Employee”;
String columnFamily = “Employee_Details”;

String keyUserID = hiId.toString();

// insert data
long timestamp = System.currentTimeMillis();

ColumnPath colPathhiId = new ColumnPath(columnFamily);
colPathhiId.setColumn(“hiId”.getBytes(UTF8));
client.insert(keyspace, keyUserID, colPathhiId, hiId.toString().getBytes(UTF8), timestamp, ConsistencyLevel.ONE);

//Fetching of single row
Column col = client.get(keyspace, keyUserID, colPathhiId, ConsistencyLevel.ONE).getColumn();

System.out.println(“column name: ” + new String(col.name, UTF8));
System.out.println(“column value: ” + new String(col.value, UTF8));
System.out.println(“column timestamp: ” + new Date(col.timestamp));

tr.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (TTransportException e) {
e.printStackTrace();
} catch (UnavailableException e) {
e.printStackTrace();
} catch (TimedOutException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
}

ENABLING JMX PORT IN WEBLOGIC


Weblogic runs with domains. With different domains you can enable the JMX port by fallowing the below steps.

Step 1: Go to your domain bin folder, which you want to enable JMX remote port.

Ex: C:\bea\wlserver_10.3\samples\domains\wl_server\bin

Step 2: edit the “setDomainEnv.cmd” file and add the fallowing code as above line of the set CLASSPATH

set JAVA_OPTIONS= %JAVA_OPTIONS%
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=8006
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false

Step 3: Restart the weblogic server.
Step 4 : Try to connect for the Jconsole with host:8006, It will connect to your domain of weblogic server.

Introduction to Apache Cassandra.


What is Cassandra?

Apache Cassandra is a non relational database which is given by the Apache. Initially, Cassandra was open sourced by Facebook in 2008, and is now developed by Apache Group.

In the normal relational databases data stores in the format of rows, but in Cassandra the data will stored in columns format as key value pairs. Due to this column based data storage its giving the high performance while comparing the relational databases.

Cassandra can handle many terabytes of data if need be and can easily handle millions of rows, even on a smaller cluster. Cassandra can get around 20K inserts per second.

The performance of Cassandra is high and Keeping the performance up while reading mostly depends on the hardware, configuration and number of nodes in your cluster.  It can be done in Cassandra without much trouble.

But there is No SQL, then how to Query?

To insert and retrieve the data there are some apis. Thrift framework is also one of its client API. Essentially a communication protocol used not just by Cassandra but by many others.

Who are using Apache Cassandra?

Cassandra is in use at Digg, Facebook, Twitter, Reddit, Rackspace, Cloudkick, Cisco, SimpleGeo, Ooyala, OpenX, and more companies that have large, active data sets. The largest production cluster has over 100 TB of data in over 150 machines.

For the RDBMS users it will take time to implement the Cassandra.

Terminology of Cassandra:

Column – Column is a tuple with binary no-fixed length name and value along with the timestamp. To keep it simple ignore the timestamp for the moment.

Super Column – Essentially a container for one or more columns. It is again a tuple with a binary name and a map of where key is the same as the name of the column. The main difference between Column and Super Column is, Column contains string format data and Super Column contains Hash Table Format.

Column Family – A structure which keeps an infinite number of rows just like a traditional table. Each row in itself has a binary key and a map of where again a key is the same as the name of the column

Super Column Family – Same as column family with the exception that each row has a map of super columns instead of columns. The map is keyed with the name of each SuperColumn and the value is the SuperColumn itself

Keyspace – It is like schema containing the column families

Sorting – The data is sorted as soon as we put the data within the cluster and it remains that way as there is no way to do it while fetching the data which makes it all the most necessary to plan it right as per the access path.

What might be the reason to develop Cassandra in Java?

* Security: it’s easier to write secure software in Java than in C++ (remember the buffer overflows?)

* Performance: it’s not THAT worse. It’s definetely worse at startup, but once the code is up and running, it’s not a big thing. Actually, you have to remember an important point here: Java code is continually optimized by the VM, so in some circunstances it get faster then C++

Features of Cassandra:

Fault Tolerant : Data is automatically replicated to multiple nodes. Loosing a node doesn’t bring down the cluster

Flexible Schema : We are talking in terms of columns, supercolumns and columnfamilies instead of rows and tables. BigTable datamodel

Symmetric : No single point of failure, Every node within the cluster is identical and there are no network bottlenecks

Scalable : Linear with addition of new machines with no downtime or interruption to applications. Read and write throughput increase linearly as new machines are added

Support for Large Data : The ability to scale to many hundreds of gigabytes of data

Written in Java : Originally built for facebook and then made open source

Apache Cassandra


Some of useful links

http://cassandra.apache.org/http://schabby.de/cassandra-installation-configuration/http://wiki.apache.org/cassandra/ThriftExampleshttp://prettyprint.me/2010/02/23/hector-a-java-cassandra-client/http://www.rackspacecloud.com/blog/2010/05/12/cassandra-by-example/http://blog.evanweaver.com/articles/2009/07/06/up-and-running-with-cassandra/http://wiki.apache.org/cassandra/APIhttp://anismiles.wordpress.com/2010/05/24/connecting-to-cassandra-1/

Tips to fallow while doing JAVA code


  • Counting down (i.e. for (int i=n; i>0; i–)) is twice as fast as counting up: my machine can count down to 144 million in a second, but up to only 72 million.
  • Calling Math.max(a,b) is 7 times slower than (a > b) ? a : b. This is the cost of a method call.
  • Arrays are 15 to 30 times faster than Vectors. Hashtables are 2/3 as fast as Vectors.
  • Use System.arraycopy(firstArray, 0, secondArray, 0, firstArray.length) method instead of iterating the first array and copying into second array.
  • Use compound assignment operators (+=, -=, *=, and /=) instead of using normal operators.
  • Ex: a = a+b takes longer time to execute when compared to a += b. In fact, these cause different Java byte codes to be generated.
  • Eliminate unnecessary code in the loops and you should avoid declaring variables unnecessarily within loops.
  • Use int instead of other primitive types because operations performed on int primitives generally execute faster than for any other primitive type supported by Java, so you should use int values whenever possible; char and short values are promoted to int automatically before arithmetic operations.
  • Use notify() instead of notifyAll(), notify will execute faster than notifyAll().
  • When joining couple of Stings use StringBuffer instead of String.
  • Don’t try to convert Strings to upper or lower case for String comparison.
  • In the String Class prefer charAt() method instead of startsWith() method. From performance perspective, startWith() makes quite a few comparisons preparing itself to compare it’s prefix with another string.
  • Don’ t initialize the public instance variable in constructor if they already initialized outside the constructor. Because all public initialized instance variables are again initialized in constructor by default.
  • Vector provides the following methods to insert elements.
    addElementAt( e, index)
    addElement (e)
    add(e)
    add(index, e)
  • Out of these try to avoid using methods, addElementAt( e, index) and add(index, e). The way these methods work is , all the elements are moved down between the insertion point and the end of the vector, making space for the new Element. The same works for deleting element at a Particular index. If possible, if these features are required, then try to use a different Data Structure if possible.
  • If the approximate size of the Vector is know initially then use it. Instead of declaring Vector as,

Vector v = newVector();
declare it as,Vector v = new Vector(40);
or  Vector v = new Vector(40,25) ;

  • This method indicates initial capacity of Vector is 40 and increment by 25 elements per expansion.The way the Vector is expanded is, a new Vector of double the size of currentVector is created, all the Elements in the old Vector is copied to the new Vector and then the old Vector is discarded. (During GC). This has major effect on performance.