Spring Autowiring

Written by admin on . Posted in Spring, Uncategorized

Spring container autowire relationship between different beans. Whenever we create a bean in Spring (In configuration file). Spring will check the dependency of that bean and inject the dependency @ run-time  Spring will identify @ run-time which bean to inject by different mechanism, It is called Autowiring.

SQLifying NoSQL – Are ORM tools relevant to NoSQL?

Written by amresh on . Posted in Uncategorized

Introduction

If you reached this page, it’s fair to assume that you must have worked on at least one relational database in your lifetime. They have been in use for a quarter of a century and are found in almost all business applications.

But, NoSQL databases are gaining traction these days. they are often called “Not only SQL” databases. It’s an umbrella term for a loosely defined class of non-relational data-stores.

They exhibit following main characteristics:

  • They don’t use SQL as their query language.
  • They may not give full ACID guarantees.
  • They have distributed, fault-tolerant architecture.

In this article, I am going to explore whether ORM tools (whatever they are) make sense in NoSQL world…and whether they will be able to solve problems that are NoSQL specific. Next we’ll delve into approaches and challenges in making such a tool. This article assumes you are already familiar with and have worked on one (and possibly more) NoSQL database.

ORM Solutions

ORM (Object Relational Mapping) solutions came into existence to solve OO-impedance mismatching problem. Most popular among them are Hibernate, Toplink, EclipseLink etc. They worked beautifully with relational databases like Oracle and MySQL, among others.

Each ORM solution had its own API and object query language (like HQL for hibernate) which made it difficult for programmers to switch from one framework to another. As a result, efforts were made to make standards and specifications.

 Most popular ORM Standards are:

  • EJB – Enterprise Java Beans (Entity Beans to be specific)
  • JPA – Java Persistence API
  • JDO – Java Data Objects
  • SDO – Service Data Objects

 

Problems in working with NoSQL

Problem with NoSQL databases is that there is NOT EVEN ONE existing industry standard (like SQL) for them. The very basic idea of “something opposed to SQL”…and as a result – deviation from standards and rules, is going to be suicidal, if not corrected at right time. Learning to work with a new NoSQL database is always cumbersome as a result.

Apart from that, people lack in-depth knowledge of NoSQL. Even if they do, they are confined to one or two. In relational world, people depend upon their knowledge of SQL and JDBC to work on basic and intermediate database things. Switching to another database requires little or almost no effort, which otherwise is painful in NoSQL world.

ORM for NoSQL?

ORM for NoSQL is a bit mis-leading term. People prefer to call it “OM tool for NoSQL” or maybe “ODM – Object data-store Mapping tool”. ORM frameworks have already been there for 30+ years and it’s a de-facto industry standard. People are very clear about what ORM tools are supposed to do. There are no surprises.

Key here is to let people forget worrying about complexities inherent in NoSQLs. Let them do things in a way they already know and are comfortable with. Why not use an approach that is there for this problem domain for decades and has proven its usefulness.

A good use case advocating use of ORM tools is migration of applications (built using ORM tool) from RDBMS to NoSQL database. (or even from one NoSQL database to another). This requires (at least in theory) little or no programming effort in business domain.

Challenges in Making ORM Solution

Here are some real challenges that ORM solution providers are going to face:

  • Making one solution for many common-looking problems requires some really tough generalizations and abstractions. Design is going to be a challenge as abstractions sometimes snatch away (or at least makes it difficult to put in front) many powers of NoSQL.
  • NoSQL databases are built for performance and scalability. ORM tools have to employ techniques that don’t put too much burden on performance which users would otherwise get while using plain vanilla driver available with the database.
  • Many ORM standards (like JPA and JDO) were written for relational databases that don’t always provide ways of doing certain things in NoSQL. In fact, most of the ORM tools (like hibernate) were built to solve only 80% of the frequently used mapping problems. Low level tweaking may prove to be crucial for applications built on NoSQL databases. Challenge here is to provide developers hooks for solving remaining 20% of the mapping problem.
  • Each NoSQL database has its own architecture and network topology. They make specific assumptions in data center, racks and disks connectivity and how data is stored and distributed on them. In simple terms – NoSQL databases are “born distributed”. It’s a real challenge to provide all these configuration using ORM specifications.

Approach

There are some guidelines worth sharing, that helps develop ORM tools for NoSQL:

  • Map notations in ORM framework to structures in NoSQL that they are most likely to be thought related to. Example is @Embedded in JPA with Super column in Cassandra.
  • If framework provides a way to operate on database directly, leverage them. Example is mapping Native queries in JPA with CQL in Cassandra.
  • NoSQL is built for performance. noticeable overhead over plain drivers is going to be a big turn-off for users. Make special efforts to keep overhead of ORM to minimum even if that requires sleepless nights and weeks of design-discussion fights.

