Install MySQL on Ubuntu

Install MySQL (5.6) on Ubuntu (14.04)

Install MySQL

During the installation system will ask for root user password.

Check the service after installing mysql

sudo netstat -tap | grep mysql

mysql-service-working

If database installed cloud/network or not localhost then you need to change bind address to connect from your pc, to change bind address

nano /etc/mysql/my.cnf

To get effect need restart service

Now use ssh or MySQL Workbench as client of mysql database.

mysql-connection

Yet, connection error from MySQL Workbench

Failed to Connect to MySQL at xxx.xx.xxx.xxx:3306 with user root

Host ‘xxx.xx.xxx.xxx’ is not allowed to connect to this MySQL server

mysql-connection-error

To resolve this we need to create user and grant privilege to user

Install Pig on Ubuntu

Install Pig (0.15.0) on Ubuntu (14.04)

To know how to install Hadoop on Ubuntu please visit Hadoop 2.7 installation on Ubuntu 14.04 (Single Node)

Download pig to server:

Unzip & installation pig:

Pig generates lot of log for itself. If we do not provide log4j properties file then it shows error message, to avoid this we need to add properties file. Pig provides few log4j properties file

Need to let pig know about log4j.properties file by updating pig.properties file

Open file by nano command and set log4jconf value

nano /usr/local/pig/conf/pig.properties
log4jconf=./conf/log4j.properties

All are set…..

Improve performance of stored procedure called by java application

Improve performance of stored procedure called by java application

We have a stored procedure being called from our java web service.  It takes 1 sec from MS SQL management studio but takes 32 sec from the client of web service.

Java code:

Stored Procedure:

Few setting which will improve performance of stored procedure

set implicit_transactions off
set transaction isolation level READ COMMITTED

So new stored procedure will be

Hadoop 2.7 installation on Ubuntu 14.04 (Single Node)

Hadoop installation on Ubuntu

Installation Ubuntu on virtual machine

Here I am showing hadoop installation Ubuntu using VM player. Here is the vm setting of ubuntu

vm player settings

VM memory should be 25% – 35% of hosting machine.

Prerequisites

Before starting installation lets update all applications of software.

Installing Java

if java is not installed, then use following command for installing java

Or use ubuntu software center for installing jdk 6 or 7. Now check is it installed correctly

java-version

Adding a dedicated Hadoop system user

Installing SSH

Generate an SSH key for the hduser user

Here P “” indicates an empty password. Now needs to enable SSH access to local machine with this newly created key.

Test ssh

ssh-test

Disabling IPv6

We need to disable IPv6 because Ubuntu is using 0.0.0.0 IP for different Hadoop configurations. To do that open /etc/sysctl.conf file by gedit or nano and add following 3 lines at the end

Need to reboot system to get this effect.

Install Hadoop

The following files are needed to update single node Hadoop cluster

  1. core-site.xml
  2. mapred-site.xml
  3. hdfs-site.xml
  4. Update $HOME/.bashrc
  5. hadoop-env.sh

1.core-site.xml

The /usr/local/hadoop/etc/hadoop/core-site.xml file contains configuration properties which is used by Hadoop. Need to add following property to this file

Now lets create the directory and set the required ownership and permissions

2. mapred-site.xml

By default, the /usr/local/hadoop/etc/hadoop/ folder contains
mapred-site.xml.template and need to rename as mapred-site.xml and enter following content

3. hdfs-site.xml

Here we will specify namenode and datanode directories, to do that we need to create folders

to add properties we can use vi or nano,

4. Update $HOME/.bashrc

nano-bash

add following values at the end of file

5.hadoop-env.sh

Set JAVA_HOME by modifying /usr/local/hadoop/etc/hadoop/hadoop-env.sh file.

Formatting the HDFS filesystem

Switch to hduser

swithc-user

Format command:

Start Hadoop

start-hadoop

Hadoop Web Interfaces

http://localhost:50070/

web

Stop Hadoop

