package com.founder.hadoop.mapreduce;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class WordCount extends Configured implements Tool{
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
private static IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
protected 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();
@Override
protected 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);
}
}
@Override
public int run(String[] arg) throws Exception {
Configuration conf = getConf();
Job job = new Job(conf, "wordcount");// 任务名称
job.setJarByClass(WordCount.class);// 指定class
FileInputFormat.addInputPath(job, new Path("/TestDir/hadoop.txt"));// 输入路径
FileOutputFormat.setOutputPath(job, new Path("/TestDir/output"));// 输出路径
job.setMapperClass(TokenizerMapper.class);//调用上面Map类作为作为map任务代码
job.setReducerClass(IntSumReducer.class);
job.setOutputFormatClass(TextOutputFormat.class);//
job.setOutputKeyClass(Text.class);//指定输出key的格式
job.setOutputValueClass(IntWritable.class);//指定输出value的格式
job.waitForCompletion(true);
return job.isSuccessful() ? 0 : 1;
}
/**
* @param args
*/
public static void main(String[] args) {
int res=0;
try {
res = ToolRunner.run(new Configuration(), new WordCount(),args);
} catch (Exception e) {
e.printStackTrace();
}
System.exit(res);
}
}
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class WordCount extends Configured implements Tool{
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
private static IntWritable one = new IntWritable(1);
private Text word = new Text();
@Override
protected 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();
@Override
protected 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);
}
}
@Override
public int run(String[] arg) throws Exception {
Configuration conf = getConf();
Job job = new Job(conf, "wordcount");// 任务名称
job.setJarByClass(WordCount.class);// 指定class
FileInputFormat.addInputPath(job, new Path("/TestDir/hadoop.txt"));// 输入路径
FileOutputFormat.setOutputPath(job, new Path("/TestDir/output"));// 输出路径
job.setMapperClass(TokenizerMapper.class);//调用上面Map类作为作为map任务代码
job.setReducerClass(IntSumReducer.class);
job.setOutputFormatClass(TextOutputFormat.class);//
job.setOutputKeyClass(Text.class);//指定输出key的格式
job.setOutputValueClass(IntWritable.class);//指定输出value的格式
job.waitForCompletion(true);
return job.isSuccessful() ? 0 : 1;
}
/**
* @param args
*/
public static void main(String[] args) {
int res=0;
try {
res = ToolRunner.run(new Configuration(), new WordCount(),args);
} catch (Exception e) {
e.printStackTrace();
}
System.exit(res);
}
}
本文介绍了一个基于Hadoop MapReduce实现的WordCount程序,详细展示了如何通过Java编写Mapper和Reducer来完成单词计数任务,并配置运行作业。
121

被折叠的 条评论
为什么被折叠?



