package com.ruozedata.bigdata.hadoop.mapreduce.wc;
import com.ruozedata.bigdata.hadoop.utils.FileUtils;
import org.apache.hadoop.conf.Configuration;
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.NLineInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class WordCountApp {
//套路编程
public static void main(String[] args) throws Exception{
//获取job对象
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
String input="data/wc.data";
String output="out/";
FileUtils.deleteTarget(output,configuration);
//设置jar相关信息
job.setJarByClass(WordCountApp.class);
//设置自定义的mapper跟Reducer
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReducer.class);
//设置map端做一个类似reduce的操作
job.setCombinerClass(MyReducer.class);
//设置mapper阶段输出的key跟value类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//设置reducer阶段输出的key跟vlaue类型
job.setoutputKeyClass(Text.class);
job.setoutputValueClass(IntWritable.class);
//设置输入输出路径
FileInputFormat.setInputPaths(job,new Path(input));
FileOutputFormat.setoutputPath(job,new Path(output));
//提交job
boolean result = job.waitForCompletion(true);
System.exit(result?0:1);
}
public static class MyMapper extends Mapper<LongWritable, Text,Text, IntWritable>{
IntWritable ONE= new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] splits = value.toString().split(",");
for(String split:splits){
context.write(new Text(split),ONE);
}
}
}
public static class MyReducer extends Reducer<Text, IntWritable,Text, IntWritable>{
@Override
protected void reduce(Text word, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int count = 0;
for(IntWritable value:values){
count += value.get();
}
context.write(word,new IntWritable(count));
}
}
}
WordCount源码分析
waitForCompletion{
submit(){
connect(); // 其实就是为了拿到一个cluster(Local YARN)
submitJobInternal(){
checkSpecs(job); // 为了检查输出目录是否存在
// 创建一个staging目录
Path jobStagingArea = JobSubmissionFiles.getStagingDir(cluster, conf);
// 获取该Job的id
JobID jobId = submitClient.getNewJobID();
job.setJobID(jobId);
// 本Job对应的目录: staging + jobid
Path submitJobDir = new Path(jobStagingArea, jobId.toString());
// 本Job的信息
copyAndConfigureFiles(job, submitJobDir);
// 获取该Job对应的map数量
int maps = writeSplits(job, submitJobDir);
// 本Job的信息写入到指定的文件中去
writeConf(conf, submitJobFile);
// 删除该Job对应的文件目录中的信息
jtFs.delete(submitJobDir, true);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。