Enjoy!!! big data!!!

Hibernate cascade inverse example (none, save-update, delete and delete-orphan)

Hibernate cascade example with inverse

We use parent child relation in our most of application. To solve this we use hibernate cascade in application. It is very important to understand how hibernate cascade and inverse works. In this example I use One to Many relation where a parent table as Parent and child table as Child. We need to understand why and when 3 queries will be fired by hibernate for all cases

Query 1: it is for parent itself

Query 2: need to update,insert/update/delete, child based on cascade value. It will fire all values of cascade except none. It also fire if manually we try to save like session.save(child).

Query 3: need to update child only if relation owner is parent that means inverse = false and when a new parent is inserting or deleting a parent or a new child is adding to parent. It wont fire when inverse = true.

I am trying to show all possible examples on hibernate cascade that will help you to know hibernate cascade, inverse and difference between them.

cascade

This keyword use in relation and let hibernate know what should do for child. Based on this value hibernate fires query to update child and intermediate table. This keyword help you reduce additional work for relation. For example if you need to do anything for child just use this keyword hibernate will take care of everything.

inverse

In fact inverse =  owner of relation.  when inverse = true then relation owner = child otherwise parent. By default inverse = false that means parent is relation owner.

Example of hibernate cascade with inverse


When value of hibernate cascade =  “none” : Hibernate ignores the association.

Cascade = none inverse= false; relation owner = Parent

Here cascade = none and inverse = true so parent is relation owner. Q1 is fired for parent itself. Since cascade=none and child is not manually saved so no query (Q2 query) will be fired for child. Since parent is owner of relation it needs to update (Q3 query) child record. Since child is not save manually and child object is in hibernate memory, transient, but it is not going to database as a result hibernate throw unsaved instance

Cascade = none inverse = true; relation owner = Child

Here cascade = none and inverse = true so Child is relation owner and Parent does not need to update Child (Q3). Since cascade=none and child is not manually saved so no query (Q2) will be fired for child.

Cascade = none inverse = false; relation owner=Parent

Q1: for parent itself. Q2: cascade = none but manually session.save() exists. Q3: inverse = false; relation owner = parent. Parent is updating children.

Cascade = none inverse = true; relation owner = Child

Q1: for parent itself. Q2: cascade = none but manually session.save() exists. Q3:wont fire because inverse = true; relation owner = Child. Child does not need to update Parent.


when value of hibernate cascade =  “save-update” : Let hibernate to save a new instance or changes persistence object of parent and also association object(s).

Cascade = save-update inverse = false; relation owner = Parent

Q1: for parent itself. Q2: cascade = save-update and manually session.save() exists. Q3: fired because inverse = false; relation owner = Parent. Parent needs to update Parent.

Cascade = save-update inverse = true; relation owner =Child

Q1: for parent itself. Q2: cascade = save-update and manually session.save() exists. Q3: wont fire because inverse = ture;

Cascade = save-update inverse = true; relation owner =Child

Q1: for parent itself. Q2: cascade = save-update and manually session.save() exists. Q3: wont fire because inverse = true;

Cascade = save-update inverse = false; relation owner=Parent

Q1: for parent itself. Q2: cascade = save-update and manually session.update() exists. Q3: wont fire when parent is updating.

Cascade = save-update inverse = false; relation owner=Parent

Q1: wont fire because nothing change for parent.Q2: cascade = save-update. Q3: inverse = false; relation owner = parent. Parent is updating children.

Cascade = save-update inverse = true; relation owner=Child

Q1: wont fire because nothing change for parent.Q2: cascade = save-update. Q3: inverse = true; relation owner = Child.


when value of hibernate cascade= “delete-orphan”: Let hibernate deletes persistent object that has been removed from the association.

Cascade = delete-orphan inverse = false; relation owner=Parent

Q1: for parent itself. Q2: cascade = delete-orphan. Q3: inverse = false; relation owner = parent. Parent is updating children.

