`
dawuafang
  • 浏览: 1105061 次
文章分类
社区版块
存档分类
最新评论

HADOOP的学习笔记 (第四期) eclipse 执行 wordcount

 
阅读更多


上一期基本已经搭配好了eclipse hadoop的配置环境,但是肯定会有的朋友说有问题。经过我的测试发现是有一定的问题,但是问题不大,这期首先先把上期遇到的问题解决一下,提出我的解决方案,如果不是我遇到的问题,就需要朋友们自己找解决方案了。


1、第一个问题:环境搭配成功了以后,点击create new directory 无法创建目录。

我发现的原因是,因为我的虚拟机用配置的hadoop环境用的是root这个用户,而我们windows中用的是administrator这个用户,而这时是用administrator这个用户去进行的hadoop操作,所以才会不行,这个时候我们只需要在window中创建一个root的来宾账户,用这个账户打开eclipse,然后就ok了。


这样基本就ok了。环境ok了我们来实现我们第一个mr程序,wordcount,其实也不是我们的啦。在hadoop源码包中有此文件。我们只是用到。

现在我们创建一个hadoop project

第一步:

在空白处点右键---》new ---》Other

选择

第二步:

给项目起个名字。然后点击finish。

第三步:

添完以后点击finish。

第四步:

填入如下代码:

package org.apache.hadoop.examples;

import java.io.IOException;
import java.util.StringTokenizer;

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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

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();
		conf.set("mapred.job.tracker", "192.168.0.151:9001");
		String[] ars = new String[] { "input", "output" };
		String[] otherArgs = new GenericOptionsParser(conf, ars)
				.getRemainingArgs();
		if (otherArgs.length != 2) {
			System.err.println("Usage: wordcount  ");
			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);
	}
}
第五步:

运行WordCount.java,在类上点击右键,选择run on hadoop

点击finish。

第六步:

查看运行结果:


至此算是在eclipse中的wordcount程序跑完了。


ps:运行途中可能会出现的问题:

1.我忘记了截图,我第一次跑的时候会提示一个错误,192.168.0.151:50010什么的错误,我感觉应该是50010端口没有开放,进到虚拟机中, vi /etc/sysconfig/iptables 将50010端口开放,然后重启service iptables restart 然后再执行就ok了。

2.如果你和我一样懒,觉得没有资源文件,那就直接解压缩的文件夹中找,我就是在文件夹中找的。 在解压缩的hadoop文件夹中,hadoop fs -put *.txt input就ok了。


*-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-**-_-*

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics