1、PVStartV2App.java
src/main/java/project/mrv2/PVStartV2App.java
升级之处:数据输入来源为input/etl,即ETLApp.java的输出,即不读取全部原始数据,只读取处理后的数据。
package project.mrv2; /* * 第二版本浏览量的统计 */ import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; 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 java.io.IOException; public class PVStartV2App { public static void main(String[] args) throws Exception{ Configuration configuration = new Configuration(); FileSystem fileSystem = FileSystem.get(configuration); Path outputPath = new Path("output/v2/pvstart"); if (fileSystem.exists(outputPath)){ fileSystem.delete(outputPath, true); } Job job = Job .getInstance(configuration); job.setJarByClass(PVStartV2App.class); job.setMapperClass(MyMapper.class); job.setReducerClass(MyReducer.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(LongWritable.class); job.setOutputKeyClass(NullWritable.class); job.setOutputValueClass(LongWritable.class); FileInputFormat.setInputPaths(job, new Path("input/etl")); FileOutputFormat.setOutputPath(job, new Path("output/v2/pvstart")); job.waitForCompletion(true); } static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable>{ //将key固定 private Text KEY = new Text("key"); private LongWritable ONE = new LongWritable(1); //在这里要实现一个map方法 //直接输入map+回车 @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { context.write(KEY, ONE); } } static class MyReducer extends Reducer<Text, LongWritable, NullWritable, LongWritable>{ //在这里要实现一个reduce方法 //直接输入reduce+回车 @Override protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException { long count = 0; for (LongWritable value: values){ count++; } context.write(NullWritable.get(), new LongWritable(count)); } } }