Hadoop Map reduce作业提供ClassNotFound异常,即使使用纱线运行时存在mapper?

Hadoop Map reduce作业提供ClassNotFound异常,即使使用纱线运行时存在mapper?,hadoop,mapreduce,Hadoop,Mapreduce,我正在运行一个hadoop作业,当我在伪分布式模式下运行它时,它运行得很好,但当运行Thread时,它给了我类not found exception 16/03/24 01:43:40 INFO mapreduce.Job: Task Id : attempt_1458775953882_0002_m_000003_1, Status : FAILED Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Cla

我正在运行一个hadoop作业,当我在伪分布式模式下运行它时,它运行得很好,但当运行Thread时,它给了我类not found exception

16/03/24 01:43:40 INFO mapreduce.Job: Task Id : attempt_1458775953882_0002_m_000003_1, Status : FAILED
Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.hadoop.keyword.count.ItemMapper not found
    at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2195)
    at org.apache.hadoop.mapreduce.task.JobContextImpl.getMapperClass(JobContextImpl.java:186)
    at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:745)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
    at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:164)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:415)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1657)
    at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)
Caused by: java.lang.ClassNotFoundException: Class com.hadoop.keyword.count.ItemMapper not found
    at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:2101)
    at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2193)
    ... 8 more
下面是

这是我正在运行的命令

hadoop jar ~/itemcount.jar /user/rohit/tweets /home/rohit/outputs/23mar-yarn13 vodka,wine,whisky
根据建议编辑代码

