MapReduce案例:实现用户总流量倒序———compareTo方法

用户总流量分析中我们成功得到了数据如下:

13480253104	180	180	360
13502468823	7335	110349	117684
13560439658	2034	5892	7926
13600217502	1080	186852	187932
13602846565	1938	2910	4848
13660577991	6960	690	7650
13719199419	240	0	240
13726230503	2481	24681	27162
13760778710	120	120	240
13823070001	360	180	540
13826544101	264	0	264
13922314466	3008	3720	6728
13925057413	11058	48243	59301
13926251106	240	0	240
13926435656	132	1512	1644
15013685858	3659	3538	7197
15920133257	3156	2936	6092
15989002119	1938	180	2118
18211575961	1527	2106	3633
18320173382	9531	2412	11943
84138413	4116	1432	5548

接下来我们按照总流量(最后一列)倒序排序:

package cn.itcast.hadoop.flowsum.sort;

import cn.itcast.hadoop.flowsum.Flowbean;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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 java.io.IOException;

public class FlowSumSort {
    public static class FlowSumSortmapper extends Mapper<LongWritable, Text, Flowbean,Text>{
        Text v = new Text();
        Flowbean k = new Flowbean();

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] fields = line.split("\t");
            String phoneNum = fields[0];

            long upFlow = Long.parseLong(fields[1]);
            long downFlow = Long.parseLong(fields[2]);

            k.set(upFlow,downFlow);
            v.set(phoneNum);

            context.write(k,v);
        }
    }
    public static class FlowSumSortReducer extends Reducer<Flowbean,Text,Text,Flowbean>{
        @Override
        protected void reduce(Flowbean arg0, Iterable<Text> arg1, Context arg2) throws IOException, InterruptedException {
            arg2.write(arg1.iterator().next(),arg0);
        }
    }
    public static void main(String[] args) throws Exception{
        Configuration conf = new Configuration();
        conf.set("mapreduce.framework.name","local");
        Job job = Job.getInstance(conf);

        //指定这个job所在的jar包位置
        job.setJarByClass(FlowSumSort.class);

        //指定使用的Mapper和Reducer是哪个类
        job.setMapperClass(FlowSumSortmapper.class);
        job.setReducerClass(FlowSumSortReducer.class);

        //设置业务逻辑Mapper类的输出kv数据类型

        job.setMapOutputKeyClass(Flowbean.class);
        job.setMapOutputValueClass(Text.class);

        //设置业务逻辑Reducer类的输出kv数据类型

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


        FileInputFormat.setInputPaths(job,"E:\\Flowsum\\output");
        FileOutputFormat.setOutputPath(job,new Path("E:\\Flowsum\\outputsort"));

        //向yarn集群提交job
        boolean res = job.waitForCompletion(true);
        System.exit(res?0:1);

    }
}

在Flowbean类中的compareTo方法自定义实现倒序排序。

代码中Mapper思路
将Flowbean类型(总流量)作为key,Text类型(手机号)为value原因是按照key进行排序。
Reduce思路:由于从mapper传过来的value(手机号)本来就是唯一的,所以迭代器中值只有一个。只需对调kv位置。

arg2.write(arg1.iterator().next(),arg0);

最终输出kv为<Text(手机号),Flowbean(总流量) >

运行结果:
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MelodyYN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值