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.

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.
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:



JSONObject Model APIStreaming API
{ }JsonObject jsonObject =
     new JsonObjectBuilder().build();

new JsonWriter(System.out)
     .writeObject(jsonObject);
JsonGeneratorFactory factory =
     Json.createGeneratorFactory();

JsonGenerator gen =
     factory.createGenerator(System.out);

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("apple","red"))

  .add(new JsonObjectBuilder()
          .add("banana","yellow"))

  .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:
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 fallows
package 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
<?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