Other Benefits

There are many other benefits of using a standard ORM tool over plain low-level driver library:

  • Ease of use, faster development, increased productivity.
  • NoSQL databases don’t provide mechanism to maintain relationship between tables. In real life though, business objects or entities do have relationship among them. ORM solutions may allow you to define these relationships in business objects and handle their storage and retrieval behind the scene.
  • ORM solutions (like Kundera and SpringData) may provide polyglot persistence transparently that would otherwise be impossible. This may prove to be a boon for complex application requiring storage in multiple databases.
  • Most NoSQL databases lack transaction management capabilities as they were not built that way (and because they were built to solve problem that didn’t require it at the first place). Many ORM specifications (like JPA) mandate this capability. If by any chance your application requires likes of atomicity, they are going to be your rescuer.

As it happens with everything in life, Discipline and Rules that you promise to yourself, pay in the longer run…Those NoSQLs that provide best of balance in features and ease of use are going to be successful eventually. ORM tools could be one facilitator in that pursuit.

[author]

Hi, I am Amresh. I work for R&D lab of Software Product Engineering firm Impetus Technologies.

I am technology enthusiast and love reading…mostly news and magazine articles. I am a fitness freak and love watching movies and listening to music. My Blog

[/author]

Your first Hadoop Map-Reduce Job

Written by amresh on . Posted in Uncategorized

Introduction

Hadoop Map-Reduce is a YARN-based system for parallel processing of large data sets. If you are new to hadoop, first visit here. In this article, I will help you quickly start with writing the simplest Map-Reduce job. This is a famous “Wordcount” MR job and the first one for 90% of the people (if not more).

WordCount is a simple application that counts the number of occurrences of each word in a given input set.

This code example is from MapReduce tutorial available here. You can checkout source code directly from this small Github project I created.

Step 1. Install and start Hadoop server

In this tutorial, I assume your hadoop installation is ready. For Single Node setup,visit here.

Start Hadoop

amresh@ubuntu:/home/amresh$ cd /usr/local/hadoop/
amresh@ubuntu:/usr/local/hadoop-1.0.2$ bin/start-all.sh
amresh@ubuntu:/usr/local/hadoop-1.0.2$ sudo jps

6098 JobTracker
8024 Jps
5783 DataNode
5997 SecondaryNameNode
5571 NameNode
6310 TaskTracker

 (Make sure NameNode, DataNode, JobTracker, TaskTracker, SecondaryNameNode are running)

Step 2. Write Map-Reduce Job for Wordcount

Map.java (Mapper Implementation)

package com.impetus.code.examples.hadoop.mapred.wordcount;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
public class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable>
{
 private final static IntWritable one = new IntWritable(1);

private Text word = new Text();

public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter)
 throws IOException
 {
 String line = value.toString();
 StringTokenizer tokenizer = new StringTokenizer(line);
 while (tokenizer.hasMoreTokens())
 {
 word.set(tokenizer.nextToken());
 output.collect(word, one);
 }
 }
}

 Reduce.java (Reducer Implementation)

package com.impetus.code.examples.hadoop.mapred.wordcount;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;

public class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable>
{
 public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output,
 Reporter reporter) throws IOException
 {
 int sum = 0;
 while (values.hasNext())
 {
 sum += values.next().get();
 }
 output.collect(key, new IntWritable(sum));
 }
}

 WordCount.java (Job)

package com.impetus.code.examples.hadoop.mapred.wordcount;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;

public class WordCount
{
public static void main(String[] args) throws Exception
 {
 JobConf conf = new JobConf(WordCount.class);
 conf.setJobName("wordcount");

conf.setOutputKeyClass(Text.class);
 conf.setOutputValueClass(IntWritable.class);

conf.setMapperClass(Map.class);
 conf.setCombinerClass(Reduce.class);
 conf.setReducerClass(Reduce.class);

conf.setInputFormat(TextInputFormat.class);
 conf.setOutputFormat(TextOutputFormat.class);

FileInputFormat.setInputPaths(conf, new Path(args[0]));
 FileOutputFormat.setOutputPath(conf, new Path(args[1]));

JobClient.runJob(conf);

 }
}

Step 3. Compile and Create Jar file

I prefer maven for building my java project. You can find POM file here and add to your java project. This will make sure you have Hadoop Jar dependency ready.

Just Run:

amresh@ubuntu:/usr/local/hadoop-1.0.2$ cd ~/development/hadoop-examples
amresh@ubuntu:/home/amresh/development/hadoop-examples$ mvn clean install

 Step 4. Create input files to copy words from

