参看文档
http://blog.youkuaiyun.com/tryhl/article/details/43967441
也可以关注我的另外一篇文章http://www.aboutyun.com/blog-12709-1768.html
也可以加入群 316297243一起学习讨论
此文章用于学习和交流,转载请注明
1 Hadoop1.x的Maven项目构建
怎么用Maven构建项目,以及怎么构建hadoop项目,这里就不详细介绍,详情请参考
http://blog.youkuaiyun.com/tryhl/article/details/43967441
这里只讲述在用maven构建hadoop1.x项目以及导入Eclipse运行需要注意的问题以及产生的问题。
1)修改pom.xml文件
我依赖的是hadoop-1.2.1版本的
加入如下依赖
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
加入依赖之后等待下载相关的jar包
2)从hadoop集群中下载配置文件
这里不加也行
下载配置文件:
core-site.sh
hdfs-site.sh
mapred-site.sh
把配置文件加入/main/java/resources/hadoop文件夹下
3) 加入WordCount源码
public class WordCount {
public static class WordCountMapper extends MapReduceBase implements
Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
public void map(Object key, Text value,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
output.collect(word, one);
}
}
}
public static class WordCountReducer extends MapReduceBase implements
Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
@Override
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
// TODO Auto-generated method stub
int sum = 0;
while (values.hasNext()) {
sum += va