Cascade = delete-orphan inverse = true; relation owner=Child

Q1: for parent itself. Q2: cascade = delete-orphan. Q3: wont fire because inverse = true.

Cascade = delete-orphan inverse = false; relation owner=Parent

Q1: nothing update for parent. Q2: cascade = delete-orphan or manually delete. Q3: no insert or delete for parent.

Cascade = delete-orphan inverse = true; relation owner=Child

Q1: nothing update for parent. Q2: cascade = delete-orphan or manually delete. Q3: wont fire because there is no insert or delete for parent.

Download of Hibernate Cascade project

To get complete project of Hibernate cascade please click here.

Jdbc Connection

Jdbc Connection Using Oracle, SQL Server, My Sql

A jdbc connection is the object where commands are sent to the database and responses are returned to the program. Jdbc connection is that enables Java programs to execute SQL statements. The JDBC API consists of a set of interfaces and classes written in the Java programming language.

All databases provide their driver that enable users to make JDBC connections to databases. The two most common methods of connecting to Oracle databases  are Oracle Thin JDBC driver and the Oracle OCI JDBC driver.

For mssql server there are two ways we can make jdbc connection, using windows authentication or SQL authentication. For this there are two commonly used drivers JDBC and JTDS.

Steps of making Jdbc Connection

  • Load the JDBC driver
  • Define the connection url
  • Establish the connection using connection parameters

Examples of JDBC driver

Oracle :

jar file:  ojdbc14-10.2.0.3.0.jar

MSSQL :

jar files : sqljdbc.jar   sqljdbc4-4.0.jar 

My Sql:

jar file – mysql-connector-java-5.1.6.jar

Examples of Connection Url

Oracle :

MSSQL :

My SQL :

Examples of Jdbc Connection parameters

Oracle/MSSQL/My Sql :

jdbc connection – common errors

MSSQL:

  • Error: “java.net.SocketTimeoutException: Receive timed out”. Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434.
  • com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host  has failed. java.net.ConnectException: Connection refused: connect

Cause : 1. protocol (TCP/IP) is not enabled  2.Port is incorrect  3. missing of sqljdbc_auth.dll

Solutions:

1. protocol (TCP/IP) is not enabled :

a) Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager.  > Protocols for inst01, instance name, under SQL Server Network Configuration. Right-click on TCP/IP and choose Properties. Set Enabled = YES.  [this should be done by admin user]

Jdbc Connection - SQL Server Configuration Manager

Jdbc Connection – SQL Server Configuration Manager

2. Port is incorrect : Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager  > SQL Server Network Configuration >Protocols for inst01, instance name,  > Properties of TCP/IP  > IP Address tab and set TCP Port: 1433 , under IP All [this should be done by admin user]

3. missing of sqljdbc_auth.dll –

a) Download latest MSSQL JDBC driver from here: link1 , link2

b) take sqljdbc_auth.dll from x86/x64 folder.

c) copy the sqljdbc_auth.dll to your JDK directory bin and windowssystem32 folder. (you do not need restart your machine)

Download

To get a complete project of jdbc connection with MSSQL, click here.

Java classes of a project by reflection

Java classes by reflection

Today I am gonna show you all java classes of a project. Some times we get a requirement that we have to show all java classes that are using in a project. These classes those are written by developer of project. Basically this post is showing a recursion java example to get all java classes of a project. Using javadoc we can see in html page but this post java classes will give you a spread sheet file which contains of all java classes. In this example there is a interface, called service, it has method that’s input/output class contains different class. In these input/output class contains some domain class.  Here I am using Member, PersonalDetails, Address etc class as domain class. Here is one example of Member class

Service Interface

CreateMemberInput class

Our target output like

all java classes of output

all java classes of domain

To get all java classes

Step of to get all java classes:

Step 1: getAllServiceNames(): read service interface

Step 2: getServiceDetails(): Make a list of service input and output

