Mapreduce算法三、利用cleanup对reduce结果进行排序

package CartsDemo;

import java.io.IOException;
import java.util.HashMap;
import java.util.Set;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
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 MRDemo.NewTime;



public class carNumber {
public static void main(String[] args) throws Exception {
    if (args.length!=2) {
        System.exit(0);
    }
    Job job = new Job (new Configuration(),"carNumber");
    job.setJarByClass(carNumber.class);
    FileInputFormat.setInputPaths(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    job.setMapperClass(numberMap.class);
    job.setReducerClass(numberReduce.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(IntWritable.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(DoubleWritable.class);
    job.waitForCompletion(true);

}
public static class numberMap extends Mapper<Object, Text, Text, IntWritable>{
    public static boolean isNumeric(String str){
          for (int i = 0; i < str.length(); i++){
           //验证字符串中的字符是不是数字
           if (!Character.isDigit(str.charAt(i))){
            return false;
           }
          }
          return true;
  }
    protected void map(Object key, Text value, org.apache.hadoop.mapreduce.Mapper<Object,Text,Text,IntWritable>.Context context) throws java.io.IOException ,InterruptedException {
        String[] lines = value.toString().split("\t");

        //Integer a = Integer.parseInt.(month);
        String month = lines[1].trim();
        //if(lines.length>1 &&  >=1 &&lines[1]<=12){
        if(lines.length>1 &&isNumeric(lines[1])&&lines[1]!=null){
            context.write(new Text(month),new IntWritable(1));
        }
        /*if (lines.length>1){

        }*/
    };
}
public static class numberReduce extends Reducer<Text, IntWritable, Text, DoubleWritable>{
    Double total = 0.0;
    HashMap<String, Integer> map =  new HashMap<>();
    protected void reduce(Text key, java.lang.Iterable<IntWritable> values, org.apache.hadoop.mapreduce.Reducer<Text,IntWritable,Text,DoubleWritable>.Context context) throws java.io.IOException ,InterruptedException {
        int sum = 0;
        for (IntWritable value : values) {
            sum+=value.get();
        }
        //context.write(key, new IntWritable(sum));
//      把每种车的品牌与总数加入map中,
        map.put(key.toString(),sum );
//      把所有车的总数相加
        total+=sum;
    };
    protected void cleanup(org.apache.hadoop.mapreduce.Reducer<Text,IntWritable,Text,DoubleWritable>.Context context) throws IOException ,InterruptedException {
        Set<String >  set = map.keySet();
        for (String key : set) {
//          通过map中的key获取value
            int count = map.get(key);
//          求出每种车所占卖出总数的比例
            double percent = count/total;
            context.write(new Text(key), new DoubleWritable(percent) );
        }
    };
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值