mapreduce分区输出日期的盈利(收入-支出)求最大

public class Week2Data implements WritableComparable<Week2Data>{
    private String day;
    private Integer balance;

    public Week2Data() {
    }

    public Week2Data(String day,Integer balance) {
        this.balance = balance;
        this.day = day;
    }

    public String getDay() {
        return day;
    }

    public void setDay(String day) {
        this.day = day;
    }

    public Integer getBalance() {
        return balance;
    }

    public void setBalance(Integer balance) {
        this.balance = balance;
    }

    @Override
    public int compareTo(Week2Data o) {
        return o.balance.compareTo(this.balance);
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(day);
        dataOutput.writeInt(balance);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.day=dataInput.readUTF();
        this.balance=dataInput.readInt();
    }
}

 

 

public class MyTest {
    /*static {
        System.setProperty("hadoop.home.dir","D:\\hadoop\\hadoop-2.8.3");
    }*/
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //创建作业
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "week2");
        job.setJarByClass(MyTest.class);
        //输入文件
        FileInputFormat.addInputPath(job,new Path(args[0]));
        //mapper计算
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Week2Data.class);
        //reduce
        job.setNumReduceTasks(2);
        job.setReducerClass(MyReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //输出文件
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        //提交
        boolean b = job.waitForCompletion(true);
        System.out.println(b?1:0);
    }
    public static class MyMapper extends Mapper<LongWritable,Text,Text, Week2Data> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String s = value.toString();
            String[] split = s.split("\\s");
            String year = split[0].substring(0, 4);
            context.write(new Text(year),new Week2Data(split[0], Integer.parseInt(split[1])-Integer.parseInt(split[2])));
        }
    }
    public static class MyReduce extends Reducer<Text,Week2Data,Text,LongWritable> {
        @Override
        protected void reduce(Text key, Iterable<Week2Data> values, Context context) throws IOException, InterruptedException {
            Integer l = 0;
            String s = "";
            for (Week2Data value:values){
                Integer i = value.getBalance();
                if (i>l) {
                    l = i;
                    s = value.getDay();
                }
            }
            context.write(new Text(s),new LongWritable(l));
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值