Archive

Archive for September, 2012

How MVC belongs to only Presentation Layer?

September 16, 2012 4 comments

Spring MVC in simple terms:

It is a presentation framework, you may have doubt “How MVC belongs to only presentation layer”.

MVC Framework:

View and Controller belongs to Presentation Layer as Controller takes the URL and directs to appropriate View based on the given URL.

 MVC Flow:

  1. Request comes to the controller, controller decides is there any business logic to be implemented for this request or not.
  2.  If no business logic required it will redirect to the appropriate View. Ex: if we are requesting a login form no business logic required here so controller directly redirect login.jsp.
  3. If business logic required to the particular request it will call the appropriate Service class to process the request. Based on the service object response, controller redirects to the appropriate view.
  4. In the view if there is any data is to be shown, then the data will be taken from appropriate model.

With the above diagram View and Controller directly interacting with Presentation Layer.

And model is used by View to show the data.

So MVC belongs to Presentation Layer

MVC in Three tier architecture:

In three tier architecture we have Presentation Tier, Middle Tier, Data Access Tier, simply we can call Presentation layer, Service layer  and DAO Layer.

Presentation layer interacts with the Service layer and Service layer interacts with DAO layer.

The Flow will be like this:

  1. Request  comes to controller and controller decides whether this request needs any business logic to be implemented or not
  2. If no it simply returns a view.
  3. If yes it interacts with Service Layer and Service Layer interacts with DAO Layer
  4. Later the based on the response from Service Layer appropriate View will be shown by the Controller.

Spring recommends the same model can be used for presentation layer, In Struts we have form beans as presentation layer model. But in Spring we can use the same model across all the Three Layers.

Advertisements
Categories: Java, Spring

Inversion of Control – Dependency Injection explained in simple terms

September 15, 2012 3 comments

Inversion of Control – Dependency Injection:

Instead of Object creating its dependencies, let container create and inject into Object. Control is being inverted from Object to Container.

Encapsulation says “The Caller should know its dependencies”

IOC  says “The Caller should know its dependency interfaces”

If the caller doesn’t know its dependencies and knows only interfaces, we can give any of the class to the Object.  Will explain with an example

Let’s Say we have a

package com.java2practice;
public class UsersListFromDB{
     public void printUsers(){
System.out.println(“printing users from database”);
     }
}

 

And UsersView to use the UsersListFromDB,

package com.java2practice;
public class UsersView{
 private UsersListFromDB userListFromDB = new UsersListFromDB();
 public void printUsers(){
      userListFromDB.printUsers();
 }
}

Now create a client program to access the UsersView.java

package com.java2practice;
public class UsersViewTest{
 public static void main(String[] args){
      UsersView usersView = new UsersView();
      usersView.printUsers();
 }
}

If we use like this, the object  UsersListFromDB is tightly coupled with UsersView. If we want to show UsersListFromExcel it won’t be possible.

For this we need to create an interface called

public interface UsersList{
       public void printUsers();
}

And make the UsersListFromDB, UsersListFromExcel implement the UsersList like below

public class UsersListFromDB implements UsersList{
       public void printUsers(){
System.out.println(“users list printing from database”);
       }
}

public class UsersListFromExcel implements UsersList{
       public void printUsers(){
System.out.println(“users list printing from excel”);
       }
}

Now we can change our UsersView class to

public class UsersView{
       private UsersList userList = new UsersListFromDB();
       public void printUsers(){
userList.printUsers();
       }
}

This concept is called interaface referencing. One of the object oriented principle Program to interface not program to class”

But still our problem is not solved, with out changing the UsersView we can not add UsersListFromExcel

Now We will try with Factory Design pattern

public class UsersFactory{
       public UsersList getUsersList(){
                 return new UsersListFromDB();
       }
}

In the UsersView.java we need to call this UsersFactory to get the appropriate object.

public UsersView{
       UsersList usersList = new UsersFactory().getUsersList();
       public void printUsers(){
usersList.printUsers();
       }
}

Now the client program will look like this


