Friday, 30 May 2014

Getting Started:Python-CGI programming Configuring CGI scripts on apache server

Before starting Python-CGI programming first we have to allow apache server to run CGI scripts.
The following steps are for ubuntu 14.04 users:


Step 1:Using ScriptAlias

Open terminal and run 

sudo gedit /etc/apache2/apache2.conf
Note:First create the following directory /var/www/cgi-bin

Add the following lines:-

ServerName localhost
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
Options +ExecCGI
AddHandler cgi-script .cgi .pl .py

ScriptAlias converts /cgi-bin/ address to the specified address.
The following AddHandler directive tells the server to treat all files with the cgi or pl extension as CGI programs.

Open terminal and run 

sudo gedit /etc/apache2/conf-available/serve-cgi-bin.conf
Add the following lines between <IfDefine ENABLE_USR_LIB_CGI_BIN> and </Directory>
.

ScriptAlias /cgi-bin/ /var/www/cgi-bin
   
      Options +ExecCGI

Step 2 :Creating CGI scripts

Open terminal and run 


sudo gedit /var/www/cgi-gin/hello.py

Copy the following lines and save the file.

#!/usr/bin/python

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'

'Note: /usr/bin/python is the python installation directory

After saving the file then run the following command:

chmod +x hello.py

Now restart the apache server:

sudo /etc/init.d/apache2 restart


Step 3 :Running CGI scripts

Now open your browser and type the url


localhost/cgi-bin/hello.py



It will print Hello World! This is my first CGI program. 

Saturday, 5 April 2014

Hadoop : WARN util.NativeCodeLoader



This is one of my daily adventure posts.In this post i am going to discuss the problem and the solution for a problem that occured while running Word Count job on my Hadoop installation on my Ubuntu 13.04.


So this is what,I encountered every time while running the job as well as starting start-all.sh script.

WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
This was because the bundled native libraries are for 32-bit systems. We need to build the 64-bit libraries ourselves from the source.

So after googling and trying half a dozen tutorials.I finally found the answer.



Prepare
  1. Install utilities for building.
    sudo apt-get install build-essential
    sudo apt-get install g++ autoconf automake libtool cmake zlib1g-dev pkg-config libssl-dev
    sudo apt-get install maven
  2. Ensure that protobuf 2.5 is installed. Download from https://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz
    tar xzvf protobuf-2.5.0.tar.gz
    cd protobuf-2.5.0
    /configure --prefix=/usr
    make
    sudo make install
Download Sources
  1. Download the source tarball hadoop-2.2.0-src.tar.gz from one of the mirrors.
  2. Extract the sources to a convenient location – this path is mentioned as hadoop-2.2.0-src below.
Compile Hadoop
  1. There is a known compilation issue with the 2.2.0 sources. Let’s fix it first. Edit hadoop-common-project/hadoop-auth/pom.xml, and insert
    
    org.mortbay.jetty
    jetty-util
    test
    
    above the dependency block for jetty.
  2. Compile sources. Make sure you are in the source folder (hadoop-2.2.0-src).
    mvn package -Pdist,native -DskipTests -Dtar
  3. It will take a while to complete the compilation process. Back up the files from hadoop-2.2.0/lib/native, and copy the newly generated files in hadoop-2.2.0-src/hadoop-dist/target/hadoop-2.2.0/lib/native to hadoop-2.2.0/lib/native.
  4. Run the examples again to see if the performance is better. The warnings related to the non-native libraries should be gone. For me, the performance improved significantly after building native 64-bit libraries.

Saturday, 22 March 2014

Introduction to NoSQL

NoSQL stands for NO SQL that is ,it does not follow traditional SQL with RDBMS instead it is a DBMS with support of large scalability and thus it is emerging as the solution to BigData.

Big data is a popular term used to describe the exponential growth and availability of data, both structured and unstructured.


Difference between NoSQL and SQL?


With increasing amount of data on social networking sites such as posts,tweets,messages etc.Need for fast data processing emerged SQL is strictly RDBMS and has strict Schema but it lacks in Scalability and that is where NoSQL emerges as a hero!

The key differences between them are:

Comparison on
SQL
NoSQL
Winner
Queries
Has good support for all kind of query needs like SELECT,UPDATE,JOINS etc.
Support is currently being added as it lacks the ease with which JOINS are performed in SQL and many other features of a   perfect SQL.
SQL
Speed
Uses query for performing tasks on data.
Uses Map Reduce to perform tasks on data which gives distinct advantage as Map Reduce computes parallel on blocks of a single file.
NoSQL
Transactions
Can commit any number of cross transactions.
Uses Entity Group for Multiple Row Transactions but usage of these Entity Groups for cross transactions is limited to 5 in most cases.

SQL
Scalability
Not scalable to millions of requests at a time.
More scalable than SQL as it uses multiple replication.
NoSQL
Schema
Strictly enforced schema which provides a good hold over how data is being stored and its relationship with others.
But for huge table changing schema sometimes locks table temporary this is the main disadvantage.
No locking of table occurs.
NoSQL



To have more grasp on the differences between SQL and NoSQL please watch this video.



Now moving towards the BigData available solutions:-
  1. The First Solution is developed and maintained by Google which is known as Data Store.
  2. The Second Solution is known as Hadoop and is maintained by Apache.

Data Store

Developed and maintained by Google it is a stack which uses GFS(Google File System) as stroage file system and uses Big Table as NoSQL solution.
It's complete stack consists of :

Hadoop

Hadoop was initially developed at Yahoo and it is a set of tools which uses HDFS in place of GFS of Google and instead of BigTable it uses HBase.

To understand the working of Hadoop more clearly and broad sense.I would recommend you to read the following blog.






Adding JLabel and JButton to JTable

Adding JButton to JTable


First we are going to create a class which extends Cell Renderer.
ie.

public class Renderer extends DefaultTableCellRenderer{

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
         boolean hasFocus, int row, int column)
     {

        if(value instanceof JButton){
         
             JButton Button = (JButton)value;
           
           
            return (JButton)value;
        }
     
     

        else{
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
     }
}


Then create a class MyModel which extends Default Table Model.

ie.

class MyModel extends javax.swing.table.DefaultTableModel{

    Object[][] row = new Object[2][2];

    Object[] col = {"File Name", "Status"};
   

    public MyModel (){
   
row[0][0]=new JButton("This File"); //adds button
row[0][1]="HI";
row[1][0]=new JButton("That File"); //adds button
row[1][1]="BYE"; //adds button
       
 }

       pack();
    //Adding columns
        for(Object c: col)
            this.addColumn(c);

    //Adding rows
        for(Object[] r: row)
            addRow(r);

    }

    @Override

    public Class getColumnClass(int columnIndex) {
        if(columnIndex == 1)return getValueAt(0, columnIndex).getClass(); //Sets JButton on 1 column
   
        else return super.getColumnClass(columnIndex);
   
         

    }

}

Now to get your table working add the following lines in your JFrame constructor.

jTable1.setModel(new MyModel());
 
 jTable1.setDefaultRenderer(JButton.class,  new Renderer());



Adding JLabel to JTable


First we are going to create a class which extends Cell Renderer.
ie.

public class Renderer extends DefaultTableCellRenderer{
 
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
         boolean hasFocus, int row, int column)
     {
 
        if(value instanceof JLabel){
           
             JLabel label = (JLabel)value;
            
             
            return (JLabel)value;
        }
       
       
 
        else{
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
     }
}


Then create a class MyModel which extends Default Table Model.

ie.

class MyModel extends javax.swing.table.DefaultTableModel{
 
    Object[][] row = new Object[2][2];

    Object[] col = {"File Name", "Status"};
    
 
    public MyModel (){
    
row[0][0]=new JLabel("This File"); //adds label
row[0][1]="HI"; 
row[1][0]=new JLabel("That File"); //adds label
row[1][1]="BYE"; //adds label
         
 }
 
       pack();
    //Adding columns
        for(Object c: col)
            this.addColumn(c);
 
    //Adding rows
        for(Object[] r: row)
            addRow(r);
 
    }
 
    @Override
 
    public Class getColumnClass(int columnIndex) {
        if(columnIndex == 1)return getValueAt(0, columnIndex).getClass(); //Sets JLabel on 1 column
     
        else return super.getColumnClass(columnIndex);
    
          
 
    }
 
}

Now to get your table working add the following lines in your JFrame constructor.

jTable1.setModel(new MyModel());
   
 jTable1.setDefaultRenderer(JLabel.class,  new Renderer());

Sunday, 2 March 2014

