博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MapReduce练习——手机流量案例分析2
阅读量:3958 次
发布时间:2019-05-24

本文共 4372 字,大约阅读时间需要 14 分钟。

要求

对上一个输出的结果,按照upPackNo顺序排列

 

代码实现

实体类

public class TelphoneName implements  Writable{    private String phoneNo;    private int upPackNo;    private int downPackNo;    private int upPayLoad;    private int downPayLoad;    public String getPhoneNo() {        return phoneNo;    }    public void setPhoneNo(String phoneNo) {        this.phoneNo = phoneNo;    }    public int getUpPackNo() {        return upPackNo;    }    public void setUpPackNo(int upPackNo) {        this.upPackNo = upPackNo;    }    public int getDownPackNo() {        return downPackNo;    }    public void setDownPackNo(int downPackNo) {        this.downPackNo = downPackNo;    }    public int getUpPayLoad() {        return upPayLoad;    }    public void setUpPayLoad(int upPayLoad) {        this.upPayLoad = upPayLoad;    }    public int getDownPayLoad() {        return downPayLoad;    }    public void setDownPayLoad(int downPayLoad) {        this.downPayLoad = downPayLoad;    }    public TelphoneName(){    }    public TelphoneName(String phoneNo,int upPackNo,int downPackNo,int upPayLoad,int downPayLoad){        this.phoneNo = phoneNo;        this.upPackNo = upPackNo;        this.downPackNo = downPackNo;        this.upPayLoad = upPayLoad;        this.downPayLoad = downPayLoad;    }    public TelphoneName(String[] split){        this(split[1], Integer.parseInt(split[6]), Integer.parseInt(split[7]),Integer.parseInt(split[8]),Integer.parseInt(split[9]));    }    @Override    public void write(DataOutput out) throws IOException {        out.writeUTF(phoneNo);        out.writeInt(upPackNo);        out.writeInt(downPackNo);        out.writeInt(upPayLoad);        out.writeInt(downPayLoad);    }    @Override    public void readFields(DataInput in) throws IOException {        phoneNo=in.readUTF();        upPackNo=in.readInt();        downPackNo=in.readInt();        upPayLoad=in.readInt();        downPayLoad=in.readInt();    }    @Override    public String toString() {        return upPackNo+"\t"+downPackNo+"\t"+upPayLoad+"\t"+downPayLoad;    }}

 

TrafficCountMapper 

public class TrafficCountMapper extends Mapper
{ @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] split = value.toString().split("\\s"); context.write(new Text(split[1]),new TelphoneName(split)); }}

 

TrafficCountReducer 

public class TrafficCountReducer extends Reducer
{ List
list = new ArrayList<>(); @Override protected void reduce(Text key, Iterable
values, Context context) throws IOException, InterruptedException { int upPackNo =0; int downPackNo =0; int upPayLoad =0; int downPayLoad =0; for(TelphoneName telphoneName:values){ upPackNo += telphoneName.getUpPackNo(); downPackNo += telphoneName.getDownPackNo(); upPayLoad += telphoneName.getUpPayLoad(); downPayLoad += telphoneName.getDownPayLoad(); } TelphoneName telphoneName = new TelphoneName(key.toString(),upPackNo,downPackNo,upPayLoad,downPayLoad); list.add(telphoneName); //context.write(key,telphoneName); }

 

驱动

//分区class MyPartitioner extends Partitioner
{ @Override public int getPartition(Text key, TelphoneName telphoneName, int i) { String keyString = key.toString(); return (keyString.length()==11)?0:1; }}public class Reducer { public static void main(String[] args) throws Exception { //实现一个job的实例 Configuration configuration = new Configuration(); Job job = Job.getInstance(configuration); //设置jar加载路径 job.setJarByClass(Reducer.class); //设置mapper 和reducer类路径 job.setMapperClass(TrafficCountMapper.class); job.setReducerClass(TrafficCountReducer.class); //设置mapper 输出 job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(TelphoneName.class); //设置reducer输出 job.setOutputKeyClass(Text.class); job.setOutputValueClass(TelphoneName.class); //map阶段执行分区 job.setPartitionerClass(MyPartitioner.class); job.setNumReduceTasks(2); //设置输入输出 FileInputFormat.setInputPaths(job,new Path(args[0])); FileOutputFormat.setOutputPath(job,new Path(args[1])); //提交 job.waitForCompletion(true); }}

 

转载地址:http://riazi.baihongyu.com/

你可能感兴趣的文章
GDB调试各功能总结
查看>>
"undefined reference to" 多种可能出现的问题解决方法
查看>>
类结构定义
查看>>
Windows下关于多线程类 CSemaphore,CMutex,CCriticalSection,CEvent,信号量CSemaphore的使用介绍
查看>>
图像处理基本算法(汇总)以及实现
查看>>
C++编程获取本机网卡信息 本机IP 包括Windows和Linux
查看>>
23种设计模式详解及C++实现
查看>>
C++连接CTP接口实现简单量化交易
查看>>
服务端使用c++实现websocket协议解析及通信
查看>>
C# string.Format使用说明
查看>>
Linux下安装Mysql数据库开发环境
查看>>
Linux用户及用户组添加和删除操作
查看>>
通用 Makefile 的编写方法以及多目录 makefile 写法
查看>>
C++的4种智能指针剖析使用
查看>>
RPC框架实现之容灾策略
查看>>
Docker私库
查看>>
hdu——1106排序(重定向)
查看>>
hdu——1556Color the ball(树状数组)
查看>>
hdu——1541Stars(树状数组)
查看>>
快速幂的精简代码
查看>>