Step 3: getAllFields(): read a class and return a list of attributes. Please see ReflectionHelper class. You will see download link at bottom.
Step 4: writeSpredSheet : write all results to spread sheet file. Please see ReflectionHelper class.

Now lets see visitAllClass() method of ReflectionHelper class, this method is visiting recursively to all child java classes according to all parent class. Here is this method

In top method, I am using java reflection at line 11 and getting class details. In deed, this we are passing parent class name as a parameter of this method. At line 14, we are taking all attributes of this class. At 19, we are checking all fields so that we can take a decision is it java.lang object or our domain object. If it is our domain object then it calls itself see line 34.

If any class has same class reference then you will get java.lang.reflect.InvocationTargetException , to rid off from this error you should check, see line 33, the same class name should not pass as argument of visitAllClass method.

I am using google guava collection framework though you can remove this jar from project. It is a maven project you can remove from decency from pom file. To know maven please visit here. Another post of this blog is describing about maven.

Download

Click here to download this project.

Jasper Reports Example

Jasper Reports example – Create Pie chart using iReport

In this blog I will show a jasper reports example using iRport designer. In this post I will show how to create a pie chart.  This is a very simple jasper reports example. I have one table called category and it has different categoryno with category name. Each category has subcategory. I want to show how many categories are there by pie chart.  Category table structure link will give you the table structure and some sample data. I used MySql database for this report. It supports couple of databases.

Jasper Reports example

The JasperReports api is the most popular open source reporting engine. It can be used in java enabled applications, including J2EE or web application to producing dynamic content. It is a Java based project. It reads its instructions from an XML or .jasper file. The tool iReport is use to create report template, design of report,  setting value of parameter and query etc. In this Jasper reports example  we will feed data from any kind of data sources.  It produce different kind of report like HTML, PDF, EXCEL, XML etc. To integrate a j2ee application, you need to add jar file to class path.  JasperReports reports are defined in an XML file format. This XML file is called JRXML file. So its file extension is .jrxml.  This file is readable, any one can easily modify it. iReport designer tool generates this jrxml file. We can create sub-report using iReport tool. In master details report we need sub-report. iReport is a graphical representation of jrxml. It is very useful tool for designing the entire report. After compiling a jrxml file iReport produces a file is called jasper file. Its extension is .jasper. A jasper file is used at runtime to produce reports.

If we need format any data we can add additional java code to report using iRerport. It supports all java codes. It is another beauty of iReport. For example currency, date format we can use it. Even column can show/hide based on value. That means we can add if / else logic. iReport designer runs reports using all data sources and also apply all internal java logics.

I have showed other jasper reports example in my other post.

Jasper Reports example – Steps for iReport designer

To get more details on Jasper reports example and documents click here and iReport is available at here.

  • Create New document from iReport. use this band value title = 30, columnHeader = 15, detail = 15 and summary = 300 and others = 0.
  • Add Query:     select category “Cat No”, subcat_name “category Name”, count(category) total from category
    group by category order by 1
  • Add 3 fields – Cat No, Category Name, Total to Detail band.    Jasper Reports Example - detail-band
  • Click the char button and select the Pie 3D. I put its width is 268 and height is 203.   Jasper Reports Example - ui2
  • right click on chart and select cart properties. select cart data tab, select the Details tab. and put the value as follows –  for Key Expression = $F{category Name} ,  Value Expression = $F{total},  Label Expression = $F{category Name}
  • click close.
  • b4 execute you must select your database connection. now click Execute with data connection. the output file will looks like as follows

jasper reports example: output-pie-report

Javascript validate number

Javascript validate number

this example allow only number character in you text field. This javascript validate number code can be tested at here .

—- JavaScript validate number code

——- html code

 

javascript convert string to number

Javascript convert string to number

Some time we need to get number value from currency format string . This Javascript convert string to number post will show how we get the exact number value. This javascript convert string to number code can be tested at here .

Lets consider we have a string its value like $1,221.22. So the actual number is 1221.22 after eliminate the Dollar sign and comma. The javascript code is –