How to Execute Serialization and Deserialization program for Hadoop in Netbeans IDE..!

Hey Guyss.! I am posting this blog because when I was new to hadoop my teacher asked me to execute serialization and deserialization program on netbeans using Hadoop.  For executing any hadoop file on netbeans we need hadoop’s jar file, but  I faced so much difficulty  finding the correct jar file for it. Believe me I searched all through  the possible sites for a briefing on how to do it but it wasn’t worth. So I just don’t want you guys to face the same difficulty as I faced. So here are the steps that you can follow to easily execute the serialization and deserialization program for hadoop using Netbeans as your IDE:

Step 1: Start Netbeans.

Step 2: Click on File then on New Project, you will see java is already selected in categories tab so just click on next button.

Step 3: Then enter some project name or let it be the default one.(I have put the “blog” as the name, you can see).

first_blog

Fig. 1

Step 4: A java file will be created same as the project name, if you wish you can change it. Just right click on the java file name and select Refactor and then Click on the Rename option. In next dialog box enter your desired filename and then click on the Refactor button.(You can see it in Fig.2 and Fig.3)

 

secblog

Fig.2

newfig3

 

Fig.3

Now is the time to start actually what we are trying to do in this blog . So as you can see in fig 1, I have kept the java file name as “Serialization” so in the same way create one more java file and name it as “Deserialization”.

Program Code:

Serialization.java

package serialization;

import java.io.ByteArrayOutputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;

public class Serialization {

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

// TODO code application logic here

Serialization serialization= new Serialization();

serialization.serialize();

System.out.println(“Heyy! You serialized the data successfully”);

}

public byte[] serialize() throws IOException{

 

//Instantiating the IntWritable object

IntWritable intwritable = new IntWritable(28);

//Instantiating ByteArrayOutputStream object

ByteArrayOutputStream byteoutputStream = new

ByteArrayOutputStream();

//Instantiating DataOutputStream object

DataOutputStream dataOutputStream = new

DataOutputStream(byteoutputStream);

//Serializing the data

intwritable.write(dataOutputStream);

//storing the serialized object in bytearray

byte[] byteArray = byteoutputStream.toByteArray();

//Closing the OutputStream

dataOutputStream.close(); return(byteArray);

}

}

 

Deserialization.java

package serialization;

import java.io.ByteArrayInputStream;

import java.io.DataInputStream;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;

public class Deserialization {

public void deserialize(byte[] byteArray) throws IOException{

//Instantiating the IntWritable class

IntWritable intwritable =new IntWritable();

//Instantiating ByteArrayInputStream object

ByteArrayInputStream byteInputStream = new

ByteArrayInputStream(byteArray);

//Instantiating DataInputStream object

DataInputStream datainputstream=new

DataInputStream(byteInputStream);

//deserializing the data in DataInputStream

intwritable.readFields(datainputstream);

//printing the serialized data

System.out.println((intwritable).get()); }

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

System.out.println(“Heyy! Your deserialized value is:”);

Deserialization dese = new Deserialization();

dese.deserialize(new Serialization().serialize());

}

}

So these are  just two programs that are needed for serializing and deserializing your data. In this example I am using just an integer value. You can experiment by changing it to float etc.  Now in the below shown figure (fig 4) I am showing the jar file needed for its proper execution:

lib

Fig. 4

All you have to do is to just type these jar files name and you will easily get these files over the internet, just download it..!! So I hope you have downloaded by now, okk so next is how to add these jar files to your project.  JAR files are JAVA ARCHIVE file that contain number of classes, images and sound files for running a java application. These jar files are basically built on ZIP and has .jar file extension. After downloading these files unzip these files first then right click on Library and click on “Add jar/Folder”, select the file that you have unzipped from jar file and click on ok.

jar

Fig. 5

Now you are done with everything adding jar file to writing the code. Last thing is to run this program. We run these programs in the same way that we use to execute any other program using Netbeans. Remember you have to run the Serialize code first and then the Deserialize code. You can see the output in the output console…I hope this would work for you too..:) All the Best..:)

 

 

 

Advertisements