amresh@ubuntu:/usr/local/hadoop-1.0.2$ bin/hadoop dfs -mkdir ~/wordcount/input
amresh@ubuntu:/usr/local/hadoop-1.0.2$ sudo vi file01 (Hello World Bye World)
amresh@ubuntu:/usr/local/hadoop-1.0.2$ sudo vi file02 (Hello Hadoop Goodbye Hadoop)
amresh@ubuntu:/usr/local/hadoop-1.0.2$ bin/hadoop dfs -copyFromLocal file01 /home/amresh/wordcount/input/
amresh@ubuntu:/usr/local/hadoop-1.0.2$ bin/hadoop dfs -copyFromLocal file02 /home/amresh/wordcount/input/
amresh@ubuntu:/usr/local/hadoop-1.0.2$ bin/hadoop dfs -ls /home/amresh/wordcount/input/

Found 2 items
-rw-r--r-- 1 amresh supergroup 0 2012-05-08 14:51 /home/amresh/wordcount/input/file01
-rw-r--r-- 1 amresh supergroup 0 2012-05-08 14:51 /home/amresh/wordcount/input/file02

 Step 5. Run Map-Reduce job you wrote

amresh@ubuntu:/usr/local/hadoop-1.0.2$ bin/hadoop jar ~/development/hadoop-examples/target/hadoop-examples-1.0.jar com.impetus.code.examples.hadoop.mapred.wordcount.WordCount /home/amresh/wordcount/input /home/amresh/wordcount/output
amresh@ubuntu:/usr/local/hadoop-1.0.2$ bin/hadoop dfs -ls /home/amresh/wordcount/output/

Found 3 items
-rw-r--r-- 1 amresh supergroup 0 2012-05-08 15:23 /home/amresh/wordcount/output/_SUCCESS
drwxr-xr-x - amresh supergroup 0 2012-05-08 15:22 /home/amresh/wordcount/output/_logs
-rw-r--r-- 1 amresh supergroup 41 2012-05-08 15:23 /home/amresh/wordcount/output/part-00000

amresh@ubuntu:/usr/local/hadoop-1.0.2$ bin/hadoop dfs -cat /home/amresh/wordcount/output/part-00000

Bye 1
Goodbye 1
Hadoop 2
Hello 2
World 2

 

Hi, I am Amresh. I work for R&D lab of Software Product Engineering firm Impetus Technologies.

I am technology enthusiast and love reading…mostly news and magazine articles. I am a fitness freak and love watching movies and listening to music. My Blog

 

Disclaimer

Written by admin on . Posted in Uncategorized

This website is owned and controlled by Javabeginnertutorial.com. The site reserves the right to alter the content of this website in any way, at any time and for any reason, with or without prior notification, and will not be liable in any way for possible consequences of such changes or for inaccuracies, typographical errors or omissions in the contents hereof. Javabeginnertutorial.com may revise this Disclaimer at any time by updating this notice.

Javabeginnertutorial.com does not accept any liability arising out of the application or use of any information provided herein. Javabeginnertutorial.com shall not be liable for any loss or damage arising from, or in any way connected with, your uses of this website of information contained or derived from it, whether such losses foreseen, foreseeable, known or otherwise. This includes, without limitation, any damage for loss of profits, loss of data, loss of business, loss of reputation, loss of opportunity, loss of good will or injury to reputation, losses suffered by third parties or any indirect, consequential, special or exemplary damages.

This tutorial is not meant to be considered the definitive source for learning Java. It is not intended to be a substitute for Text books, professional instructions and/or directions. It should be used rather as a guide and starting point for learning Java. Javabeginnertutorial.com does not guarantee that all information contained herein is accurate. If you see an error, please send an email Javabeginnertutorial.com through Feedback section indicating the error.

Javabeginnertutorial.com shall not take responsibility for the availability or content of any third party websites or resources to which it has hyperlinks and might not endorse or approve any material on websites linked from to this website.

Please read Terms of Use

BY USING THIS WEBSITE, YOU ACKNOWLEDGE AND AGREE THAT YOU HAVE READ DISCLAIMER, UNDERSTOOD AND AGREE TO TERMS OF USE.

Hello world!

Written by admin on . Posted in Uncategorized

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

package springapp.web;

import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.util.Date;

public class HelloController implements Controller {

    protected final Log logger = LogFactory.getLog(getClass());

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String now = (new Date()).toString();
        logger.info("Returning hello view with " + now);

        return new ModelAndView("WEB-INF/jsp/hello.jsp", "now", now);
    }

}