package com.hadoop.keyword.count;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ItemImpl {

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

        Configuration conf = new Configuration();
        conf.set("keywords", args[2]);

        Job job = Job.getInstance(conf, "item count");
        job.setJarByClass(ItemImpl.class);
        job.setMapperClass(ItemMapper.class);
        job.setReducerClass(ItemReducer.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }


    public static class ItemMapper extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        JSONParser parser = new JSONParser();

        @Override
        public void map(Object key, Text value, Context output) throws IOException,
                InterruptedException {

            JSONObject tweetObject = null;

            String[] keywords = this.getKeyWords(output);

            try {
                tweetObject = (JSONObject) parser.parse(value.toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (tweetObject != null) {
                String tweetText = (String) tweetObject.get("text");

                if(tweetText == null){
                    return;
                }

                tweetText = tweetText.toLowerCase();
    /*          StringTokenizer st = new StringTokenizer(tweetText);

                ArrayList<String> tokens = new ArrayList<String>();

                while (st.hasMoreTokens()) {
                    tokens.add(st.nextToken());
                }*/

                for (String keyword : keywords) {
                    keyword = keyword.toLowerCase();
                    if (tweetText.contains(keyword)) {
                        output.write(new Text(keyword), one);
                    }
                }
                output.write(new Text("count"), one);
            }

        }

        String[] getKeyWords(Mapper<Object, Text, Text, IntWritable>.Context context) {

            Configuration conf = (Configuration) context.getConfiguration();
            String param = conf.get("keywords");

            return param.split(",");

        }
    }

    public static class ItemReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context output)
                throws IOException, InterruptedException {

            int wordCount = 0;

            for (IntWritable value : values) {
                wordCount += value.get();
            }

            output.write(key, new IntWritable(wordCount));
        }
    }
}
package com.hadoop.keyword.count;
导入java.io.IOException;
导入org.apache.hadoop.conf.Configuration;
导入org.apache.hadoop.fs.Path;
导入org.apache.hadoop.io.IntWritable;
导入org.apache.hadoop.io.Text;
导入org.apache.hadoop.mapreduce.Job;
导入org.apache.hadoop.mapreduce.Mapper;
导入org.apache.hadoop.mapreduce.Reducer;
导入org.apache.hadoop.mapreduce.Mapper.Context;
导入org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
导入org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
导入org.json.simple.JSONObject;
导入org.json.simple.parser.JSONParser;
导入org.json.simple.parser.ParseException;
公共类itempl{
公共静态void main(字符串[]args)引发异常{
Configuration conf=新配置();
conf.set(“关键字”,args[2]);
Job Job=Job.getInstance(conf,“项目计数”);
job.setJarByClass(itempl.class);
setMapperClass(ItemMapper.class);
job.setReducerClass(ItemReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
addInputPath(作业,新路径(args[0]);
setOutputPath(作业,新路径(args[1]);
系统退出(作业等待完成(真)?0:1;
}
公共静态类ItemMapper扩展了映射器{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
JSONParser=新的JSONParser();
@凌驾
公共void映射(对象键、文本值、上下文输出)引发IOException,
中断异常{
JSONObject tweetObject=null;
字符串[]关键字=this.getKeyWords(输出);
试一试{
tweetObject=(JSONObject)parser.parse(value.toString());
}捕获(解析异常){
e、 printStackTrace();
}
if(tweetObject!=null){
String tweetText=(String)tweetObject.get(“文本”);
如果(tweetText==null){
返回;
}
tweetText=tweetText.toLowerCase();
/*StringTokenizer st=新的StringTokenizer(tweetText);
ArrayList标记=新的ArrayList();
而(st.hasMoreTokens()){
添加(st.nextToken());
}*/
for(字符串关键字:关键字){
关键字=关键字.toLowerCase();
if(tweetText.contains(关键字)){
输出.写入(新文本(关键字),一个);
}
}
输出。写入(新文本(“计数”),一个;
}
}
字符串[]getKeyWords(Mapper.Context上下文){
Configuration=(Configuration)context.getConfiguration();
字符串param=conf.get(“关键字”);
返回参数拆分(“,”);
}
}
公共静态类ItemReducer扩展了Reducer{
@凌驾
受保护的void reduce(文本键、Iterable值、上下文输出)
抛出IOException、InterruptedException{
int字数=0;
for(可写入值:值){
wordCount+=value.get();
}
write(key,新的intwriteable(wordCount));
}
}
}

您能检查itemcount.jar的内容吗?(jar-tvf itemcount.jar)。我曾经遇到过这个问题,但却发现jar中缺少.class。

几天前我也遇到了同样的错误

  • 更改地图并将类减少为静态修复了我的问题
  • 制作地图并减少类内部的类
  • 控制map和reduce类的构造函数(i/o值和override语句)
  • 检查jar命令
旧的

hadoop jar~/itemcount.jar/user/rohit/tweets/home/rohit/outputs/23mar-yarn13伏特加、葡萄酒、威士忌

新的

hadoop jar~/itemcount.jar com.hadoop.keyword.count.itempl/user/rohit/tweets/home/rohit/outputs/23mar-yarn13伏特加、葡萄酒、威士忌

  • 在指定.jar文件后添加packageName.mainclass
试抓

try {
         tweetObject = (JSONObject) parser.parse(value.toString());
         } catch (Exception e) { **// Change ParseException to Exception if you don't only expect Parse error**
          e.printStackTrace();
         return; **// return from function in case of any error**
            }
}
扩展配置和实施工具

public class ItemImpl extends Configured implements Tool{
public static void main (String[] args) throws Exception{
    int res =ToolRunner.run(new ItemImpl(), args);
    System.exit(res);
        }

    @Override
    public int run(String[] args) throws Exception { 

        Job job=Job.getInstance(getConf(),"ItemImpl ");
        job.setJarByClass(this.getClass());

        job.setJarByClass(ItemImpl.class);
        job.setMapperClass(ItemMapper.class);
        job.setReducerClass(ItemReducer.class);
        job.setMapOutputKeyClass(Text.class);//probably not essential but make it certain and clear
        job.setMapOutputValueClass(IntWritable.class); //probably not essential but make it certain and clear
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
 add public static map
 add public static reduce
 I'm not an expert about this topic but This  implementation is from one of my working projects. Try this if doesn't work for you I would suggest you check the libraries you added to your project.
也许第一步就能解决这个问题,但是
如果这些步骤不起作用,请与我们共享代码。

在完全分布式模式下运行您的TaskTracker/NodeManager(运行映射程序的东西)正在一个单独的JVM中运行,听起来您的类没有进入该JVM的类路径

尝试在作业调用时使用
-libjars
命令行arg。这将使Hadoop将jar分发到TaskTracker JVM,并从该jar加载您的类。(注意,这会将jar复制到集群中的每个节点,并使其仅可用于该特定作业。如果您有许多作业都需要调用的公共库,则需要研究使用Hadoop分布式缓存。)


您可能还希望在启动作业时尝试
warn-jar…
而不是
hadoop-jar…
,因为这是启动warn作业的新的/首选方式。

我检查了内容,并且类已经存在。而且,当我在没有院子的情况下运行罐子时,它工作得很好。所以我不认为这是问题所在。事实上,在你提出建议之前,我已经尝试了第一步。但是得到了同样的错误,唯一的区别是找不到类ItemImpl.ItemMapper。@伙计,你能解释一下伏特加是什么吗
public class ItemImpl extends Configured implements Tool{
public static void main (String[] args) throws Exception{
    int res =ToolRunner.run(new ItemImpl(), args);
    System.exit(res);
        }

    @Override
    public int run(String[] args) throws Exception { 

        Job job=Job.getInstance(getConf(),"ItemImpl ");
        job.setJarByClass(this.getClass());

        job.setJarByClass(ItemImpl.class);
        job.setMapperClass(ItemMapper.class);
        job.setReducerClass(ItemReducer.class);
        job.setMapOutputKeyClass(Text.class);//probably not essential but make it certain and clear
        job.setMapOutputValueClass(IntWritable.class); //probably not essential but make it certain and clear
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
 add public static map
 add public static reduce
 I'm not an expert about this topic but This  implementation is from one of my working projects. Try this if doesn't work for you I would suggest you check the libraries you added to your project.