无法在mapreduce 2.x中运行wordcount示例

时间:2015-05-29 10:31:33

标签: hadoop mapreduce

我正在尝试在java中的mapreduce 2.x中执行mapreduce字数exaple ....我已经创建了jar但是在执行它时显示的错误就像在我的包中找不到WordMapper类但我已经声明了我的包.....请帮我解决问题......

这是我的WordCount驱动程序代码:

package com.mapreduce2.x;

public class WordCount {

public static void main(String args[]) throws IOException, ClassNotFoundException, InterruptedException
{
    Configuration conf=new Configuration();

    org.apache.hadoop.mapreduce.Job job= new org.apache.hadoop.mapreduce.Job(conf, "Word_Count");


    job.setMapperClass(WordMapper.class);
    job.setReducerClass(WordReducer.class);

    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(IntWritable.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    org.apache.hadoop.mapreduce.lib.input.FileInputFormat.setInputPaths(job, new Path(args[0]));
    org.apache.hadoop.mapreduce.lib.output.FileOutputFormat.setOutputPath(job, new Path(args[1]));

    job.waitForCompletion(true);

}}

这是我的WordMapper类: -

public class WordMapper extends Mapper<LongWritable, Text, Text,IntWritable>{

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

public void map(LongWritable key, Text value, org.apache.hadoop.mapreduce.Reducer.Context context) throws IOException, InterruptedException
{
    String line=value.toString();
    StringTokenizer tokenizer=new StringTokenizer(line);

    while(tokenizer.hasMoreTokens())
    {
        word.set(tokenizer.nextToken());
        context.write(word, one);

    }


}}

WordReducer代码 -

public class WordReducer extends Reducer<Text, IntWritable, Text, IntWritable>{


public void reduce(Text key, Iterator<IntWritable> values,Context context) throws IOException, InterruptedException
{
    int sum =0;

    while(values.hasNext())
    {
        sum= sum+values.next().get();
    }

    context.write(key, new IntWritable(sum));
}}

执行 -

时显示以下错误
15/05/29 10:12:26 INFO mapreduce.Job:  map 0% reduce 0%
15/05/29 10:12:33 INFO mapreduce.Job: Task Id : attempt_1432876892622_0005_m_000000_0, Status : FAILED
Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.mapreduce2.x.WordMapper not found
    at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2076)
    at org.apache.hadoop.mapreduce.task.JobContextImpl.getMapperClass(JobContextImpl.java:186)
    at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:742)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
    at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:163)
    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:1628)
    at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)
Caused by: java.lang.ClassNotFoundException: Class com.mapreduce2.x.WordMapper not found
    at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:1982)
    at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2074)
    ... 8 more

3 个答案:

答案 0 :(得分:0)

在运行JAR文件时包含类名,或者您可以在创建JAR文件时指定主类名。

如果您在没有类名的情况下运行,请在运行JAR时指定类名。

使用该命令 hadoop jar word.jar com.mapreduce2.x.WordMapper / input / output

这里word.jar是JAR文件名。

OR

您还可以在创建jar文件时包含主类名。 脚步: 档案 - &gt;出口 - &gt; JAR - &gt;位置 - &gt;然后点击下一步 - &gt;它要求选择主类 - &gt;选择该类,然后单击“确定”

之后,您可以使用命令

运行jar文件

hadoop jar word.jar / input / output

希望这能解决您的问题。

答案 1 :(得分:0)

尝试添加下面的注释行 工作职位=新工作(conf,&#34; wordcount&#34;); //job.setJarByClass(WordCount.class);

它对我有用

答案 2 :(得分:0)

你可以尝试这个:(在Linux / Unix中)

  1. 删除java代码中的包名称。

  2. 在包含java程序的目录中,创建一个名为classes的新目录。例如:Hadoop-Wordcount -> classes , WordCount.java

  3. 编译:{{1​​}}

  4. 创建一个jar javac -classpath $HADOOP_HOME/hadoop-common-2.7.1.jar:$HADOOP_HOME/hadoop-mapreduce-client-core-2.7.1.jar:$HADOOP_HOME/hadoop-annotations-2.7.1.jar:$HADOOP_HOME/commons-cli-1.2.jar -d ./classes WordCount.java

  5. 5.run jar -cvf wordcount.jar -C ./classes/ .