一、小文件优化

1.Mapper类

  1. package com.css.combine;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.hadoop.io.IntWritable;
  6. import org.apache.hadoop.io.LongWritable;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Mapper;
  9.  
  10. /**
  11. * 思路?
  12. * wordcount单词计数
  13. * <单词,1>
  14. *
  15. * 数据传输
  16. *
  17. * KEYIN:数据的起始偏移量0~10 11~20 21~30
  18. * VALUEIN:数据
  19. *
  20. * KEYOUT:mapper输出到reduce阶段 k的类型
  21. * VALUEOUT:mapper输出到reduce阶段v的类型
  22. * <China,1><Beijing,1><love,1>
  23. */
  24. public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
  25. //key 起始偏移量 value 数据 context 上下文
  26. @Override
  27. protected void map(LongWritable key, Text value, Context context)
  28. throws IOException, InterruptedException {
  29. // 1.读取数据
  30. String line = value.toString();
  31. // 2.切割 love Beijing
  32. String[] words = line.split(" ");
  33. // 3.循环的写到下一个阶段<love,1><Beijing,1>
  34. for (String w : words) {
  35. // 4.输出到reducer阶段
  36. context.write(new Text(w), new IntWritable(1));
  37. }
  38. }
  39. }

2.Reducer类

  1. package com.css.combine;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.hadoop.io.IntWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Reducer;
  8.  
  9. public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
  10.  
  11. @Override
  12. protected void reduce(Text key, Iterable<IntWritable> values,
  13. Context context) throws IOException, InterruptedException {
  14. // 统计单词出现的次数
  15. int sum = 0;
  16. // 累加求和
  17. for (IntWritable count : values) {
  18. // 拿到值累加
  19. sum += count.get();
  20. }
  21. // 结果输出
  22. context.write(key, new IntWritable(sum));
  23. }
  24. }

3.Driver类

  1. package com.css.combine;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.IntWritable;
  8. import org.apache.hadoop.io.Text;
  9. import org.apache.hadoop.mapreduce.Job;
  10. import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  13.  
  14. public class WordCountDriver {
  15. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  16. // 获取job信息
  17. Configuration conf = new Configuration();
  18. Job job = Job.getInstance(conf);
  19. // 获取jar包
  20. job.setJarByClass(WordCountDriver.class);
  21. // 获取自定义的mapper与reducer类
  22. job.setMapperClass(WordCountMapper.class);
  23. job.setReducerClass(WordCountReducer.class);
  24. // 设置map输出的数据类型
  25. job.setMapOutputKeyClass(Text.class);
  26. job.setMapOutputValueClass(IntWritable.class);
  27. // 设置reduce输出的数据类型(最终的数据类型)
  28. job.setOutputKeyClass(Text.class);
  29. job.setOutputValueClass(IntWritable.class);
  30. // 指定运行的inputformat方式 默认的方式是textinputformat(小文件优化)
  31. job.setInputFormatClass(CombineTextInputFormat.class);
  32. CombineTextInputFormat.setMaxInputSplitSize(job, 4194304); // 最大4M
  33. CombineTextInputFormat.setMinInputSplitSize(job, 3145728); // 最小3M
  34. // 设置输入存在的路径与处理后的结果路径
  35. FileInputFormat.setInputPaths(job, new Path("c:/in1024/"));
  36. FileOutputFormat.setOutputPath(job, new Path("c:/out1024/"));
  37. // 提交任务
  38. boolean rs = job.waitForCompletion(true);
  39. System.out.println(rs ? 0 : 1);
  40. }
  41. }

二、分区