Introduction to Exceptions and Ducking

In this blog we are going to discuss about error and exception handling.


Note:It should be noted that only developers care about handling Exceptions although you must understand that you can handle Error using try-catch but it is useless as you cannot predict chance of occurring of an Error such as JAVA OutOfMemory.

The following diagram explains hierarchy order






Throwable is at top.
Two subclasses of it are Error and Exception.
We only deal with Exception excluding Runtime Exceptions.

Note:You can catch Error and Runtime Exceptions but it is advised not to as it is difficult to guess where and when they could occur.

 To deal with Exceptions you must remember this basic rule in JAVA that Exceptions must be declared or handled.

Exceptions are of two types.

 Checked Exceptions :

Include all subtypes of Exception, excluding classes that extend RuntimeException.
 Checked exceptions are subject to the handle or declare rule; any method that might throw a checked exception (including methods that invoke methods that can throw a checked exception) must either declare the exception using throws, or handle the exception with an appropriate try/catch.In case of Checked Exceptions compiler strictly follows handle or declare rule.


 Unchecked Exceptions :

Subtypes of Error or RuntimeException are unchecked, so the compiler doesn't enforce the handle or declare rule. You're free to handle them, or to declare them, but the compiler doesn't care one way or the other.


To deal with Exception and Error handling we use try-catch and finally blocks.

Basic definitions of try-catch and finally

try-Suspicious code is put within try block.
catch-contains code to executed for certain suspected Exceptions.
finally- code within it will be executed always whether exception occurs or not.


Sample code

import java.lang.Exception;
public class TestClass{
public static void main(String[] args)
{
try{
int d=5/0;
}
catch(Exception e) //Exception can catch any Exception but //always try be to specific
{
System.out.println(""+e.getMessage());  //This prints message if //any
}
finally{
System.out.println("Hello"); //This will run //always whether exception occured or not
}
}
}

Output will be 
/ by zero
Hello



Rules for try-catch block:
1.try must be followed either by catch or finally ,or both.
2.finally will be executed always whether exception occurs or not.
3.finally can only be interrupted by System.exit(0).
4.It is legal to throw exceptions in catch or finally block.
5.If multiple Exceptions are thrown then only Exception thrown at last is reported.



Ducking (or passing the buck !)
First consider the example below :

For point 5 of try-catch and ducking consider the following example:

import java.lang.Exception;
public class tilak{

public static void main(String[] args) throws Exception  //Exception is ducked till main
{
try{
tilak a=new tilak();
a.dos();
}
catch(Exception e){
throw new Exception("Hi");   //this is thrown second within catch block
}
finally{
throw new Exception("Hello");  //this is thrown last within finally block and it is reported
}
}
void dos() throws Exception{
throw new Exception("");    //this is thrown first within method
}

}

Output

Exception in thread "main" java.lang.Exception: Hello
at tilak.main(tilak.java:14)



You must be wondering why in the above example,I used public static void main(String[] args) throws Exception.
This is known as ducking as it is basic rule in java that Exception must be declared or handled.When you handle it then it is OK but when you declare it you say to caller that beware ! it can cause trouble.So what caller can do is either handle the trouble or pass it's trouble to it's own caller and like this each caller will pass the trouble to it's caller(ducking) and so on until passing reaches to main if main also passes it to jvm then program will shut.




Note: throw which we used in above example can only throw Throwable and it's subclasses.


Note :for a method throwing exceptions intentionally it's return type is useless.
Consider the following example.

import java.lang.Exception;
public class tilak{

public static void main(String[] args) throws Exception
{
try{
tilak a=new tilak();
a.dos();
}
catch(Exception e){
throw new Exception("Hi");
}

}
int dos() throws Exception{
return 0;
throw new Exception("");  //unreachable
}

}

OUTPUT
tilak.java:17: error: unreachable statement
throw new Exception("");
^
1 error


Consider this

import java.lang.Exception;
public class tilak{

public static void main(String[] args) throws Exception
{
try{
tilak a=new tilak();
a.dos();
}
catch(Exception e){
throw new Exception("Hi");
}

}
int dos() throws Exception{

throw new Exception("");
return 0;  //unreachable
}

}


OUTPUT

tilak.java:18: error: unreachable statement
return 0;
^
1 error



Thursday, 27 February 2014

Comparator vs Comparable

