Wordcount测试用例 (1)

本文详细介绍了如何使用 Java 和 Hadoop MapReduce 实现单词计数程序,包括代码实现、测试数据输入及执行流程,展示了如何将数据从本地文件上传至 HDFS 并进行分布式计算。

1. 代码

package cn.edu.xjtu.wordcount;

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

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class WordCounter extends Configured  implements Tool{
	
	public static class WordCountMapper extends 
	         Mapper<LongWritable, Text, Text, IntWritable>{
		private final static IntWritable one = new IntWritable(1);
		private Text word =  new Text();
		
		public void map(LongWritable key, Text value, Context context) 
		          throws IOException, InterruptedException {
			String line = value.toString();
			StringTokenizer tokenizer = new StringTokenizer(line);
			while(tokenizer.hasMoreElements()){
				word.set(tokenizer.nextToken());
				context.write(word, one);
			}
		}		
	}

	public static class WordCountReducer extends 
	           Reducer<Text, IntWritable, Text, IntWritable>{
		public void reduce(Text key, Iterable<IntWritable> values, Context context) 
		            throws IOException, InterruptedException {
			int sum = 0;
			for(IntWritable val : values){
				sum += val.get();
			}
			context.write(key, new IntWritable(sum));
		}		
	}
	
	public int run(String[] args) throws Exception{
		Job job = new Job(getConf());
		job.setJarByClass(WordCounter.class);
		job.setJobName("WordCount");
		
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		job.setInputFormatClass(TextInputFormat.class);
		job.setOutputFormatClass(TextOutputFormat.class);

		job.setMapperClass(WordCountMapper.class);
		job.setCombinerClass(WordCountReducer.class);
		job.setReducerClass(WordCountReducer.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
				
		boolean success = job.waitForCompletion(true);
		return success ? 0: 1;
	 }
	
	public static void main(String[] args) throws Exception{
		int ret = ToolRunner.run(new WordCounter(), args);
		System.exit(ret);
	}
}

2. 测试数据 input.txt,传到node14

3. 从eclipse中export出app.jar,传到node14

4. 执行

       [hadoop@node14 app]$ hadoop fs -put input.txt input.txt

       [hadoop@node14 app]$ hadoop jar app.jar cn.edu.xjtu.wordcount.WordCounter input.txt outputDir

      查看结果

      [hadoop@node14 app]$ hadoop fs -ls outputDir

      [hadoop@node14 app]$ hadoop fs -cat outputDir/part-r-00000

     

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值