1.Mapper类

  1. package com.css.flow.partition;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.hadoop.io.LongWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Mapper;
  8.  
  9. /**
  10. * 3631279850362 13726130503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 www.itstaredu.com 教育网站 24 27 299 681 200
  11. *
  12. * 13726130503 299 681 980
  13. */
  14. public class FlowCountMapper extends Mapper<LongWritable, Text, Text, FlowBean>{
  15. @Override
  16. protected void map(LongWritable key, Text value, Context context)
  17. throws IOException, InterruptedException {
  18. // 1.获取数据
  19. String line = value.toString();
  20.  
  21. // 2.切割
  22. String[] fields = line.split("\t");
  23.  
  24. // 3.封装对象 拿到关键字段 数据清洗
  25. String phoneN = fields[1];
  26. long upFlow = Long.parseLong(fields[fields.length - 3]);
  27. long dfFlow = Long.parseLong(fields[fields.length - 2]);
  28.  
  29. // 4.输出到reduce端
  30. context.write(new Text(phoneN), new FlowBean(upFlow, dfFlow));
  31. }
  32. }

2.Reducer类

  1. package com.css.flow.partition;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.hadoop.io.Text;
  6. import org.apache.hadoop.mapreduce.Reducer;
  7.  
  8. public class FlowCountReducer extends Reducer<Text, FlowBean, Text, FlowBean>{
  9. @Override
  10. protected void reduce(Text key, Iterable<FlowBean> values, Context context)
  11. throws IOException, InterruptedException {
  12. // 1.相同手机号 的流量使用再次汇总
  13. long upFlow_sum = 0;
  14. long dfFlow_sum = 0;
  15.  
  16. // 2.累加
  17. for (FlowBean f : values) {
  18. upFlow_sum += f.getUpFlow();
  19. dfFlow_sum += f.getDfFlow();
  20. }
  21. FlowBean rs = new FlowBean(upFlow_sum, dfFlow_sum);
  22. // 3.输出
  23. context.write(key, rs);
  24. }
  25. }

3.封装类

  1. package com.css.flow.partition;
  2.  
  3. import java.io.DataInput;
  4. import java.io.DataOutput;
  5. import java.io.IOException;
  6.  
  7. import org.apache.hadoop.io.Writable;
  8.  
  9. /**
  10. * 封装类 数据的传输
  11. */
  12. public class FlowBean implements Writable{
  13. // 定义属性
  14. private long upFlow;
  15. private long dfFlow;
  16. private long flowSum;
  17.  
  18. public FlowBean() {
  19. }
  20.  
  21. // 流量累加
  22. public FlowBean(long upFlow, long dfFlow) {
  23. this.upFlow = upFlow;
  24. this.dfFlow = dfFlow;
  25. this.flowSum = upFlow + dfFlow;
  26. }
  27.  
  28. // 反序列化
  29. @Override
  30. public void readFields(DataInput in) throws IOException {
  31. upFlow = in.readLong();
  32. dfFlow = in.readLong();
  33. flowSum = in.readLong();
  34. }
  35.  
  36. // 序列化
  37. @Override
  38. public void write(DataOutput out) throws IOException {
  39. out.writeLong(upFlow);
  40. out.writeLong(dfFlow);
  41. out.writeLong(flowSum);
  42. }
  43.  
  44. @Override
  45. public String toString() {
  46. return upFlow + "\t" + dfFlow + "\t" + flowSum;
  47. }
  48.  
  49. public long getUpFlow() {
  50. return upFlow;
  51. }
  52.  
  53. public void setUpFlow(long upFlow) {
  54. this.upFlow = upFlow;
  55. }
  56.  
  57. public long getDfFlow() {
  58. return dfFlow;
  59. }
  60.  
  61. public void setDfFlow(long dfFlow) {
  62. this.dfFlow = dfFlow;
  63. }
  64.  
  65. public long getFlowSum() {
  66. return flowSum;
  67. }
  68.  
  69. public void setFlowSum(long flowSum) {
  70. this.flowSum = flowSum;
  71. }
  72. }