To those of you who do not about Comparable and Comparator these are interfaces provided to sort objects of a given class.

Comparator vs Comparable in Java


Here are some of the common differences,

1) Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package, which very much says that Comparator should be used as an utility to sort objects which Comparable should be provided by default.

2) Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

3) If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified. I have shared lot of tips on how to override compareTo() method and avoid some common mistakes programmer makes while implementing Comparable interface.

4) Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implements Comparable interface.Its always good practice to override compareTo() for value objects.

5) If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using  Collections.sort() or Arrays.sort() method and object will be sorted based on there natural order defined by CompareTo method.

6)Objects which implement Comparable in Java  can be used as keys in a SortedMap like TreeMap or elements in a SortedSet for example TreeSet, without specifying any Comparator.

So which one we should use?



That depends on the problem you are dealing with.If you have a class whose instances store information about employee.And if you want to sort these objects on the basis of employee id than Comparable would be enough but  if we look at other side of this problem you might ask that hey can't we sort them with salary or any other basis and also provide user with the option so that he can provide the sort parameter.This is where Comparator comes into picture .With Comparator implemented on a class user can pass parameter saying okay now i want to sort these objects by salary or retirement year.


Example using Comparator

import java.util.*;
public class sort{
static ArrayList<DvdInfo> list=new ArrayList<DvdInfo>();
sort()
{
DvdInfo a=new DvdInfo("Batman Begins "," Christian Bale");
DvdInfo b=new DvdInfo("The Dark Knight "," Aaron Eckhart");
list.add(a);
list.add(b);
}
public static void main(String[] args)
{
sort dummy=new sort();
title t=new title();
Collections.sort(list,t);
System.out.println(list.toString());
actor a=new actor();
Collections.sort(list,a);
System.out.println(list.toString());
}
}
class DvdInfo{
private String title;
private String actor;
DvdInfo(String a,String b)
{
title=a;
actor=b;

}
String getTitle()
{
return title;
}
String getActor()
{
return actor;
}
public String toString(){
return title+""+actor;
}
}
class title implements Comparator<DvdInfo>{
public int compare(DvdInfo a,DvdInfo b)
{
return a.getTitle().compareTo(b.getTitle());
}
}
class actor implements Comparator<DvdInfo>{
public int compare(DvdInfo a,DvdInfo b)
{
return a.getActor().compareTo(b.getActor());
}
}

Output is

[Batman Begins  Christian Bale, The Dark Knight  Aaron Eckhart]
[The Dark Knight  Aaron Eckhart, Batman Begins  Christian Bale]



Example using Comparable

import java.util.*; 
public class sam{ 
public static void main(String[] args) 

Test a=new Test();    //Object a
Test b=new Test();   //Object b
a.c=-50;                 //Assigning variable c of object a to -50
b.c=55; //Assigning variable c of object b to 55

int x=b.compareTo(a);   //Comparing a and b 
System.out.println(""+x); 


class Test implements Comparable{ 
Integer c; 
public int compareTo(Object t) 

Integer v=10; 
return c.compareTo(v);   //this is a test condition

}
Output will be 

1



Wednesday, 26 February 2014

.hashCode() :Introduction


The hashcode was introduced in java for comparison of the objects in java.
To compare two or more objects using the .equals() method their hash code must be same.
Yeah it's true in our previous blog we discussed about the differences between == and .equals().
The one step ahead this concept will give you a sneak peek into the background working of .equals().
So what exactly is hashcode ?
Hash Codes are used in data sorting.In data sorting we assign a hash code to every object if two objects have same hash code they are put together in same tuple.
Consider this example ,
We are using a simple hash code algorithm.
Note: This is not a real hashcode algorithm it is just an example as real hashcode algorithms are way tougher.
Suppose we want to store names Alex,May and Amy.


So, hashcode


ALEX=1+12+5+24= 42


MAY=13+1+25= 39


AMY=1+13+25= 39
So the names having same hash codes are put together in same tuple.
So May and Amy are in same tuple but Alex will be in another tuple.
So,when you search about May it will calculate it's hash code which is 37 so it will start searching in the tuple which is marked 37.
It is also true that you can override the hash code and equals of any class.
But remember to maintain the consistency of the program the overriding must be such that it follows our basic rule i.e two objects are equal if their hash code id equal.
These are some of the basics of hash codes they have a large application in data sorting in Array Lists etc.