Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a Hadoop program?

Tags:

hadoop

I have set up Hadoop on my laptop and ran the example program given in the installation guide successfully. But, I am not able to run a program.

rohit@renaissance1:~/hadoop/ch2$ hadoop MaxTemperature input/ncdc/sample.txt output
Exception in thread "main" java.lang.NoClassDefFoundError: MaxTemperature
Caused by: java.lang.ClassNotFoundException: MaxTemperature
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: MaxTemperature.  Program will exit.

The book said that we should set a Hadoop Classpath by writing

rohit@renaissance1:~/hadoop/ch2$ export HADOOP_CLASSPATH=build/classes

The main class is defined in MaxTemperature.java file that I am executing. How do we set the Hadoop Classpath? Do we have to do it for all program execution or only once? Where should I put the input folder. My code is at /home/rohit/hadoop/ch2 and my Hadoop installation is at /home/hadoop.

like image 966
rohitmishra Avatar asked Sep 26 '10 20:09

rohitmishra


2 Answers

You should package your application into a JAR file, that's much easier and less error-prone than fiddling with classpath folders.

In your case, you must also compile the .java file. You said it's MaxTemparature.java, but there must also be a MaxTemperature.class before you can run it.

like image 108
mhaller Avatar answered Nov 18 '22 20:11

mhaller


  1. First compile the Java files as told by walid:

    javac -classpath path-to-hadoop-0.19.2-core.jar .java-files -d folder-to-contain-classes
    
  2. Create jar file of application classes using the following command:

    jar cf filename.jar *.class
    

    In either of the, whether you are exporting the classes into jar file or using specific folder to store class files , you should define HADOOP_CLASSPATH pointing to that particular class file or folder containing class file. So that at the time of running Hadoop command it should know where to look specified for main class.

  3. set HADOOP_CLASSPATH

    export HADOOP_CLASSPATH=path-to-filename.jar
    

    or

    export HADOOP_CLASSPATH=path-to-folder-containing-classes
    
  4. Run using Hadoop command:

    hadoop main-class args
    
like image 7
swapy Avatar answered Nov 18 '22 19:11

swapy