Thursday, August 19, 2010

Cassandra Fetch Example -- Single Row fetch

package com.examples;

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 CassandraFetchHInfo {
    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 {
      
        CassandraFetchHInfo cassandraFetchHInfo = new CassandraFetchHInfo();
        cassandraFetchHInfo.insertHistoricalInfo(hiId, objectObjHierarchyId, hiParameterOid, hiMonitoredValue, hiTimestampCreated, hiObjectId, hiInfoEventType);
    }

    public void insertHistoricalInfo(Long hiId,
            Long objectObjHierarchyId, String hiParameterOid,
            String hiMonitoredValue, Date hiTimestampCreated,
            Object hiObjectId, String hiInfoEventType) {
        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 = "Historical_Info";
            String columnFamily = "Historical_Info_Column";

            String keyUserID = hiId.toString();

          
            ColumnPath colPathhiId = new ColumnPath(columnFamily);
            colPathhiId.setColumn("hiId".getBytes(UTF8));

          
            // read single column
          
             System.out.println("single column:");
             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();
        }
    }
}

Cassandra insertion 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();
        }
    }
}

Tuesday, August 17, 2010

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 will store as 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.


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
    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. I am not going to go all in detail here but I guess you got the idea.

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

Monday, August 2, 2010

Java Performance Tuning Tips

  1. 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. 
  2. Calling Math.max(a,b) is 7 times slower than (a > b) ? a : b. This is the cost of a method call. 
  3. Arrays are 15 to 30 times faster than Vectors. Hashtables are 2/3 as fast as Vectors.
  4. Use System.arraycopy(firstArray, 0, secondArray, 0, firstArray.length) method instead of iterating the first array and copying into second array.
  5. Use compound assignment operators (+=, -=, *=, and /=) instead of using normal operators.
  6. 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.
  7. Eliminate unnecessary code in the loops and you should avoid declaring variables unnecessarily within loops.
  8. 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.
  9. Use notify() instead of notifyAll(), notify will execute faster than notifyAll().
  10. When joining couple of Stings use StringBuffer instead of String.
  11. Don't try to convert Strings to upper or lower case for String comparison.
  12. 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. 
  13. 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.
  14. Vector provides the following methods to insert elements.                                                                       addElementAt( e, index)
             addElement (e)
             add(e)
             add(index, e)
  15. 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.
  16. 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) ; 
  17. 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.

Tuesday, July 27, 2010

Enabling JMX port in Tomcat

By default JMX port is not enable in the Tomcat.
If you want to enable the JMX port, add the fallowing properties
in catalina.bat file which located in bin directory of the %CATALINA_HOME%.
Add the fallowing properties as starting line of catalina.bat
set JAVA_OPTS= %JAVA_OPTS% -Dcom.sun.management.jmxremote.port=7009
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false

Enabling JMX port in JBOSS

By default JMX port is not enabled in the JBOSS. If you want enable the JMX port, add the falowing properties
in run.bat file which located in bin directory of the %CATALINA_HOME%.
set JAVA_OPTS = %JAVA_OPTS% -Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl
-Djboss.platform.mbeanserver -Dcom.sun.management.jmxremote.port=8007
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
To add the above propertis in run.bat fallow the steps
1. Find for the fallowing code:
set JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx512m
2. Replace the above code with :
set JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx512m -Djavax.management.builder.initial=org.jboss.system.server.jmx.MBeanServerBuilderImpl -Djboss.platform.mbeanserver -Dcom.sun.management.jmxremote.port=8007 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false

Monday, July 26, 2010

Enable JMX Remote port in Websphere

By default Websphere is not JMX enabled ,  so we have to enable the JMX remote port after installation.
Below I am trying to explain the steps to enable the JMX remote port in Websphere.  These are the steps I have fallowed to enable the JMX remote port in Websphere 7.0.
STEP 1:
Login to Admin console of the web sphere any profile(server). You can find the shortcut in the Windows menu after installation.
Step 2:
Deploy the PerfServletApp.ear application if not deployed already:  Go to "Applications" left pane of admin console, click on  WebSphere Enterpise Applications. Check PerfServletApp.ear is deployed or not, if its not deployed – fallow the below steps
New Application underApplications. Browse from WebSphere directory -> AppServer -> InstallableApps.
Click on next button to  deploy Pref Application. Fallow the next steps to complete the installation
STEP 3:
 Enable the PMI Data and set all the statistics enabled:
