Hadoop之MapReduce (Partitioner)

今天我们用Partitioner按电话号查找需要字段的加和。

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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 org.apache.log4j.BasicConfigurator;

import javax.tools.ForwardingJavaFileObject;
import java.io.IOException;

public class FlowSum {
    public static class FlowSumMap extends Mapper<LongWritable, Text,Text, IntWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] split = value.toString().split("\t");
            //取出电话号
            String tellNum=split[1];
            //取出需要得数据,,加和
            Integer upFlow = Integer.parseInt(split[8]);
            Integer downFlow = Integer.parseInt(split[9]);
            Integer sumFlow = upFlow + downFlow;
            context.write(new Text(tellNum),new IntWritable(sumFlow));
        }
    }
    public static class FlowSumReduce extends Reducer<Text,IntWritable,Text,IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            Integer heFlow = 0;
            for (IntWritable value : values) {
                //重复的电话号也会加起来,
                heFlow += value.get();
            }
            context.write(key,new IntWritable(heFlow));
        }
    }

    public static void main(String[] args) throws Exception {
        BasicConfigurator.configure();//自动快速使用缺省log4j的环境
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(FlowSum.class);
        //里面参数是分区,写 1 是默认值 不分区。都在一个里。不能少于你所定义的分区个数。
        job.setNumReduceTasks(8);
        //具体使用了哪个partition
        job.setPartitionerClass(PrivatePatitioner.class);


        job.setMapperClass(FlowSumMap.class);
        job.setReducerClass(FlowSumReduce.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job,new Path("D:\\eclipse\\wc\\input\\flow.log"));
        FileOutputFormat.setOutputPath(job,new Path("D:\\eclipse\\wc\\outputflow"));

        job.submit();
        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}

Partitioner


import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

import java.util.HashMap;

public class PrivatePatitioner extends Partitioner<Text, IntWritable> {
    private static HashMap<String,Integer> privateMap = new HashMap<>();
    static {
        privateMap.put("135",0);
        privateMap.put("136",1);
        privateMap.put("137",2);
        privateMap.put("138",3);
        privateMap.put("139",4);
    }
    @Override
    public int getPartition(Text text, IntWritable intWritable, int i) {
        String tellNum = text.toString().substring(0,3);
        Integer integer = privateMap.get(tellNum);
        //如果integer 没有值 说明不在上面的字段中。除了上面的字段,其他的都放在5
        return integer == null ? 5 : integer;
    }
}

结果
在这里插入图片描述
内容
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值