Thursday 1 January 2015

Creating a JFrame Interface for programs in Cloudera hadoop

In the following example let us display the content of the HDFS file in the Java form.

Step1: Create a JFrame form using netbeans(so that it can be created using drag and drop components)

Fig: HadoopInterface.java


Step 2: Create a Java Class HadoopInterface.java in Eclipse and copy paste the HadoopInterface.java code from netbeans in host machine  into it.

Node: Step1 and Step 2 should be performed if the Swing plugin is not installed in eclipse.

Step 3: In button click write the following code to read the content of a file(which is a hadoop program output file)

Process p = Runtime.getRuntime().exec("hadoop fs -cat /user/training/"+jTextField1.getText()+"/part-r-00000");

or else if it is a text file or file with any other extension do the following................

Process p = Runtime.getRuntime().exec("hadoop fs -cat /user/training/"+jTextField1.text);

(e.g: where jTextField1.text will have filename eg:filename.txt)

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    
String s;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) 
{
               String str=jTextArea1.getText();
               jTextArea1.setText(str+"\n"+s);
}

Step 4: Final Output, on button click(SOutput is the file name)

Fig: Hadoop file content in JFrame Form.