4.分区类

  1. package com.css.flow.partition;
  2.  
  3. import org.apache.hadoop.io.Text;
  4. import org.apache.hadoop.mapreduce.Partitioner;
  5.  
  6. public class PhoneNumPartitioner extends Partitioner<Text, FlowBean>{
  7.  
  8. // 根据手机号前三位进行分区
  9. @Override
  10. public int getPartition(Text key, FlowBean value, int numPartitions) {
  11. // 获取手机号前三位
  12. String phoneNum = key.toString().substring(0, 3);
  13. // 分区
  14. int partitioner = 4;
  15.  
  16. if ("135".equals(phoneNum)) {
  17. return 0;
  18. }else if ("137".equals(phoneNum)) {
  19. return 1;
  20. }else if ("138".equals(phoneNum)) {
  21. return 2;
  22. }else if ("139".equals(phoneNum)) {
  23. return 3;
  24. }
  25. return partitioner;
  26. }
  27. }

5.Driver类

  1. package com.css.flow.partition;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Job;
  9. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  10. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  11.  
  12. public class FlowCountDriver {
  13. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  14. // 1.获取job信息
  15. Configuration conf = new Configuration();
  16. Job job = Job.getInstance(conf);
  17.  
  18. // 2.获取jar包
  19. job.setJarByClass(FlowCountDriver.class);
  20.  
  21. // 3.获取自定义的mapper与reducer类
  22. job.setMapperClass(FlowCountMapper.class);
  23. job.setReducerClass(FlowCountReducer.class);
  24.  
  25. // 4.设置map输出的数据类型
  26. job.setMapOutputKeyClass(Text.class);
  27. job.setMapOutputValueClass(FlowBean.class);
  28.  
  29. // 5.设置reduce输出的数据类型(最终的数据类型)
  30. job.setOutputKeyClass(Text.class);
  31. job.setOutputValueClass(FlowBean.class);
  32.  
  33. // 设置自定义的分区类
  34. // 自定义分区个数要大于分区数
  35. job.setPartitionerClass(PhoneNumPartitioner.class);
  36. job.setNumReduceTasks(5);
  37.  
  38. // 6.设置输入存在的路径与处理后的结果路径
  39. FileInputFormat.setInputPaths(job, new Path("c:/flow1020/in"));
  40. FileOutputFormat.setOutputPath(job, new Path("c:/flow1020/out"));
  41.  
  42. // 7.提交任务
  43. boolean rs = job.waitForCompletion(true);
  44. System.out.println(rs ? 0 : 1);
  45. }
  46. }

