public class PageCountStep1 {
public static class PageCountStep1Mapper extends Mapper<LongWritable, Text, Text, IntWritable>{
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] split = line.split(" ");
context.write(new Text(split[1]), new IntWritable(1));
}
}
public static class PageCountStep1Reducer extends Reducer<Text, IntWritable, Text, IntWritable>{
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int count = 0;
for (IntWritable v : values) {
count += v.get();
}
context.write(key, new IntWritable(count));
}
}
public static void main(String[] args) throws Exception {
/**
* 通过加载classpath下的*-site.xml文件解析参数
*/
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(PageCountStep1.class);
job.setMapperClass(PageCountStep1Mapper.class);
job.setReducerClass(PageCountStep1Reducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.setInputPaths(job, new Path("F:\\mrdata\\url\\input"));
FileOutputFormat.setOutputPath(job, new Path("F:\\mrdata\\url\\countout"));
job.setNumReduceTasks(3);
job.waitForCompletion(true);
}
}
public class PageCountStep2 {
public static class PageCountStep2Mapper extends Mapper<LongWritable, Text, PageCount, NullWritable>{
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, PageCount, NullWritable>.Context context)
throws IOException, InterruptedException {
String[] split = value.toString().split("\t");
PageCount pageCount = new PageCount();
pageCount.set(split[0], Integer.parseInt(split[1]));
context.write(pageCount, NullWritable.get());
}
}
public static class PageCountStep2Reducer extends Reducer<PageCount, NullWritable, PageCount, NullWritable>{
@Override
protected void reduce(PageCount key, Iterable<NullWritable> values,
Reducer<PageCount, NullWritable, PageCount, NullWritable>.Context context)
throws IOException, InterruptedException {
context.write(key, NullWritable.get());
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(PageCountStep2.class);
job.setMapperClass(PageCountStep2Mapper.class);
job.setReducerClass(PageCountStep2Reducer.class);
job.setMapOutputKeyClass(PageCount.class);
job.setMapOutputValueClass(NullWritable.class);
job.setOutputKeyClass(PageCount.class);
job.setOutputValueClass(NullWritable.class);
FileInputFormat.setInputPaths(job, new Path("F:\\mrdata\\url\\countout"));
FileOutputFormat.setOutputPath(job, new Path("F:\\mrdata\\url\\sortout"));
job.setNumReduceTasks(1);
job.waitForCompletion(true);
}
}
---------------------
【转载,仅作分享,侵删】
作者:高辉
原文:https://blog.csdn.net/ZJX103RLF/article/details/89048857
版权声明:本文为博主原创文章,转载请附上博文链接!
|
|