public class UsersViewTest{
       public static void main(String[] args){
              UsersView usersView = new UsersView();
              usersView.printUsers();
       }
}

This is more elegant and with out touching UsersView.java file we can switch to UsersListFromExcel  or UsersListFromDB by changing UsersFactory.

But still we need to change the UsersFactory.

With spring IOC our UsersView.java will look like this

public class UsersView{
       private UsersList usersList;
       public void setUsersList(UsersList usersList){
              this.usersList = usersList;
       }
       public void printUsers(){
             usersList.printUsers();
       }
}

That’s it, we don’t need to bother about instantiating the UsersList interface with its implemented classes.

To tell the container about our Java classes we need to write configuration file.

Bean-config.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

      <bean id="usersListFromDB" class="com.java2practice.UsersListFromDB"/>
      <bean id="usersListFromExcel" class="com.java2practice.UsersListFromExcel/">
      <bean id="userView">
<property ref=” usersListFromDB” />
      </bean>
</beans>

We have to write our client program to use all these:

public class UesrsViewTest{
       public static void main(String[] args){
              ApplicationContext context = new ClassPathXmlApplicationContext("bean-config.xml");
              UsersView usersView = (UsersView)context.getBean(“userView”);
              usersView.printUsers();
       }
}

Here ApplicationContext will read the bean configuration file and accordingly it will inject the dependencies into the UsersView.java

When we are calling context.getBean(“userView”) it will return UsersView with dependencies injected into it.

If we want to use UsersListFromExcel, just we need to change in configuration file

Then the out put will be “printing users from excel file”

Categories: Spring

Creating a HBase table through Java program

September 11, 2012 2 comments

To create a HBase table through Java program. We need to use HBaseConfiguration class

this is the main class which holds the configuration details about HBase.

then we have to add our hbase-site.xml to the HBaseConfiguration.
After preparing the configuration correctly pass this to HBaseAdmin. To specify the HBase table name HTableDescriptor will be used and

HColumnDescriptor is used for Column names.

After all set up is done call the createTable method of HBaseAdmin


import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;

public class HbaseTableCreator {

  public static void main(String[] args) throws Exception {

   HBaseConfiguration conf = new HBaseConfiguration();
   conf.addResource("conf/hbase-site.xml");
   conf.set("hbase.master","localhost:60000");

   HBaseAdmin hbase = new HBaseAdmin(conf);
   HTableDescriptor desc = new HTableDescriptor("sample");
   HColumnDescriptor meta = new HColumnDescriptor("samplecolumn1".getBytes());
   HColumnDescriptor prefix = new HColumnDescriptor("samplecolumn2".getBytes());
   desc.addFamily(meta);
   desc.addFamily(prefix);
   hbase.createTable(desc);

 }

}

After successful running of the above program, check with hbase shell whether the table is created or not.

Categories: HBase, Java

HBase installation on Ubuntu

September 11, 2012 Leave a comment

Steps to install HBase on Ubuntu Machine:

  1. Download latest Hbase  from http://hbase.apache.org/
  2. extract it to hbase
  3. Set JAVA_HOME variable in conf/hbase-env.sh
  4. Add hbase/bin to PATH variable
  5. go to hbase folder
  6. $bin/hbase  if it is giving some thing means hbase installed correctly

Start and Stop the HBase:

  1. Start the hbase : $bin/hbase start-hbase.sh
  2. Stop the hbase : $bin/hbase stop-hbase.sh // this will take time and remember stop the hadoop first and then stop the hbase.

HBase Shell Commands:

  1. $bin/hbase shell run this command to enter into hbase shell
  2. To see list of tables created in hbase $bin/hbase list
  3. To create a table in hbase  $create ‘test’,’cf’
  4. Insert some values into the above created test $put ‘test’,’row1’,’cf:a’,’value1’
  5. To see the data $ scan ‘test’
  6. To get one row from the hbase table $ get ‘test’,’row1’.

To Delete Hbase table:

  1. after disabling we can able to drop it $disable ‘test’ and then $drop ‘test’

Hope it helps.

Thanks.

Categories: HBase