数据内容
公司名 总收入 总支出
1 apple 1520 1002 apple 3421 2543 apple 4500 3641 huawei 3700 2542 huawei 2700 3543 huawei 5700 5541 xiaomi 3521 2542 xiaomi 3123 3543 xiaomi 3412 554
目标:获得公司总利润,即总收入-总支出
1、companyDoubleMr.java
package hadoop_test.mutil_mr_10.company;import org.apache.hadoop.io.WritableComparable;import java.io.DataInput;import java.io.DataOutput;import java.io.IOException;public class DoubleMr implements WritableComparable
package hadoop_test.mutil_mr_10.mr1;import hadoop_test.Utils_hadoop;import hadoop_test.mutil_mr_10.company.DoubleMr;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.NullWritable;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;import java.awt.*;public class FirstMrDriver {public static void main(String[] args) throws Exception {System.setProperty("HADOOP_USER_NAME", "root");Configuration conf=new Configuration();Job job=Job.getInstance(conf);job.setJarByClass(FirstMrDriver.class);job.setMapperClass(FirstMrMapper.class);job.setReducerClass(FirstMrReducer.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(DoubleMr.class);job.setOutputKeyClass(DoubleMr.class);job.setOutputValueClass(NullWritable.class);FileInputFormat.setInputPaths(job,new Path("/hadoop_test/muti_mr/mutil_mr.txt"));FileOutputFormat.setOutputPath(job,new Path("/hadoop_test/muti_mr/result"));job.waitForCompletion(true);}}
FirstMrMapperpackage hadoop_test.mutil_mr_10.mr1;import hadoop_test.mutil_mr_10.company.DoubleMr;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;public class FirstMrMapper extends Mapper
package hadoop_test.mutil_mr_10.mr1;import hadoop_test.mutil_mr_10.company.DoubleMr;import org.apache.hadoop.io.NullWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class FirstMrReducer extends Reducer
package hadoop_test.mutil_mr_10.mr2;import hadoop_test.Utils_hadoop;import hadoop_test.mutil_mr_10.company.DoubleMr;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.NullWritable;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class SecondMrDriver {public static void main(String[] args) throws Exception {System.setProperty("HADOOP_USER_NAME", "root");Configuration conf=new Configuration();Job job=Job.getInstance(conf);job.setJarByClass(SecondMrDriver.class);job.setMapperClass(SecondMrMapper.class);job.setMapOutputKeyClass(DoubleMr.class);job.setMapOutputValueClass(NullWritable.class);FileInputFormat.setInputPaths(job,new Path("/hadoop_test/muti_mr/result"));FileOutputFormat.setOutputPath(job,new Path("/hadoop_test/muti_mr/result01"));job.waitForCompletion(true);}}
依赖于mr1输出的文件结果result01,只有等到结果输出后,mr2中才能正常启动
SecondMapperpackage hadoop_test.mutil_mr_10.mr2;import hadoop_test.mutil_mr_10.company.DoubleMr;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.NullWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;import java.io.IOException;public class SecondMrMapper extends Mapper
输出结果
mr1输出
经过mr2后