作解5 Mapper cannot be resolved to a type

请问基本的wordcount示例,建立了一个maven项目,然后把示例代码写进去,并且在pom.xml中添加了一项
 <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.6.0-cdh5.4.8</version>   
 </dependency>
请问为什么还会出现Mapper cannot be resolved to a type、IntWritable cannot be resolved to a type、Reducer cannot be resolved to a type这些错误?怎么解决?

wangxiaolei

赞同来自:

看看你写的代码

vitocorleone

赞同来自:

package cn.chinahadoop;

import java.io.IOException;


/**
 * Hello world!
 *
 */
public class WordCount{
    public static class TokenizerMapper
        extends Mapper<Object,Text,Text,IntWritable>{
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();
        public void map(Object key,Text value,Context context
            )throws IOException,InterruptedException{
            StringTokenizer itr = new StringTokenizer(value.toString());
            while(itr.hasMoreTokens()){
                word.set(itr.nextToken());
                context.write(word,one);
            }
        }
    }  
    public static class IntSumReducer extends
        Reducer<Text,IntWritable,Text,IntWritable>{
        private IntWritable result = new IntWritable();
        public void reduce(Text key,Iterable<IntWritable> values,
            Context context)throws IOException,InterruptedException{
            int sum = 0;
            for(IntWritable val : values){ 
                sum += val.get();                  
                }
            result.set(sum);
            context.write(key,result);
            }       
    }
    public static void main(String[] args)throws Exception{
    Configuration conf = new Configuration();
    String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
    if(otherArgs.length !=2){
        System.err.println("Usage:wordcount <in> <out>");
        System.exit(2);
     }
    Job job = new Job(conf,"word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.addInputPath(job,new Path(otherArgs[0]));
    FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);  
    }
}

wangxiaolei

赞同来自:

写好pom.xml文件后, 先生成Eclipse项目,使用命令 mvn eclipse:eclipse 可以在Eclipse界面上的代码中,使用快捷建导入包。 然后再使用命令mvn clean package  

vitocorleone

赞同来自:

[root@dx2-1 WordCount]# mvn eclipse:clean [INFO] Scanning for projects... [INFO]                                                                          [INFO] ------------------------------------------------------------------------ [INFO] Building WordCount 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO]  [INFO] --- maven-eclipse-plugin:2.10:clean (default-cli) @ WordCount --- [INFO] Deleting file: .project [INFO] Deleting file: .classpath [INFO] Deleting file: .wtpmodules [INFO] Deleting file: .settings [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.927 s [INFO] Finished at: 2016-06-21T17:51:39+08:00 [INFO] Final Memory: 10M/29M [INFO] ------------------------------------------------------------------------ [root@dx2-1 WordCount]# mvn eclipse:eclipse [INFO] Scanning for projects... [INFO]                                                                          [INFO] ------------------------------------------------------------------------ [INFO] Building WordCount 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO]  [INFO] >>> maven-eclipse-plugin:2.10:eclipse (default-cli) > generate-resources @ WordCount >>> [INFO]  [INFO] <<< maven-eclipse-plugin:2.10:eclipse (default-cli) < generate-resources @ WordCount <<< [INFO]  [INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ WordCount --- [INFO] Using Eclipse Workspace: /root/workspace [INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER [WARNING] Missing POM for org.apache.hadoop:hadoop-client:jar:2.6.0-cdh5.4.8 [INFO] Not writing settings - defaults suffice [INFO] Wrote Eclipse project for "WordCount" to /root/workspace/WordCount. [INFO]  [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.948 s [INFO] Finished at: 2016-06-21T17:52:08+08:00 [INFO] Final Memory: 10M/29M [INFO] ------------------------------------------------------------------------ [root@dx2-1 WordCount]#  再打开eclipse后<ctrl>+<shift>+o后显示如下图:请问选择哪个?

vitocorleone

赞同来自:

如下图两个红箭头所指,这是什么问题?怎么解决?

wangxiaolei

赞同来自:

pom.xml文件中没有配置
<repositories>
        <repository>
        <id>cloudera</id>
        <url>https://repository.cloudera.co ... gt%3B
        </repository>
</repositories>

wangxiaolei

赞同来自:

如果在命令行成功的话。 红叉可以不用管,是Eclipse编译器问题造成的。

vitocorleone

赞同来自:

如下图,在命令行运行mvn eclipse:eclipse可以BUILD SUCCESS但是,在WordCount.java中还是有如图红圈所示的错误,为什么呀?

wangxiaolei

赞同来自:

缺少包,需要导入。 不会导入?机器IP密码私信发我  

wangxiaolei

赞同来自:

1466565104089.png
我操作的可以导入啊。关于导入包的,你问过很多次了。 自己去导包。

vitocorleone

赞同来自:

如下图,请问从第一张图到第六张图,分别应该选择导入哪个包?

vitocorleone

赞同来自:

导入哪个?

vitocorleone

赞同来自:

导入哪个?

vitocorleone

赞同来自:

导入哪个?

vitocorleone

赞同来自:

导入哪个?

vitocorleone

赞同来自:

导入哪个?

vitocorleone

赞同来自:

直接点next,最后点finish即可

要回复问题请先登录注册