hadoop:classnotfoundexception类

hec6srdp  于 2021-05-29  发布在  Hadoop
关注(0)|答案(4)|浏览(442)

我跟随这个教程,这是非常简单的(http://www.tutorialspoint.com/hadoop/hadoop_mapreduce.htm)在我尝试运行java文件之前,一切都正常。
我得到了以下线索。

Exception in thread "main" java.lang.ClassNotFoundException: ProcessUnits
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.apache.hadoop.util.RunJar.run(RunJar.java:214)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:136)

我尝试过很多不同的方法,我总是得到classnotfoundexception。我觉得这和hadoop类路径或者其他什么东西有关。请帮忙。

ykejflvf

ykejflvf1#

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;

public class ProcessUnits 
{ 
   //Mapper class 
   public static class E_EMapper extends MapReduceBase implements 
   Mapper<LongWritable ,/*Input key Type */ 
   Text,                /*Input value Type*/ 
   Text,                /*Output key Type*/ 
   IntWritable>        /*Output value Type*/ 
   { 

      //Map function 
      public void map(LongWritable key, Text value, 
      OutputCollector<Text, IntWritable> output,   
      Reporter reporter) throws IOException 
      { 
         String line = value.toString(); 
         String lasttoken = null; 
         StringTokenizer s = new StringTokenizer(line,"\t"); 
         String year = s.nextToken(); 

         while(s.hasMoreTokens())
            {
               lasttoken=s.nextToken();
            } 
         int avgprice = Integer.parseInt(lasttoken.trim()); 
         output.collect(new Text(year), new IntWritable(avgprice)); 
      } 
   } 

   //Reducer class 
   public static class E_EReduce extends MapReduceBase implements 
   Reducer< Text, IntWritable, Text, IntWritable > 
   {  

      //Reduce function 
      public void reduce( Text key, Iterator <IntWritable> values, 
         OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException 
         { 
            int maxavg=30; 
            int val=Integer.MIN_VALUE; 

            while (values.hasNext()) 
            { 
               if((val=values.next().get())>maxavg) 
               { 
                  output.collect(key, new IntWritable(val)); 
               } 
            } 

         } 
   }  
   //Main function 
   public static void main(String args[])throws Exception 
   { 
      JobConf conf = new JobConf(ProcessUnits.class); 
      conf.setJobName("max_eletricityunits"); 
      conf.setOutputKeyClass(Text.class);
      conf.setOutputValueClass(IntWritable.class); 
      conf.setMapperClass(E_EMapper.class); 
      conf.setCombinerClass(E_EReduce.class); 
      conf.setReducerClass(E_EReduce.class); 
      conf.setInputFormat(TextInputFormat.class); 
      conf.setOutputFormat(TextOutputFormat.class); 

      FileInputFormat.setInputPaths(conf, new Path(args[0])); 
      FileOutputFormat.setOutputPath(conf, new Path(args[1])); 

      JobClient.runJob(conf); 
   } 
}

这个代码运行良好。 ProcessUnits 是主班,我想,你改了主班的名字。 JobConf conf = new JobConf(ProcessUnits.class); 在那里你没有改变根据你的主要类。根据你的课程安排给班级主课。

c3frrgcw

c3frrgcw2#

是否可以检查是否正确运行该命令(检查当前目录和类名,应该有hadoop.processunits,而不仅仅是processunits,请参见下面的内容)。
$hadoop\u home/bin/hadoop jar units.jar hadoop.processunits input\u dir output\u dir
如果仍然不起作用,请尝试下面的命令
$hadoop\u home/bin/hadoop jar units.jar-cp./*hadoop.processunits input\u dir output\u dir

wsewodh2

wsewodh23#

尝试将jar文件添加到 HADOOP_CLASSPATH 并验证您是否编写了类的完整规范名称 mypackage.MyClass .

zlhcx6iw

zlhcx6iw4#

如果在构建将用作“hadoopjar”参数的jar文件的过程中出现错误,也会发生这种情况。这会导致jar文件清单中的主类为“null”而不是null。这将阻止hadoopjar命令从命令行参数获取主类。

相关问题