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。

第四步:

填入如下代码:

  1. package org.apache.hadoop.examples;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.StringTokenizer;  
  5.   
  6. import org.apache.hadoop.conf.Configuration;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.IntWritable;  
  9. import org.apache.hadoop.io.Text;  
  10. import org.apache.hadoop.mapreduce.Job;  
  11. import org.apache.hadoop.mapreduce.Mapper;  
  12. import org.apache.hadoop.mapreduce.Reducer;  
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  14. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  15. import org.apache.hadoop.util.GenericOptionsParser;  
  16.   
  17. public class WordCount {  
  18.   
  19.     public static class TokenizerMapper extends  
  20.             Mapper<Object, Text, Text, IntWritable> {  
  21.   
  22.         private final static IntWritable one = new IntWritable(1);  
  23.         private Text word = new Text();  
  24.   
  25.         public void map(Object key, Text value, Context context)  
  26.                 throws IOException, InterruptedException {  
  27.             StringTokenizer itr = new StringTokenizer(value.toString());  
  28.             while (itr.hasMoreTokens()) {  
  29.                 word.set(itr.nextToken());  
  30.                 context.write(word, one);  
  31.             }  
  32.         }  
  33.     }  
  34.   
  35.     public static class IntSumReducer extends  
  36.             Reducer<Text, IntWritable, Text, IntWritable> {  
  37.         private IntWritable result = new IntWritable();  
  38.   
  39.         public void reduce(Text key, Iterable<IntWritable> values, Context context)  
  40.                 throws IOException, InterruptedException {  
  41.             int sum = 0;  
  42.             for (IntWritable val : values) {  
  43.                 sum += val.get();  
  44.             }  
  45.             result.set(sum);  
  46.             context.write(key, result);  
  47.         }  
  48.     }  
  49.   
  50.     public static void main(String[] args) throws Exception {  
  51.         Configuration conf = new Configuration();  
  52.         conf.set("mapred.job.tracker""192.168.0.151:9001");  
  53.         String[] ars = new String[] { "input""output" };  
  54.         String[] otherArgs = new GenericOptionsParser(conf, ars)  
  55.                 .getRemainingArgs();  
  56.         if (otherArgs.length != 2) {  
  57.             System.err.println("Usage: wordcount  ");  
  58.             System.exit(2);  
  59.         }  
  60.         Job job = new Job(conf, "word count");  
  61.         job.setJarByClass(WordCount.class);  
  62.         job.setMapperClass(TokenizerMapper.class);  
  63.         job.setCombinerClass(IntSumReducer.class);  
  64.         job.setReducerClass(IntSumReducer.class);  
  65.         job.setOutputKeyClass(Text.class);  
  66.         job.setOutputValueClass(IntWritable.class);  
  67.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  68.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  69.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  70.     }  
  71. }  
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了。


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值