【问题标题】:not able to run wordcount example in mapreduce 2.x无法在 mapreduce 2.x 中运行 wordcount 示例
【发布时间】:2015-05-29 10:31:33
【问题描述】:

我正在尝试在 java 中的 mapreduce 2.x 中执行 mapreduce word count 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

【问题讨论】:

    标签: hadoop mapreduce


    【解决方案1】:

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

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

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

    这里 word.jar 是 JAR 文件名。

    您还可以在创建 jar 文件时包含主类名。 脚步: 文件 --> 导出 --> JAR --> 位置 --> 然后单击下一步 --> 它要求选择主类 --> 选择类并单击确定

    之后就可以用命令运行jar文件了

    hadoop jar word.jar /input /output

    希望这能解决您的问题。

    【讨论】:

    • wordcount 是我的主类,我在执行时指定... hadoop jar wordcount.jar com.mapreduce2.x.WordCount /input/output 但它不起作用
    【解决方案2】:

    尝试在下面添加注释行 工作job = new Job(conf, "wordcount"); //job.setJarByClass(WordCount.class);

    它对我有用

    【讨论】:

      【解决方案3】:

      你可以试试这个:(IN Linux/Unix)

      1. 去掉java代码中的包名。

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

      3. 编译: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

      4. 创建一个罐子jar -cvf wordcount.jar -C ./classes/ .

      5.运行bin/hadoop jar $HADOOP_HOME/Hadoop-WordCount/wordcount.jar WordCount input output

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-02
        • 1970-01-01
        • 2017-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多