6.输入的文件HTTP_20180313143750.dat

  1. 3631279850362 13726130503 00-FD-07-A4-72-B8:CMCC 120.196.100.82 www.itstaredu.com 教育网站 24 27 299 681 200
  2. 3631279950322 13822544101 5C-0E-8B-C7-F1-E0:CMCC 120.197.40.4 www.taobao.com 淘宝网 4 0 264 0 200
  3. 3631279910362 13926435656 20-10-7A-28-CC-0A:CMCC 120.196.100.99 2 4 132 1512 200
  4. 3631244000322 13926251106 5C-0E-8B-8B-B1-50:CMCC 120.197.40.4 4 0 240 0 200
  5. 3631279930342 18212575961 94-71-AC-CD-E6-18:CMCC-EASY 120.196.100.99 iface.qiyi.com 视频网站 15 12 1527 2106 200
  6. 3631279950342 13884138413 5C-0E-8B-8C-E8-20:7DaysInn 120.197.40.4 122.72.52.12 20 16 4116 1432 200
  7. 3631279930352 13510439658 C4-17-FE-BA-DE-D9:CMCC 120.196.100.99 18 15 1116 954 200
  8. 3631279950332 15920133257 5C-0E-8B-C7-BA-20:CMCC 120.197.40.4 sug.so.360.cn 信息安全 20 20 316 296 200
  9. 3631279830392 13719199419 68-A1-B7-03-07-B1:CMCC-EASY 120.196.100.82 4 0 240 0 200
  10. 3631279840312 13660577991 5C-0E-8B-92-5C-20:CMCC-EASY 120.197.40.4 s19.cnzz.com 站点统计 24 9 660 690 200
  11. 3631279730382 15013685858 5C-0E-8B-C7-F7-90:CMCC 120.197.40.4 rank.ie.sogou.com 搜索引擎 28 27 369 338 200
  12. 3631279860392 15889002119 E8-99-C4-4E-93-E0:CMCC-EASY 120.196.100.99 www.umeng.com 站点统计 3 3 938 380 200
  13. 3631279920332 13560439658 C4-17-FE-BA-DE-D9:CMCC 120.196.100.99 15 9 918 4938 200
  14. 3631279860312 13480253104 5C-0E-8B-C7-FC-80:CMCC-EASY 120.197.40.4 3 3 120 1320 200
  15. 3631279840302 13602846565 5C-0E-8B-8B-B6-00:CMCC 120.197.40.4 2052.flash2-http.qq.com 综合门户 15 12 198 910 200
  16. 3631279950332 13922314466 00-FD-07-A2-EC-BA:CMCC 120.196.100.82 img.qfc.cn 12 12 3008 3720 200
  17. 3631279820302 13502468823 5C-0A-5B-6A-0B-D4:CMCC-EASY 120.196.100.99 y0.ifengimg.com 综合门户 57 102 735 11349 400
  18. 3631279860322 18320173382 84-25-DB-4F-10-1A:CMCC-EASY 120.196.100.99 input.shouji.sogou.com 搜索引擎 21 18 9531 212 200
  19. 3631279900332 13925057413 00-1F-64-E1-E6-9A:CMCC 120.196.100.55 t3.baidu.com 搜索引擎 69 63 11058 4243 200
  20. 3631279880322 13760778710 00-FD-07-A4-7B-08:CMCC 120.196.100.82 2 2 120 120 200
  21. 3631279850362 13726238888 00-FD-07-A4-72-B8:CMCC 120.196.100.82 i02.c.aliimg.com 24 27 2481 24681 200
  22. 3631279930352 13560436666 C4-17-FE-BA-DE-D9:CMCC 120.196.100.99 18 15 1136 94 200
  23. 3631279930353 13560436326 C4-17-FE-BA-DE-D9:CMCC 120.196.100.77 lol.qq.com/ 英雄联盟 18 15 1136 94 200

7.输出的文件

  1. 1part-r-00000
  2. 13502468823 735 11349 12084
  3. 13510439658 1116 954 2070
  4. 13560436326 1136 94 1230
  5. 13560436666 1136 94 1230
  6. 13560439658 918 4938 5856
  7.  
  8. 2part-r-00001
  9. 13719199419 240 0 240
  10. 13726130503 299 681 980
  11. 13726238888 2481 24681 27162
  12. 13760778710 120 120 240
  13.  
  14. 3part-r-00002
  15. 13822544101 264 0 264
  16. 13884138413 4116 1432 5548
  17.  
  18. 4part-r-00003
  19. 13922314466 3008 3720 6728
  20. 13925057413 11058 4243 15301
  21. 13926251106 240 0 240
  22. 13926435656 132 1512 1644
  23.  
  24. 5part-r-00004
  25. 13480253104 120 1320 1440
  26. 13602846565 198 910 1108
  27. 13660577991 660 690 1350
  28. 15013685858 369 338 707
  29. 15889002119 938 380 1318
  30. 15920133257 316 296 612
  31. 18212575961 1527 2106 3633
  32. 18320173382 9531 212 9743

