Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Friday, October 6, 2017
Saturday, August 8, 2015
Sunday, January 4, 2015
Google Gauva API in a one glance: Objects Class to Implement hashcode, equals, toString and compareTo methods
Objects class helps the developer accurately and easily implement the equals(), hashCode(),toString(), and compareTo() methods. it gives the developer to provide implementation of these utility method in more clean and compact way.Developer need not to worry about the clumsy implementation involving variables and nulls.
lets demonstrate it by taking out Employee Class and override these methods with the help of Objects class
lets demonstrate it by taking out Employee Class and override these methods with the help of Objects class
package com.rajkrrsingh.test.guava;
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Objects;
import com.google.common.collect.ComparisonChain;
public class Employee implements Comparable<Employee>{
private String empid;
private String empName;
private int salary;
private int age;
private static List<Employee> list;
static{
list = new ArrayList<Employee>();
list.add(new Employee("101", "RKS", 10000, 31));
list.add(new Employee("102", "Derek", 10500, 35));
list.add(new Employee("103", "Jack", 9000, 29));
list.add(new Employee("104", "Nick", 9600, 35));
}
public static List<Employee> getEmployeeList(){
return list;
}
public Employee(){}
public Employee(String empid,String empName,int salary,int age){
this.empid = empid;
this.empName = empName;
this.salary = salary;
this.age = age;
}
public String getEmpid() {
return empid;
}
public void setEmpid(String empid) {
this.empid = empid;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return Objects.hashCode(empid,empName);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Employee)) {
return false;
}
Employee emp = (Employee) obj;
return Objects.equal(this.empid, emp.empid) && Objects.equal(this.empName, emp.empName);
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("empid", empid).add("empName", empName).toString();
}
@Override
public int compareTo(Employee o) {
// ComparisionChain
return ComparisonChain.start().compare(empid, o.empid)
.compare(empName, o.empName)
.compare(salary, o.salary)
.compare(age, o.age)
.result();
}
}
now test our implementation using out tester class/**
*
*/
package com.rajkrrsingh.test.guava;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
/**
* @author rks
* @04-Jan-2015
*/
public class GuavaObjectsDemo {
public static void main(String[] args) {
objectClass();
cleanComapreToTest();
}
// see the compareTo implementation in Employee class
public static void cleanComapreToTest(){
Set<Employee> set = new HashSet<Employee>();
// print true
System.out.println(set.add(new Employee("101", "RKS", 10000, 31)));
// print false - duplicate object
System.out.println(set.add(new Employee("101", "RKS", 10000, 31)));
}
// Object class with the helper method to implement hashcode() equals() and toString() methods
public static void objectClass(){
Employee e = new Employee("105", "Tom", 80000, 24);
// toString test
System.out.println(e);
Employee e1 = new Employee("105", "Tomm", 80000, 24);
System.out.println(e1.equals(e));
}
}
Google Gauva API in a one glance: CharMatcher
It's a class similar to Predicate,apply a boolean check on the chars, have methods to work on the char sequence e.g.
removeFrom(), replaceFrom(), trimFrom(), collapseFrom(), retainFrom()
lets see a simple example involving CharMatcher, follow along the code here
removeFrom(), replaceFrom(), trimFrom(), collapseFrom(), retainFrom()
lets see a simple example involving CharMatcher, follow along the code here
/**Output
*
*/
package com.rajkrrsingh.test.guava;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
/**
* @author rks
* @04-Jan-2015
*/
public class GuavaCharMatcherDemo {
public static void main(String[] args) {
charMatcher();
}
public static void charMatcher(){
// remove char occurance in the range of 1 and 4
System.out.println(CharMatcher.inRange('1', '4').removeFrom(" 981 654 239"));
// negate the result obtained from previous statement
System.out.println(CharMatcher.inRange('1', '4').negate().removeFrom(" 981 654 239"));
// count no of digit in the string
System.out.println(CharMatcher.DIGIT.countIn(" 981 654 239 "));
// collapse a matching digits with the provided chars
System.out.println(CharMatcher.DIGIT.collapseFrom("collapse from 981 654 239", 'X'));
// replace digit in the string with the provide char
System.out.println(CharMatcher.DIGIT.replaceFrom("collapse from 981 654 239", 'X'));
// trim a string on matching char
System.out.println(CharMatcher.is(' ').trimFrom(" 981 654 239 "));
System.out.println(CharMatcher.is(' ').trimLeadingFrom(" 981 654 239 "));
System.out.println(CharMatcher.is(' ').trimTrailingFrom(" 981 654 239 "));
System.out.println(CharMatcher.is(' ').trimAndCollapseFrom(" 981 654 239 ",'X'));
}
}
98 65 9 1423 9 collapse from X X X collapse from XXX XXX XXX 981 654 239 981 654 239 981 654 239 981X654X239please follow the comment in the code to relate output.
Google Gauva API in a one glance: Joiner
@GwtCompatible public class Joiner extends Object
An object which joins pieces of text (specified as an array,
Iterable, varargs or even a Map) with a separator. It either appends the results to an Appendable or returns them as a String./**Please follow my comments in the code to relate with the output
*
*/
package com.rajkrrsingh.test.guava;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
/**
* @author rks
* @04-Jan-2015
*/
public class GuavaJoinerDemo {
public static void main(String[] args) {
joinerDemo();
}
// tranform a collection into a sting
public static void joinerDemo(){
List<String> list = Arrays.asList("RKS","John","Nick","Harry");
System.out.println(Joiner.on("#").join(list));
List<String> list1 = Arrays.asList("RKS","John",null,"Nick","Harry");
//skip nulls
System.out.println(Joiner.on("#").skipNulls().join(list1));
//defualt value for null
System.out.println(Joiner.on("#").useForNull("BLANK").join(list1));
// joiner on Map
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
map.put("key4", "value4");
System.out.println(Joiner.on("#").withKeyValueSeparator(":").join(map));
}
}
RKS#John#Nick#Harry RKS#John#Nick#Harry RKS#John#BLANK#Nick#Harry key4:value4#key3:value3#key2:value2#key1:value1
Google Gauva API in a one glance: Predicate
- Predicate<T>, which has the single method boolean apply(T input). Instances of Predicate are generally expected to be side-effect-free and consistent with equals.
@GwtCompatible public interface Predicate<T>
Determines a true or false value for a given input.The
Predicates class provides common predicates and related utilities./**Please follow my comments in the code to relate with the output
*
*/
package com.rajkrrsingh.test.guava;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
/**
* @author rks
* @04-Jan-2015
*/
public class GuavaPredicateDemo {
public static void main(String[] args) {
pridcateDemo();
}
public static void pridcateDemo(){
List<Employee> empList = Employee.getEmployeeList();
Predicate<Employee> ageOver30 = new Predicate<Employee>() {
@Override
public boolean apply(Employee emp) {
if(emp.getAge()>30){
return true;
}
return false;
}
};
Predicate<Employee> slryLt10000 = new Predicate<Employee>() {
@Override
public boolean apply(Employee emp) {
if(emp.getSalary()<10000)
return true;
return false;
}
};
System.out.println("**** print emp name whose age is greater than 30 ****");
Iterator<Employee> filterAgeIterator= Iterables.filter(empList, ageOver30).iterator();
while(filterAgeIterator.hasNext()){
System.out.println(filterAgeIterator.next().getEmpName());
}
System.out.println("**** print emp name whose age is greater than 30 and salary is less than 10000 ****");
Iterator<Employee> filterOnAgeAndSal= Iterables.filter(empList, Predicates.and(ageOver30, slryLt10000)).iterator();
while(filterOnAgeAndSal.hasNext()){
System.out.println(filterOnAgeAndSal.next().getEmpName());
}
System.out.println("**** print emp name whose age is greater than 30 OR salary is less than 10000 ****");
Iterator<Employee> filterOnAgeORSal= Iterables.filter(empList, Predicates.or(ageOver30, slryLt10000)).iterator();
while(filterOnAgeORSal.hasNext()){
System.out.println(filterOnAgeORSal.next().getEmpName());
}
}
}
**** print emp name whose age is greater than 30 **** RKS Derek Nick **** print emp name whose age is greater than 30 and salary is less than 10000 **** Nick **** print emp name whose age is greater than 30 OR salary is less than 10000 **** RKS Derek Jack Nick
Tuesday, November 19, 2013
Apache Oozie Workflow : Configure and Running a MapReduce job
In this post I will demonstrate you how to configure the Oozie workflow. let's develop a simple MapReduce program using java, if you find any difficulties in doing it then download the code from my git location.Download
Please follow my earlier post to install and run oozie server, create a job directory say SimpleOozieMR as per following directory structure
---SimpleOozieMR
----workflow
-----lib
------workflow.xml
in the lib folder copy the you hadoop job jar and related jars.
let's configure our workflow.xml and keep it into the workflow directory as shown.
Now configure your properties file PatentCitation.properties as follows
lets create a shell script which will run your first oozie job:
Please follow my earlier post to install and run oozie server, create a job directory say SimpleOozieMR as per following directory structure
---SimpleOozieMR
----workflow
-----lib
------workflow.xml
in the lib folder copy the you hadoop job jar and related jars.
let's configure our workflow.xml and keep it into the workflow directory as shown.
<workflow-app name="WorkFlowPatentCitation" xmlns="uri:oozie:workflow:0.1">
<start to="JavaMR-Job"/>
<action name="JavaMR-Job">
<java>
<job-tracker>${jobTracker}</job-tracker>
<name-node>${nameNode}</name-node>
<prepare>
<delete path="${outputDir}"/>
</prepare>
<configuration>
<name>mapred.queue.name</name>
<value>default</value>
</configuration>
<main-class>com.rjkrsinghhadoop.App</main-class>
<arg>${citationIn}</arg>
<arg>${citationOut}</arg>
</java>
<ok to="end"/>
<error to="fail"/>
</action>
<kill name="fail">
<message>"Killed job due to error: ${wf:errorMessage(wf:lastErrorNode())}"</message>
</kill>
<end name="end" />
</workflow-app>
Now configure your properties file PatentCitation.properties as follows
nameNode=hdfs://master:8020 jobTracker=master:8021 queueName=default citationIn=citationIn-hdfs citationOut=citationOut-hdfs oozie.wf.application.path=$(namenode)/user/rks/oozieworkdir/SimpleOozieMR/workflow
lets create a shell script which will run your first oozie job:
#!/bin/sh # export OOZIE_URL="http://localhost:11000/oozie" #copy your input data to the hdfs hadoop fs -copyFromLocal /home/rks/CitationInput.txt citationIn-hdfs #copy SimpleOozieMR to hdfs hadoop fs -put /home/rks/SimpleOozieMR SimpleOozieMR #running the oozie job cd /usr/lib/oozie/bin/ oozie job -config /home/rks/SimpleOozieMR/PatentCitation.properties -run
Saturday, November 16, 2013
WebHDFS REST API -- Overview
Hadoop support all the hdfs operations using the underline java implementaion for all the HDFS commands like ls, mkdir, cat, rm, merge etc.
most of the time need arises to access the HDFS from some external applications other than the accessing the Hadoop cluster, the external system can be in any programming language other than the Java.
To support the access of HDFS from external application hadoop provides the WebHDFS REST API, which is based on the commonn http methods like GET,PUT,POST,DELETE. these methods supports the user operations like OPEN, GETFILESTATUS, LISTSTATUS are using HTTP GET, others like CREATE, MKDIRS, RENAME, SETPERMISSIONS are relying on HTTP PUT. APPEND operations is based on HTTP POST, while DELETE is using HTTP DELETE on the HDFS.
WebHDFS REST API OVERVIEW
- HTTP GET
OPEN
(see FileSystem.open)
GETFILESTATUS
(see FileSystem.getFileStatus)
LISTSTATUS
(see FileSystem.listStatus)
GETCONTENTSUMMARY
(see FileSystem.getContentSummary)
GETFILECHECKSUM
(see FileSystem.getFileChecksum)
GETHOMEDIRECTORY
(see FileSystem.getHomeDirectory)
GETDELEGATIONTOKEN
(see FileSystem.getDelegationToken)
- HTTP PUT
CREATE
(see FileSystem.create)
MKDIRS
(see FileSystem.mkdirs)
RENAME
(see FileSystem.rename)
SETREPLICATION
(see FileSystem.setReplication)
SETOWNER
(see FileSystem.setOwner)
SETPERMISSION
(see FileSystem.setPermission)
SETTIMES
(see FileSystem.setTimes)
RENEWDELEGATIONTOKEN
(see DistributedFileSystem.renewDelegationToken)
CANCELDELEGATIONTOKEN
(see DistributedFileSystem.cancelDelegationToken)
- HTTP POST
APPEND
(see FileSystem.append)
- HTTP DELETE
DELETE
(see FileSystem.delete)
In order to configure the WebHDFS, update hdfs-site.xml as follows
<property>
<name>dfs.webhdfs.enabled</name>
<value>true</value>
</property>
now restart your namenode to access the WebHDFS.
in the omming post I will post about the java client to access the WebHDFS REST API... stay tuned
Thursday, November 14, 2013
Monday, November 11, 2013
Mapreduce : Writing output to multiple files using MultipleOutputFormat
Multiple Outputs
FileOutputFormat and its subclasses generate a set of files in the output directory. There is one file per reducer, and files are named by the partition number: part-00000, part-00001, etc. There is sometimes a need to have more control over the naming of the files or to produce multiple files per reducer. MapReduce comes with two libraries to help you do this: MultipleOutputFormat and MultipleOutputs.
MultipleOutputFormat
MultipleOutputFormat allows you to write data to multiple files whose names are derived from the output keys and values. MultipleOutputFormat is an abstract class with two concrete subclasses, MultipleTextOutputFormat and MultipleSequenceFileOutputFormat, which are the multiple file equivalents of TextOutputFormat and SequenceFileOutputFormat. MultipleOutputFormat provides a few protected methods that subclasses can override to control the output filename. In Example 7-5, we create a subclass of MultipleTextOutputFormat to override the generateFileNameForKeyValue() method to return the station ID, which we extracted from the record value.
-- reference Hadoop Definitive guide
In this example I will demonstrate you how to write output data to multiple files.you can find the code of this example on the following git location
we have our sample customer data with attribute customer no,cust name, region, company. we will write the same region customer to the same file along with the other attributes.
Create your Mapper Class as follows:
Setup your reduce class:
setup your driver class as follows:
Create the job jar using maven assembly plugin and run on your hadoop cluster
FileOutputFormat and its subclasses generate a set of files in the output directory. There is one file per reducer, and files are named by the partition number: part-00000, part-00001, etc. There is sometimes a need to have more control over the naming of the files or to produce multiple files per reducer. MapReduce comes with two libraries to help you do this: MultipleOutputFormat and MultipleOutputs.
MultipleOutputFormat
MultipleOutputFormat allows you to write data to multiple files whose names are derived from the output keys and values. MultipleOutputFormat is an abstract class with two concrete subclasses, MultipleTextOutputFormat and MultipleSequenceFileOutputFormat, which are the multiple file equivalents of TextOutputFormat and SequenceFileOutputFormat. MultipleOutputFormat provides a few protected methods that subclasses can override to control the output filename. In Example 7-5, we create a subclass of MultipleTextOutputFormat to override the generateFileNameForKeyValue() method to return the station ID, which we extracted from the record value.
-- reference Hadoop Definitive guide
In this example I will demonstrate you how to write output data to multiple files.you can find the code of this example on the following git location
we have our sample customer data with attribute customer no,cust name, region, company. we will write the same region customer to the same file along with the other attributes.
customer no,customer name,region,company ................................................................. 9899821411,"Burke, Honorato U.",Alaska,Eu Incorporated 9899821422,"Bell, Emily R.",Arizona,Ut Eros Non Company 9899821379,"Hewitt, Chelsea Y.",PA,Egestas Aliquam Fringilla LLP 9899821387,"Baldwin, Merrill H.",VT,Rhoncus Proin Corp. 9899821392,"Bradshaw, Uma H.",OH,Nam Nulla Associates 9899821453,"Pollard, Boris G.",Hawaii,Consequat Corp. 9899821379,"Avila, Velma D.",OR,Sodales LLC
Create your Mapper Class as follows:
package com.rajkrrsingh.mr.hadoop;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class MultipleOutputMapper extends Mapper<LongWritable, Text, Text, Text> {
private Text txtKey = new Text("");
private Text txtValue = new Text("");
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
if(value.toString().length() > 0) {
String[] custArray = value.toString().split(",");
txtKey.set(custArray[0].toString());
txtValue.set(custArray[1].toString()+"\t"+custArray[3].toString());
context.write(txtKey, txtValue);
}
}
}
Setup your reduce class:
package com.rajkrrsingh.mr.hadoop;
import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
public class MultiOutputReducer extends Reducer<Text, Text, Text, Text>{
private MultipleOutputs multipleOutputs;
@Override
protected void setup(Context context) throws IOException, InterruptedException {
multipleOutputs = new MultipleOutputs(context);
}
@Override
protected void reduce(Text key, Iterable<Text> values,Context context)
throws IOException, InterruptedException {
for(Text value : values) {
multipleOutputs.write(key, value, key.toString());
}
}
@Override
protected void cleanup(Context context)
throws IOException, InterruptedException {
multipleOutputs.close();
}
}
setup your driver class as follows:
package com.rajkrrsingh.mr.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.LazyOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class App extends Configured implements Tool
{
public static void main( String[] args ) throws Exception
{
int exitCode = ToolRunner.run(new Configuration(),new App(),args);
System.exit(exitCode);
}
@Override
public int run(String[] args) throws Exception {
if(args.length != 2) {
System.out.println("Two Params are required to extecute App <input-path> <output-path>");
}
Job job = new Job(getConf());
job.setJobName("MultipleOutputFormat example");
job.setJarByClass(App.class);
LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(MultipleOutputMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setReducerClass(MultiOutputReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setNumReduceTasks(5);
boolean success = job.waitForCompletion(true);
return success ? 0 : 1;
}
}
Create the job jar using maven assembly plugin and run on your hadoop cluster
$bin/hadoop jar /home/rks/MultipleOutputExample/target/MultipleOutputExample.jar com.rajkrrsingh.mr.hadoop.App /user/rks/input /user/rks/output
Monday, September 2, 2013
Apache Pig : Writiting Java UDF for Pig
In this tutorial how to write user defined function for the pig.suppose we have a sample data in the for of tab separate document as fallows.the first column depicts name of the customer, second column represent the location of the customer and the third column give the information of customer's credit rating on the scale of 10. we need to filter out the the credit who scored the bad rating e.g. less than 5.
Lets create a Maven java project using the fallowing command:
Now from command line execute
CreditScore.pig
run the scrip using pig CreditScore.pig and get the result.
Amit Noida 5 Ajay Delhi 8 Abhi Lucknow 3 Dev Punjab 7 Deepak Bihar 2
Lets create a Maven java project using the fallowing command:
>mvn archetype:generate -DgroupId=com.rajkrrsingh.pig.udf -DartifactId=JavaUDF -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=falseAbove command will create a new Java project with the name of JavaUDF, open pom.xml in the project directory and add the fallowing dependencies in it.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rajkrrsingh.pig.udf</groupId>
<artifactId>JavaUDF</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>JavaUDF</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- TODO: make sure Hadoop version is compatible -->
<dependency>
<groupId>org.apache.pig</groupId>
<artifactId>pig</artifactId>
<version>0.10.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/jar.xml</descriptor>
</descriptors>
<finalName>pig-examples</finalName>
<outputDirectory>${project.build.directory}/../..</outputDirectory>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Now from command line execute
mvn eclipse:eclipseImport the project in the eclipse using Import from existing project and create a java package and add the fallowing class to it.
package com.rajkrrsingh.pig.udf;
import java.io.IOException;
import org.apache.pig.FilterFunc;
import org.apache.pig.backend.executionengine.ExecException;
import org.apache.pig.data.Tuple;
public class IsGoodCreditRating extends FilterFunc {
@Override
public Boolean exec(Tuple args) throws IOException {
if (args == null || args.size() == 0) {
return false;
}
try {
Object object = args.get(0);
if (object == null) {
return false;
}
int i = (Integer) object;
if(i>5){
return true;
}else{
return false;
}
} catch (ExecException e) {
throw new IOException(e);
}
}
}
Create the jar file using the assembly plugin and moved it to your cluster.In the next step we will write a Pig script.CreditScore.pig
REGISTER JavaUDF.jar; records = LOAD 'sample.txt' AS (name:chararray, location:chararray, creditrating:int); filter_records = FILTER records BY com.rajkrrsingh.pig.udf.IsGoodCreditRating(creditrating); grouped_records = GROUP filter_records BY location; DUMP grouped_records;
run the scrip using pig CreditScore.pig and get the result.
Monday, August 26, 2013
Import TSV file from HFDS to HBase using Identity Mapper
In this tutorial I am going to demonstrate you how to import the tab separated file stored on the HDFS to HBase database.
lets start by creating a table on HBase
Step 1 : Create a table in HBase with the name of orders
with coloum family 'ship_to_address','ord_date','ship_date','item','status','price'
create 'orders','ship_to_address','ord_date','ship_date','item','status','price'
Here is our input tsv file stored on hdfs
USA NY New York 28-07-2013 29-07-2013 Toner shipped 200$
USA California San Fransico 29-07-2013 29-07-2013 Cati in process 150$
USA NY Rochester 28-07-2013 28-07-2013 Toner shipped 200$
USA NY Syracuse 21-07-2013 23-07-2013 Paper shipped 80$
USA NY Albany 21-07-2013 21-07-2013 Paper failed 80$
USA California Long Beach 26-07-2013 28-07-2013 Toner shipped 200$
Step 2 : Write your identity Mapper class as fallows:
public class ImportFromTSVMapper extends
Mapper<LongWritable, Text, ImmutableBytesWritable, Writable> {
@Override
public void map(LongWritable offset, Text line, Context context)
throws IOException {
try {
String lineString = line.toString();
String[] arr = lineString.split("\t");
Put put = new Put(arr[0].getBytes());
put.add("ship_to_address".getBytes(), "country".getBytes(), Bytes.toBytes(arr[1]));
put.add("ship_to_address".getBytes(),"state".getBytes(), Bytes.toBytes(arr[2]));
put.add("ship_to_address".getBytes(),"city".getBytes(), Bytes.toBytes(arr[3]));
put.add("ord_date".getBytes(),"ord_date".getBytes(), Bytes.toBytes(arr[4]));
put.add("ship_date".getBytes(),"ship_date".getBytes(), Bytes.toBytes(arr[5]));
put.add("item".getBytes(),"item".getBytes(), Bytes.toBytes(arr[6]));
put.add("status".getBytes(),"status".getBytes(), Bytes.toBytes(arr[7]));
put.add("price".getBytes(),"price".getBytes(), Bytes.toBytes(arr[8]));
context.write(new ImmutableBytesWritable(arr[0].getBytes()), put);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Step 3 : Write your job Main class to configure MR job
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
public class ImportTSVFile {
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
String table = "order";
String input = "/home/rajkrrsingh/mspfeed/ordfeed";
String column = "";
conf.set("conf.column", column);
Job job = new Job(conf, "Import from hdfs to hbase");
job.setJarByClass(ImportTSVFile.class);
job.setMapperClass(ImportFromTSVMapper.class);
job.setOutputFormatClass(TableOutputFormat.class);
job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE, table);
job.setOutputKeyClass(ImmutableBytesWritable.class);
job.setOutputValueClass(Writable.class);
job.setNumReduceTasks(0);
FileInputFormat.addInputPath(job, new Path(input));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
The result can be verified using HBase console,your tsv file has been imported to the HBase database.
Sunday, March 24, 2013
Importing the tweets from Twitter API to MongoDB
Importing the Tweets from twitter using the twitter api is very simple and straight forward.In the coming example I am importing the Sachin Tendulkar(Legendary Cricketer ) tweets to the MongoDb.Here you can find the URL of json document of Sachin tweets : http://api.twitter.com/1/statuses/user_timeline.json?screen_name=sachin_rt&include_rts=1
Java Program to import the tweets and save it the MongoDb.
Run the Program as Java Application but make sure that you have started the mongod (mongod --dbpath) before running the program,look out for the message import done on the console.
Now check the mongo shell

Java Program to import the tweets and save it the MongoDb.
package com.rajkrrsinghblogspot;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.util.JSON;
public class GetTweets {
public static void main(String[] args) throws IOException {
try {
MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));
DB database = client.getDB("tweetDb");
DBCollection collection = database.getCollection("tweetCollection");
List<DBObject> tweets = getSachinTweets();
for(DBObject doc : tweets){
collection.insert(doc);
}
System.out.println("Import done");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
private static List<DBObject> getSachinTweets() throws IOException {
URL url = new URL("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=sachin_rt&include_rts=1");
InputStream is = url.openStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int endOfStream;
while((endOfStream = is.read()) != -1){
bos.write(endOfStream);
}
String tweetString = bos.toString();
return (List<DBObject>) JSON.parse(tweetString);
}
}
Run the Program as Java Application but make sure that you have started the mongod (mongod --dbpath) before running the program,look out for the message import done on the console.
Now check the mongo shell

Friday, March 22, 2013
Authentication to MongoDb using Java
MongoDB provides basic support for authentication with the auth setting. For multi-instance deployments (i.e. replica sets, and sharded clusters) use the keyFile setting, which implies auth, and allows intra-deployment authentication and operation. Be aware of the following behaviors of MongoDB’s authentication system:
Enable auth settings to MongoDb requires few steps:
Invoke addUser command on mongo shell:

Start the mongod in --auth mode as shown

Check whether auth setting are working

Now test with the java program to authenticate the database in trusted enviornment.
Enable auth settings to MongoDb requires few steps:
Invoke addUser command on mongo shell:

Start the mongod in --auth mode as shown

Check whether auth setting are working

Now test with the java program to authenticate the database in trusted enviornment.
package com.rajkrrsinghblogspot;
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
public class MongoAuth {
public static void main(String[] args) {
try {
MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));
DB database = client.getDB("blog");
boolean auth = database.authenticate("rajkumar", "password".toCharArray());
DBCollection collection = database.getCollection("posts");
if(auth){
System.out.println("Authentication is Successful");
BasicDBObject query = new BasicDBObject().append("author", "rajkumar");
BasicDBObject projection = new BasicDBObject().append("author", true);
DBObject doc = collection.findOne(query,projection);
System.out.println(doc.toString());
}else{
System.out.println("Authentication is UnSuccessful");
}
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Monday, February 18, 2013
Java API for JSON Processing (JSR 353) : JsonGenerator and JsonObjectBuilder
Java API for JSON Processing (JSR 353) cleared Public Review unanimously and is on its way to to standardization. There is still Proposed Final Draft and the final vote to come. As per the Java EE 7 schedule, the specification will be final on 4/15/2013. The implementation is already integrated in GlassFish 4 builds.
The API provides an Object Model (like DOM for XML) and Streaming API (like StAX for XML) to parse and generate JSON structure. Here is a table that provide code fragments for generating some common JSON:
The API provides an Object Model (like DOM for XML) and Streaming API (like StAX for XML) to parse and generate JSON structure. Here is a table that provide code fragments for generating some common JSON:
| JSON | Object Model API | Streaming API |
|---|---|---|
{ } | JsonObject jsonObject =new JsonWriter(System.out) | JsonGeneratorFactory factory =JsonGenerator gen =gen.writeStartObject().writeEnd(); |
{ "apple":"red", "banana":"yellow"} | new JsonObjectBuilder() .add("apple", "red") .add("banana", "yellow").build(); | gen.writeStartObject() .write("apple", "red") .write("banana", "yellow") .writeEnd(); |
[ { "apple":"red" }, { "banana":"yellow" }] | JsonArray jsonArray = new JsonArrayBuilder() .add(new JsonObjectBuilder() .add(new JsonObjectBuilder() .build(); | gen.writeStartArray() .writeStartObject() .write("apple", "red") .writeEnd() .writeStartObject() .write("banana", "yellow") .writeEnd().writeEnd(); |
{ "title":"The Matrix", "year":1999, "cast":[ "Keanu Reaves", "Laurence Fishburne", "Carrie-Anne Moss" ]} | new JsonArrayBuilder() .add(new JsonObjectBuilder() .add("title", "The Matrix") .add("year", 1999) .add("cast", new JsonArrayBuilder() .add("Keanu Reaves") .add("Laurence Fishburne") .add("Carrie-Anne Moss"))).build(); | gen.writeStartObject() .write("title", "The Matrix") .write("year", 1999) .writeStartArray("cast") .write("Keanu Reaves") .write("Laurence Fishburne") .write("Carrie-Anne Moss") .writeEnd() .writeEnd(); |
Sunday, February 17, 2013
Using Python Client with Java webservices (JAX-WS 2.0)
In this tutorial I am going to demonstrate you the development and deployment of the web service using Java JAX-WS API and consuming it with the help of the Python client.
Python provide many packages to support SOAP based web services,we will look one of them named suds to devlop our client.
Suds is a lightweight SOAP python client that provides a service proxy for Web Services.you can download suds from https://fedorahosted.org/suds/
installation of suds is pretty straightforward using fallowing methods
Here are the basic instructions for 3 different installation methods:
Using pip:
* Have the 'pip' package installed.
* Run 'pip install suds-jurko'.
Using easy-install:
* Have the 'distribute' package installed.
* Run 'easy_install suds-jurko'.
From sources:
* Unpack the source package somewhere.
* Run 'python setup.py install' from the source distribution's top level
folder.
Now come to the web service development using JAX-WS.
Step 1: create a dynamic webproject in eclipse.
Step 2: download the JAX-WS api from http://jax-ws.java.net/ after download unzip the binaries in a folder and look for the lib folder inside that,copy all the jars file inside the lib folder to our already created project's WEB-INF\lib folder
Step 3: create a bean with the name of IdealMatch inside the src folder as fallows:
Step 5: provide the implementation for the service interface as shown
Step 6: create one file inside WEB-INF with the name of sun-jaxws.xml and configure it to provide end point,service name,implementation and url information.
Step 7: now configure your web.xml to add listner WSServletContextListener
lets look at the project structure to check whether all components are in place:

Step 8: Now deploy the application on server and access the url http://localhost:8083/MatchMakerService/findMatch?wsdl

it means our service is up and running now we need to have a client to access this service,we are using python client in our application,lets create our python client.
Step 9: create your python client as fallows:

Python provide many packages to support SOAP based web services,we will look one of them named suds to devlop our client.
Suds is a lightweight SOAP python client that provides a service proxy for Web Services.you can download suds from https://fedorahosted.org/suds/
installation of suds is pretty straightforward using fallowing methods
Here are the basic instructions for 3 different installation methods:
Using pip:
* Have the 'pip' package installed.
* Run 'pip install suds-jurko'.
Using easy-install:
* Have the 'distribute' package installed.
* Run 'easy_install suds-jurko'.
From sources:
* Unpack the source package somewhere.
* Run 'python setup.py install' from the source distribution's top level
folder.
Now come to the web service development using JAX-WS.
Step 1: create a dynamic webproject in eclipse.
Step 2: download the JAX-WS api from http://jax-ws.java.net/ after download unzip the binaries in a folder and look for the lib folder inside that,copy all the jars file inside the lib folder to our already created project's WEB-INF\lib folder
Step 3: create a bean with the name of IdealMatch inside the src folder as fallows:
package com.rajkrrsinghblogspot.beans;
import java.io.Serializable;
public class IdealMatch implements Serializable {
private int age;
private double salary;
private String name;
private String address;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Step 4:Now create your service interface and annotate it with @webservice and @webmethod annotations as fallowspackage com.rajkrrsinghblogspot.ws.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import com.rajkrrsinghblogspot.beans.IdealMatch;
@WebService
@SOAPBinding(style = Style.RPC)
public interface IdealMatchFinder {
@WebMethod IdealMatch findIdealMatch();
}
Step 5: provide the implementation for the service interface as shown
package com.rajkrrsinghblogspot.ws.service;
import javax.jws.WebService;
import com.rajkrrsinghblogspot.beans.IdealMatch;
@WebService(endpointInterface="com.rajkrrsinghblogspot.ws.service.IdealMatchFinder")
public class IdealMatchFinderImpl implements IdealMatchFinder {
@Override
public IdealMatch findIdealMatch() {
System.out.println(":::::::: Ideal Match Finder criteria without args ::::::::");
IdealMatch iMatch = new IdealMatch();
iMatch.setName("Ajay Singh");
iMatch.setAge(30);
iMatch.setSalary(1000000);
iMatch.setAddress("Noida");
return iMatch;
}
}
Step 6: create one file inside WEB-INF with the name of sun-jaxws.xml and configure it to provide end point,service name,implementation and url information.
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
<endpoint
name='MatchFinder'
implementation='com.rajkrrsinghblogspot.ws.service.IdealMatchFinderImpl'
url-pattern='/findMatch'/>
</endpoints>
Step 7: now configure your web.xml to add listner WSServletContextListener
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MatchMakerService</display-name>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>MatchFinder</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MatchFinder</servlet-name>
<url-pattern>/findMatch</url-pattern>
</servlet-mapping>
</web-app>
lets look at the project structure to check whether all components are in place:

Step 8: Now deploy the application on server and access the url http://localhost:8083/MatchMakerService/findMatch?wsdl

it means our service is up and running now we need to have a client to access this service,we are using python client in our application,lets create our python client.
Step 9: create your python client as fallows:
import suds
class Client:
def __init__(self):
self.client = suds.client.Client("http://localhost:8083/MatchMakerService/findMatch?wsdl")
def get_ideal_match(self):
return self.client.service.findIdealMatch()
if(__name__ == "__main__"):
client = Client()
idealMatch = client.get_ideal_match()
print idealMatch
save it with the extension of .py and run using python command.After run you will find the service return on the shell
Thursday, January 10, 2013
Hibernate : Pagination of the Data using HQL
Most common way of fetching the database record using HQL is
Query query = session.createQuery(from Employee)
but if your query result in some large data then its going to consume a lot of Memory
and can possibly going to slow your program as it would require to process lots of data
but hibernate provide a convenient approach using HQL pagination through which you can limit the no of records return by the database.
Lets walk through the fallowing code which will give you overview to use HQL as well as the Hibernate pagination.
lets create your Model class as fallows and annotate with it with the javax.persistence
package com.rajkrrsinghblogspot.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="EMP_TABLE")
public class Employee {
@Id
private int empid;
private String empName;
public Employee(){
}
public Employee(int empid,String empName){
this.empid = empid;
this.empName = empName;
}
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String toString(){
return "::EMPID::"+getEmpid()+"::EMPNAME::"+getEmpName()+"::";
}
}
Create Hibernate.cfg.xml as fallows and keep it in the classpath
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="connection.url">jdbc:derby://localhost:1527/MyHibDB;create=true</property>
<property name="connection.username">rajkumar</property>
<property name="connection.password">rajkumar</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="com.rajkrrsinghblogspot.model.Employee"/>
</session-factory>
</hibernate-configuration>
Now Create your main class to persist the database into the database and also retrive the result form database using HQL as fallows
package com.rajkrrsingh;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.rajkrrsinghblogspot.model.Employee;
public class App
{
public static void main( String[] args )
{
System.out.println("Testing my hibernate configuration");
Employee emp = null;
List<Employee> list = new ArrayList<Employee>();
for(int i=0;i<10;i++){
emp = new Employee(i, "Emp"+i);
list.add(emp);
}
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
for(Employee e : list){
session.save(e);
}
session.getTransaction().commit();
session.close();
session = sf.openSession();
session.beginTransaction();
Query query = session.createQuery("from Employee");
query.setFirstResult(1);
query.setMaxResults(3);
List<Employee> empList = query.list();
System.out.println("\n");
System.out.println("Printing 3 results from HQL using Pagination");
System.out.println("\n");
for(Employee em : empList){
System.out.println(em);
}
session.getTransaction().commit();
session.close();
}
}
After running this example you will find the fallowing output on the console
Saturday, January 5, 2013
Hibernate : CRUD Operations on the objects
Here I have demonstrated the basic CRUD operation using Hibernate API
To add records to the database, all you need to do it create an instance of your Model Class, create an instance of the Hibernate SessionFactory, grab an actual Hibernate Session from that factory start a transaction, pass your POJO instance to the save method of the Session, and then commit the transaction.
we can use the get method of the Hibernate Session to pull that instance out of the database, simply by passing in the class type and the primary key as arguments:
The get method of the Hibernate Session will return one entity. If you need to return multiple entities, you create a Hibernate query. you can use a Hibernate query to grab every record in the database as a list.
To delete a record, simply obtain the record from Hibernate using the get method and then pass the obtained object to the delete method of the Hibernate Session, all within an active transaction.
Here is the complete code to obtain all this in same place.
Create your project structure as shown in figure and add the hibernate related configuration to the project if you are not familier with hibernate configuration then fallow this link : Configuration and setting of Hibernate
Configure hibernate.cfg.xml as fallows
Create your Model class and annotate it with javax.persistance annotation
Create HibernateUtil class to expose the SessionFactory when required instead of recreate every time.
Now Create your Main class to test out all the operation.
To add records to the database, all you need to do it create an instance of your Model Class, create an instance of the Hibernate SessionFactory, grab an actual Hibernate Session from that factory start a transaction, pass your POJO instance to the save method of the Session, and then commit the transaction.
we can use the get method of the Hibernate Session to pull that instance out of the database, simply by passing in the class type and the primary key as arguments:
The get method of the Hibernate Session will return one entity. If you need to return multiple entities, you create a Hibernate query. you can use a Hibernate query to grab every record in the database as a list.
To delete a record, simply obtain the record from Hibernate using the get method and then pass the obtained object to the delete method of the Hibernate Session, all within an active transaction.
Here is the complete code to obtain all this in same place.
Create your project structure as shown in figure and add the hibernate related configuration to the project if you are not familier with hibernate configuration then fallow this link : Configuration and setting of Hibernate
Configure hibernate.cfg.xml as fallows
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/deptt</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping class="com.rajkrrsinghblogspot.model.Employee"/> </session-factory> </hibernate-configuration>
Create your Model class and annotate it with javax.persistance annotation
package com.rajkrrsinghblogspot.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="EMPLOYEE")
public class Employee {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="EMPID")
private int empId;
@Column(name="EMP_NAME")
private String empName;
@Column(name="DESIGNATION")
private String designation;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}
Create HibernateUtil class to expose the SessionFactory when required instead of recreate every time.
package com.rajkrrsinghblogspot.sessionfactory;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtilImpl {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFacotry(){
try{
sessionFactory = new Configuration().configure().buildSessionFactory();
}catch(HibernateException he){
he.printStackTrace();
}
return sessionFactory;
}
}
Now Create your Main class to test out all the operation.
package com.rajkrrsinghblogspot.main;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.rajkrrsinghblogspot.model.Employee;
import com.rajkrrsinghblogspot.sessionfactory.HibernateUtilImpl;
public class MainClass {
public static void main(String[] args) {
//createEmployee();
//readEmployee();
//updateEmployee();
deleteEmployee();
}
private static void createEmployee() {
Employee emp = new Employee();
emp.setEmpName("Amit Singh");
emp.setDesignation("Managar");
Employee empp = new Employee();
empp.setEmpName("Vijay Singh");
empp.setDesignation("TL");
SessionFactory sf = HibernateUtilImpl.getSessionFacotry();
Session session = sf.openSession();
session.beginTransaction();
session.save(emp);
session.save(empp);
session.getTransaction().commit();
session.close();
}
private static void readEmployee() {
Employee emp1 = new Employee();
SessionFactory sf = HibernateUtilImpl.getSessionFacotry();
Session session = sf.openSession();
session.beginTransaction();
// provided primary key to get the perticular record
emp1 = (Employee) session.get(Employee.class, 1);
System.out.println("The name of employee is "+emp1.getEmpName()+" And the designation of Emp is "
+emp1.getDesignation());
session.getTransaction().commit();
session.close();
// to selector list all the record in the table using HQL
session = sf.openSession();
session.beginTransaction();
Query query = session.createQuery("from Employee");
List<Employee> list = query.list();
System.out.println("\n");
System.out.println("::::::::::Printing Emp Details:::::::::::");
System.out.println("\n");
for(Employee emp : list){
System.out.println("Emp id is "+emp.getEmpId()+" Emp Name is "+emp.getEmpName()
+" and the designation is "+emp.getDesignation());
}
session.getTransaction().commit();
session.close();
}
private static void updateEmployee() {
Employee emp1 = new Employee();
SessionFactory sf = HibernateUtilImpl.getSessionFacotry();
Session session = sf.openSession();
session.beginTransaction();
// provided primary key to get the perticular record
emp1 = (Employee) session.get(Employee.class, 1);
emp1.setDesignation("SSE");
System.out.println("Designation updated successfully");
session.getTransaction().commit();
session.close();
}
private static void deleteEmployee() {
SessionFactory sf = HibernateUtilImpl.getSessionFacotry();
Session session = sf.openSession();
session.beginTransaction();
// getting the object using load method instead of using get method
Employee emp = (Employee) session.load(Employee.class, 1);
session.delete(emp);
System.out.println("Employee Deleted");
session.getTransaction().commit();
session.clear();
}
}
To test the main class uncomment the method in main method one by one to test each operation.Thanks and Happy Learniing
Saturday, October 20, 2012
Build a RESTful Web service with Java
Representational state transfer (REST) is a architectural style of designing loosely coupled applications based on web-standards and the HTTP protocol. It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.The hardest part of building a RESTful application is deciding on the resources you want to expose. Once you've done that, using the open source Restlet framework makes building RESTful Web services a snap. This tutorial guides you step-by-step through the fundamental concepts of REST and building applications with Restlets.
Step 1: Create a dynamic web project in Eclipse and add the jars shown in the figure in the lib folder under the WEB-INF
Step 2: Add the Restful nature in the the project using fallowing entry in the web.xml
Step 3:Create the Employee class as fallows and annotted it with the @Path,@Produce and @Get annotations
Step 4: now run the dynamic web application on the tomcat server and access the fallowing URL
http://localhost:8080/RESTfulProject/REST/employee
Here is the browser output
Step 5:you can use jersy api or Apache HttpClient api to the test the webservice by creating a client,here I have created the client using the jersy api
After running this main class as a java application you will find the fallowing output on the console
<html> <title>Employee Details</title><body><h1>Employee Name is Dummy Employee</body></h1></html>
Step 1: Create a dynamic web project in Eclipse and add the jars shown in the figure in the lib folder under the WEB-INF
Step 2: Add the Restful nature in the the project using fallowing entry in the web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>FirstProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>
com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/REST/*</url-pattern>
</servlet-mapping>
</web-app>
Step 3:Create the Employee class as fallows and annotted it with the @Path,@Produce and @Get annotations
package com.rajkrrsingh.blogspot;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/employee")
public class Employee {
@GET
@Produces(MediaType.TEXT_HTML)
public String getEmployee(){
return "<html> " + "<title>" + "Employee Details" + "</title>"
+ "<body><h1>" + "Employee Name is Dummy Employee" + "</body></h1>" + "</html> ";
}
}
Step 4: now run the dynamic web application on the tomcat server and access the fallowing URL
http://localhost:8080/RESTfulProject/REST/employee
Here is the browser output
Step 5:you can use jersy api or Apache HttpClient api to the test the webservice by creating a client,here I have created the client using the jersy api
package com.rajkrrsingh.blogspot.client;
import java.net.URI;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class Main {
public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
System.out.println(service.path("REST").path("employee").accept(MediaType.TEXT_HTML).get(String.class));
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/RESTfulProject").build();
}
}
After running this main class as a java application you will find the fallowing output on the console
<html> <title>Employee Details</title><body><h1>Employee Name is Dummy Employee</body></h1></html>
Subscribe to:
Posts (Atom)