Go to "Monitoring and Tuning" in left pane of Admin Console, Click on Performance Monitoring Infrastructure(PMI) in "Configuration" tab, enable the "PMI and set the All Statistics. Also set the All Statistics in Run time Tab. 
Step 4:  Don’t forget to save all changes……….. J
STEP 5:
    Set the generic jvm argument =
-Djavax.management.builder.initial= -Dcom.sun.management.jmxremote
In Severs --> Server Types --> WebSphere Application Servers  shows the servers list. Click on the server you want.
In the right pane --> Server Infrastructure --> Java and Process Management  click on Process definition, again in Additional Properties of Configuration tab

Click on Java Virtual Machine and put the -Djavax.management.builder.initial= -Dcom.sun.management.jmxremote in Generic Jvm Argument field.
SAVE All THE CHANGES
STEP 6:   
    To enable the JMX remote port open the below properties file and add the code below.
    Note : The properties file can be available in :
WebSphere_Home directory  \AppServer\java\jre\lib\management\management.properties
CODE :
        com.sun.management.jmxremote.port=9001
        com.sun.management.jmxremote.ssl=false
        com.sun.management.jmxremote.authenticate=false

STEP 7:
Save all the changes and stop and start the server.   
After doing all this you can connect the Websphere using your JConsole Appplication.

Friday, July 2, 2010

Jar file Vs Executable Jar file

Jar file is the combination of compiled java classes.
Executable jar file is also be combination of compiled java classes with Main class.
Normal jar file can be created as
C:\> jar -cvf TestNew.jar .
But while creating executable jar file we have to specify the Main-Class in a manifest file. Following are the steps to fallow while creating the executable jar file. Here its explained with a simple class in a test package.
Step1: create a java class TestNew under package test
package test;
public class TestNew{
public static void main(String a[]){
System.out.println(“Hello world”);
}
}
Step 2: compile the class with fallowing command
C:\>javac TestNew.java -d .
Note: Once you run the TestNew class you have to get “HelloWorld” in the console. To run use fallowing command
C:\>java test.TestNew
Step 3: Create a “mainClass” file in the directory where your test folder is located . This file contains a single line specifying where the main Class is to be found in the jar file. Note that I use the package specification. Here is the single line:
Main-Class: test.TestNew
Note: specify the single line only don't specify anything
Step 4: create the jar using fallowing command
C:\>jar cmf mainClass test.jar test
Where “c” represents the create that you are going to “create”, “m” specifies the manifest file mainClass (which adds information to the jar file on where the main class will be found) and “f ” refers to “file”.
Note: The file has to be created in your folder with the name test.jar
To view the content of the jar file you can use the fallowing command
C:\>jar tf test.jar
It displays as:
META-INF/
META-INF/MANIFEST.MF
test/
test/TestNew.class
To execute the jar file you can use the following commond
C:\>java -jar test.jar

What is the difference bet ween object and instance

There is a lot of discussions on this topic over the internet. Some of the people says both are same and some of other says both are different. Here I am planning to share my overall view on this topic. As per my knowledge both are looks same but different. Here are the some useful points:
  • Instance is Logical but object is Physical means occupies some memory.
  • We can create an instance for abstract class as well as for interface, but we cannot create an object for those.
  • Object is instance of class and instance means representative of class i.e object.
  • Instance refers to Reference of an object.
  • Object is actually pointing to memory address of that instance.
  • You can’t pass instance over the layers but you can pass the object over the layers
  • You can’t store an instance but you can store an object
  • A single object can have more than one instance.
  • Instance will have the both class definition and the object definition where as in object it will have only the object definition.
Syntax of Object:
classname var=new classname();
But for instance creation it returns only a pointer refering to an object, syntax is :
classname varname;