MapReduce小文件优化与分区的更多相关文章

  1. MapReduce小文件处理之CombineFileInputFormat实现

    在MapReduce使用过程中.一般会遇到输入文件特别小(几百KB.几十MB).而Hadoop默认会为每一个文件向yarn申请一个container启动map,container的启动关闭是很耗时的. ...

  2. MaxCompute小文件问题优化方案

    小文件背景知识 小文件定义 分布式文件系统按块Block存放,文件大小比块大小小的文件(默认块大小为64M),叫做小文件. 如何判断存在小文件数量多的问题 查看文件数量 desc extended + ...

  3. [大牛翻译系列]Hadoop(17)MapReduce 文件处理:小文件

    5.1 小文件 大数据这个概念似乎意味着处理GB级乃至更大的文件.实际上大数据可以是大量的小文件.比如说,日志文件通常增长到MB级时就会存档.这一节中将介绍在HDFS中有效地处理小文件的技术. 技术2 ...

  4. 第3节 mapreduce高级:5、6、通过inputformat实现小文件合并成为sequenceFile格式

    1.1 需求 无论hdfs还是mapreduce,对于小文件都有损效率,实践中,又难免面临处理大量小文件的场景,此时,就需要有相应解决方案 1.2 分析 小文件的优化无非以下几种方式: 1.  在数据 ...

  5. Spark优化之小文件是否需要合并?

    我们知道,大部分Spark计算都是在内存中完成的,所以Spark的瓶颈一般来自于集群(standalone, yarn, mesos, k8s)的资源紧张,CPU,网络带宽,内存.Spark的性能,想 ...

  6. Hadoop MapReduce编程 API入门系列之小文件合并(二十九)

    不多说,直接上代码. Hadoop 自身提供了几种机制来解决相关的问题,包括HAR,SequeueFile和CombineFileInputFormat. Hadoop 自身提供的几种小文件合并机制 ...

  7. hive优化之自己主动合并输出的小文件

    1.先在hive-site.xml中设置小文件的标准. <property> <name>hive.merge.smallfiles.avgsize</name> ...

  8. mapreduce 关于小文件导致任务缓慢的问题

    小文件导致任务执行缓慢的原因: 1.很容易想到的是map task 任务启动太多,而每个文件的实际输入量很小,所以导致了任务缓慢 这个可以通过 CombineTextInputFormat,解决,主要 ...

  9. [转载]mapreduce合并小文件成sequencefile

    mapreduce合并小文件成sequencefile http://blog.csdn.net/xiao_jun_0820/article/details/42747537

随机推荐

  1. ORACLE用户角色与授权

    --创建一个用户CREATE USER test_user IDENTIFIED BY test_user; --创建一个角色 CREATE ROLE connect2 ; --为角色授权 GRANT ...

  2. C语言 · 最大乘积

      算法提高 最大乘积   时间限制:1.0s   内存限制:512.0MB      问题描述 对于n个数,从中取出m个数,如何取使得这m个数的乘积最大呢? 输入格式 第一行一个数表示数据组数 每组 ...

  3. ApplicationListener接口中的onApplicationEvent被调用两次解决方式

    Spring容器初始化完毕后,调用BeanPostProcessor这个类,这个类实现ApplicationListener接口,重写onApplicationEvent方法, 方法中就是我们自己要在 ...

  4. js学习笔记22----BOM属性和方法

    BOM基本概念 : Browser Object Model 浏览器对象模型. BOM属性: window.navigator.userAgent : 浏览器信息 判断是否是某个浏览器,可以用 ind ...

  5. jQuery && jEasyUI 扩展功能集合

    jquery-extensions:jQuery && jEasyUI 扩展功能集合 该扩展功能基于 jQuery 1.9.x / 1.10.x / 1.11.x 和 jQuery E ...

  6. 【转】【iOS测试系列】常用测试小插件的使用

    背景介绍 由于iOS系统的限制,在非越狱的自动化测试中无法实现一些常用的功能,比如不同应用之间来回切换.模拟全局的点击事件等等.但是在越狱的环境下,这些限制就不存在了,我们可以利用各种小插件来实现我们 ...

  7. 29Spring_Autowriter的一些疑惑(很重要)

    我用一个Autowriter去注解一个属性,而且我没有在Spring的配置文件中的bean.xml中注册bean(<bean id=""...);那么这个注解有用吗?答案是不 ...

  8. error: icpc: Command not found

    交叉编译qt的程序时,出现错误:error: icpc: Command not found. 解决方法,详情查看链接. http://www.cnblogs.com/zengjfgit/p/4744 ...

  9. 如果可能的话,使用 PC-Lint、LogiScope 等工具进行代码审查

    如果可能的话,使用 PC-Lint.LogiScope 等工具进行代码审查. #include <iostream> #include <algorithm> #include ...

  10. ie中自动识别单屏与双屏(js)

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...