MapReduce开发自己的wordcount程序
1、Mapper阶段
2、Reducer阶段
3、主程序job阶段
=====================================================================
1、Mapper阶段
package demo.wc;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
// k1 v1 k2 v2
public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
@Override
protected void map(LongWritable k1, Text v1, Context context)
throws IOException, InterruptedException {
/*
* context代表Map的上下文
* 上文:HDFS
* 下文是:Reducer
*/
//数据: I love Beijing
String data = v1.toString();
//分词
String[] words = data.split(" ");
//输出: k2 v2
for(String w:words){
context.write(new Text(w), new LongWritable(1));
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
2、Reducer阶段
package demo.wc;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
// k3 v3 k4 v4
public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
@Override
protected void reduce(Text k3, Iterable<LongWritable> v3,Context context) throws IOException, InterruptedException {
/*
* context 代表Reducer上下文
* 上文:mapper
* 下文:HDFS
*/
long total = 0;
for(LongWritable l:v3){
total = total + l.get();
}
//输出 k4 v4
context.write(k3, new LongWritable(total));
}
}
--------------------------------------------------------------------------------------------------------------------
3、主程序job阶段
//创建一个job = mapper + reducer
//指定job的入口
//指定任务的mapper和输出数据类型
//指定任务的reducer和输出数据类型
//指定输入的路径和输出的路径
//执行任务
package demo.wc;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCountMain {
public static void main(String[] args) throws Exception {
//创建一个job = mapper + reducer
Job job = Job.getInstance(new Configuration());
//指定job的入口
job.setJarByClass(WordCountMain.class);
//指定任务的mapper和输出数据类型
job.setMapperClass(WordCountMapper.class);
job.setMapOutputKeyClass(Text.class); //指定k2的类型
job.setMapOutputValueClass(LongWritable.class);//指定v2的数据类型
//指定任务的reducer和输出数据类型
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);//指定k4的类型
job.setOutputValueClass(LongWritable.class);//指定v4的类型
//指定输入的路径和输出的路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//执行任务
job.waitForCompletion(true);
}
}