Hadoop的WordCount程序的一些说明

感觉接触Hadoop也有些时间了,但是因为机器环境的关系,一直没能好好折腾。Hadoop处理数据用MapReduce,存储数据用HDFS,其他先不考虑。

接触的第一个程序自然是WordCount,源代码的example中就有:


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

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;

public class WordCount {
	public static class WordCountMapper extends
			Mapper<Object, Text, Text, IntWritable> {

		private static final IntWritable ONE = new IntWritable(1);
		private Text word = new Text();

		@Override
		public void map(Object key, Text value, Context context)
				throws IOException, InterruptedException {
			System.out.println("map() is being called");
			StringTokenizer tokenizer = new StringTokenizer(value.toString());
			while (tokenizer.hasMoreTokens()) {
				word.set(tokenizer.nextToken());
				context.write(word, ONE);
			}
		}
	}

	public static class WordCountReducer extends
			Reducer<Text, IntWritable, Text, IntWritable> {

		@Override
		public void reduce(Text key, Iterable<IntWritable> values,
				Context context) throws IOException, InterruptedException {
			System.out.println("reduce() is being called");
			int wordcount = 0;
			for (IntWritable value : values) {
				wordcount += value.get();
			}
			context.write(key, new IntWritable(wordcount));
		}
	}

	public static void main(String[] args) throws Exception {
		//Configuration conf = new Configuration();

		Job job = new Job();
		job.setNumReduceTasks(0);
		job.setJarByClass(WordCount.class);
		job.setMapperClass(WordCountMapper.class);
		job.setCombinerClass(WordCountReducer.class);
		job.setReducerClass(WordCountReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		FileInputFormat.addInputPath(job, new Path("input"));
		FileOutputFormat.setOutputPath(job, new Path("output"));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}

有几点要说:

1、如果在eclipse中跑,可以想跑普通普通Java程序一样直接run就好了,如果要在集群上跑,就要打成jar包,原因我还不太清楚

2、要想看到所谓的intermediate output,可以用Job的setNumReducer()将reducer的个数设置为0,此时没有所谓的shuffle和sort,也就是看到的就是intermediate output

3、因为要将mapper处理的结果拷贝给reducer处理,那么最多有多少次拷贝呢?假设有m个mapper和r个reducer,那么将最多有m*r次拷贝,因为有可能一个mapper产生的intermediate output会被拷贝到r个不同的reducer,比如刚好每个mapper的intermediate output有r个不同的key。

4、只有所有的intermediate output都已经shuffle和sort了,reducer的计算才能开始

5、因为Hadoop实现的关系,虽然我已经setCombinerClass(),但结果并没有对intermediate output做所谓的"local aggregation",Hadoop的combiner可以被invoke 0次,1次或者多次,Hadoop决定,而且,Hadoop的combiner在reduce阶段才被invoke,也就是intermediate ouput key-value pairs已经被拷贝到reducer了,但是reducer的代码还没开始运行。

目前就大概知道这些,但是在哪里发生split的呢?还有看上去我传的是一个文件夹(input),但是map方法处理的是keyin-valuein,valuein是Text类型的,那么Hadoop是在哪里怎么对文件进行读取的呢,而且做实验发现是一行一行读取,每读取一行就会得到一个key-value pair,key是Object,value是那一行文本。这些细节都还不清楚是在哪什么时候发生的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hadoop WordCount是一个经典的MapReduce程序,用于对文本数据进行词频统计。实现方法如下: 1. Map阶段:将文本数据按照指定的分隔符进行切分,将每个单词作为Key,将频率作为Value传递给Reduce阶段。 2. Reduce阶段:将Map阶段传递过来的Key-Value对进行合并,统计每个单词在文本出现的次数,输出最终的词频统计结果。 以下是Hadoop WordCount的Java代码示例: ```java 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(); Job job = Job.getInstance(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(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 以上代码,TokenizerMapper类实现了Map阶段的逻辑,IntSumReducer类实现了Reduce阶段的逻辑,main函数用于配置和提交MapReduce作业。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值