MapReduce案例实操

您所在的位置:网站首页 手机号码100开头 MapReduce案例实操

MapReduce案例实操

2023-07-01 20:38| 来源: 网络整理| 查看: 265

通过MapReduce原理进行简单的分区。

 操作内容  1. 需求  将统计结果按照手机号前三位输出到不同文件中(分区)  (1) 输入数据 

       (2) 期望输出数据  手机号 136、137、138、139 开头都分别放到一个独 立的 4 个文件中,其他开头放到一个文件中  (3) 在序列化作业的基础上,增加一个分区类 

2. 需求分析  1. 需求:将统计结果按照手机归属地不同省份输出到不同文 件中(分区)  2. 数据输入   

3. 期望数据输出 

   

4. 增加一个 ProvincePartitioner 分区    5. Driver 驱动类 

 

这是所用到的phone_data.txt

1 13736230513 192.196.100.1 www.atguigu.com 2481 24681 200 2 13846544121 192.196.100.2 264 0 200 3 13956435636 192.196.100.3 132 1512 200 4 13966251146 192.168.100.1 240 0 404 5 18271575951 192.168.100.2 www.atguigu.com 1527 2106 200 6 84188413 192.168.100.3 www.atguigu.com 4116 1432 200 7 13590439668 192.168.100.4 1116 954 200 8 15910133277 192.168.100.5 www.hao123.com 3156 2936 200 9 13729199489 192.168.100.6 240 0 200 10 13630577991 192.168.100.7 www.shouhu.com 6960 690 200 11 15043685818 192.168.100.8 www.baidu.com 3659 3538 200 12 15959002129 192.168.100.9 www.atguigu.com 1938 180 500 13 13560439638 192.168.100.10 918 4938 200 14 13470253144 192.168.100.11 180 180 200 15 13682846555 192.168.100.12 www.qq.com 1938 2910 200 16 13992314666 192.168.100.13 www.gaga.com 3008 3720 200 17 13509468723 192.168.100.14 www.qinghua.com 7335 110349 404 18 18390173782 192.168.100.15 www.sogou.com 9531 2412 200 19 13975057813 192.168.100.16 www.baidu.com 11058 48243 200 20 13768778790 192.168.100.17 120 120 200 21 13568436656 192.168.100.18 www.alibaba.com 2481 24681 200 22 13568436656 192.168.100.19 1116 954 200

根据MapReduce原理编写FlowBean、FlowMapper、FlowReducer、FlowDriver、FlowPartitioner代码:

FlowBean.java

package com.mapreduce.writable; import org.apache.hadoop.io.Writable; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; public class FlowBean implements Writable { private long upFlow;//上行流量 private long downFlow;//下行流量 private long sumFlow;//总流量 //空参构造 public FlowBean() { } public long getUpFlow() { return upFlow; } public void setUpFlow(long upFlow) { this.upFlow = upFlow; } public long getDownFlow() { return downFlow; } public void setDownFlow(long downFlow) { this.downFlow = downFlow; } public long getSumFlow() { return sumFlow; } public void setSumFlow(long sumFlow) { this.sumFlow = sumFlow; } public void setSumFlow() { this.sumFlow = this.upFlow+this.downFlow; } @Override public void write(DataOutput dataOutput) throws IOException { dataOutput.writeLong(upFlow); dataOutput.writeLong(downFlow); dataOutput.writeLong(sumFlow); } @Override public void readFields(DataInput dataInput) throws IOException { this.upFlow=dataInput.readLong(); this.downFlow=dataInput.readLong(); this.sumFlow=dataInput.readLong(); } @Override public String toString() { return upFlow + "\t" +downFlow +"\t" +sumFlow; } }

FlowDriver.java

package com.mapreduce.writable; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import java.io.IOException; public class FlowDriver { public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { //1.获取配置信息,获取job对象实例 Configuration conf = new Configuration(); Job job = Job.getInstance(conf); //2.指定本程序的jar包所在的本利路径 job.setJarByClass(FlowDriver.class); //3.关联Mapper Reducer job.setMapperClass(FlowMapper.class); job.setReducerClass(FlowReducer.class); //4.指定Mapper输出数据的kv类型 job.setMapOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); //5.指定最终输出的数据的kv类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(FlowBean.class); //指定自定义分区 job.setPartitionerClass(FlowPartitioner.class); //指定相应数量的ReduceTask job.setNumReduceTasks(5); //6.设置出入路径和输出路径 FileInputFormat.setInputPaths(job,new Path("E:\\HAOHAO\\input\\phone_data.txt")); FileOutputFormat.setOutputPath(job,new Path("E:\\HAOHAO\\output")); //7.提交作业 boolean result = job.waitForCompletion(true); System.exit(result ? 0:1); } }

(这里的new path自己设置,将phone_data.txt放到指定的input文件夹下,注意路径一定要写对,output文件夹不用创建,代码运行之后自动生成,为了方便可以把input和output文件夹放在同一目录下)

FlowMapper.java

package com.mapreduce.writable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; public class FlowMapper extends Mapper { private Text outK=new Text(); private FlowBean outV=new FlowBean(); @Override protected void map(LongWritable key, Text value, Mapper.Context context) throws IOException, InterruptedException { //1.获取一行数据 转成字符串 String line=value.toString(); //2.切割数据 String[] split=line.split("\t"); //3.抓取想要的数据 String phone=split[1]; String up=split[split.length-3]; String down=split[split.length-2]; //4.封装数据 outK outV outK.set(phone); outV.setUpFlow(Long.parseLong(up)); outV.setDownFlow(Long.parseLong(down)); outV.setSumFlow(); //5.写出outK outV context.write(outK,outV); } }

FlowReducer.java

package com.mapreduce.writable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; public class FlowReducer extends Reducer { private FlowBean outV=new FlowBean(); @Override protected void reduce(Text key, Iterable values, Reducer.Context context) throws IOException, InterruptedException { //遍历values,分别累加上行流量、下行流量 long totalUp=0; long totalDown=0; for (FlowBean value:values){ totalUp+=value.getUpFlow(); totalDown+=value.getDownFlow(); } //封装outK outV outV.setUpFlow(totalUp); outV.setDownFlow(totalDown); outV.setSumFlow(); //写出outk,outV context.write(key,outV); } }

FlowPartitioner.java

package com.mapreduce.writable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Partitioner; public class FlowPartitioner extends Partitioner { @Override public int getPartition(Text text, FlowBean flowBean, int i) { String phone = text.toString(); String subphone = phone.substring(0,3); int partition; if ("136".equals(subphone)){ partition=0; }else if ("137".equals(subphone)) { partition = 1; }else if ("138".equals(subphone)){ partition=2; }else if ("139".equals(subphone)) { partition = 3; } else { partition = 4; } return partition; } }

运行结果:

 

  (老师上课的案例,自己做了一遍,希望对大家有所帮助,如有错误,还请大佬指正)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3