转换原始数据为块压缩的SequenceFIle

  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.conf.Configured;
  3. import org.apache.hadoop.fs.FileSystem;
  4. import org.apache.hadoop.fs.Path;
  5. import org.apache.hadoop.io.LongWritable;
  6. import org.apache.hadoop.io.SequenceFile.CompressionType;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.io.compress.GzipCodec;
  9. import org.apache.hadoop.mapreduce.Job;
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  12. import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
  13. import org.apache.hadoop.util.Tool;
  14. import org.apache.hadoop.util.ToolRunner;
  15.  
  16. import com.hadoop.compression.lzo.LzoCodec;
  17.  
  18. public class ToSeqFile extends Configured implements Tool {
  19. @Override
  20. public int run(String[] arg0) throws Exception {
  21. Job job = new Job();
  22. job.setJarByClass(getClass());
  23. Configuration conf=getConf();
  24. FileSystem fs = FileSystem.get(conf);
  25.  
  26. FileInputFormat.setInputPaths(job, "/home/hadoop/tmp/tmplzo.txt");
  27. Path outDir=new Path("/home/hadoop/tmp/tmplzo.out");
  28. fs.delete(outDir,true);
  29. FileOutputFormat.setOutputPath(job, outDir);
  30.  
  31. //job.setMapperClass(IndentityMapper);
  32. job.setNumReduceTasks(0);
  33. job.setOutputKeyClass(LongWritable.class);
  34. job.setOutputValueClass(Text.class);
  35. //设置OutputFormat为SequenceFileOutputFormat
  36. job.setOutputFormatClass(SequenceFileOutputFormat.class);
  37. //允许压缩
  38. SequenceFileOutputFormat.setCompressOutput(job, true);
  39. //压缩算法为gzip
  40. SequenceFileOutputFormat.setOutputCompressorClass(job, LzoCodec.class);
  41. //压缩模式为BLOCK
  42. SequenceFileOutputFormat.setOutputCompressionType(job, CompressionType.BLOCK);
  43.  
  44. return job.waitForCompletion(true)?0:1;
  45. }
  46.  
  47. public static void main(String[] args) throws Exception {
  48. int res = ToolRunner.run(new Configuration(), new ToSeqFile(), args);
  49. System.exit(res);
  50. }
  51. }

MR处理压缩后的sequenceFile

  1. import org.apache.hadoop.io.Text;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.net.URI;
  6. import java.util.Iterator;
  7. import java.util.StringTokenizer;
  8.  
  9. import org.apache.hadoop.conf.Configuration;
  10. import org.apache.hadoop.conf.Configured;
  11. import org.apache.hadoop.fs.FileSystem;
  12. import org.apache.hadoop.fs.Path;
  13. import org.apache.hadoop.io.IntWritable;
  14. import org.apache.hadoop.io.NullWritable;
  15. import org.apache.hadoop.io.Text;
  16. import org.apache.hadoop.io.compress.*;
  17. import org.apache.hadoop.mapreduce.ContextFactory;
  18. import org.apache.hadoop.mapreduce.InputSplit;
  19. import org.apache.hadoop.mapreduce.Job;
  20. import org.apache.hadoop.mapreduce.Mapper;
  21. import org.apache.hadoop.mapreduce.Reducer;
  22. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  23. import org.apache.hadoop.mapreduce.lib.input.FileSplit;
  24. import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
  25. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  26. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  27. import org.apache.hadoop.mapreduce.lib.output.MapFileOutputFormat;
  28. import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
  29. import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
  30. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  31. import org.apache.hadoop.util.GenericOptionsParser;
  32. import org.apache.hadoop.util.Progressable;
  33. import org.apache.hadoop.util.Tool;
  34. import org.apache.hadoop.util.ToolRunner;
  35. import org.apache.commons.logging.Log;
  36. import org.apache.commons.logging.LogFactory;
  37. //import org.apache.hadoop.mapred.DeprecatedLzoTextInputFormat;
  38.  
  39. import com.hadoop.compression.lzo.LzoCodec;
  40. import com.hadoop.mapreduce.LzoTextInputFormat;
  41.  
  42. public class compress extends Configured implements Tool {
  43. private static final Log log = LogFactory.getLog(compress.class);
  44.  
  45. private static class ProvinceMapper extends
  46. Mapper<Object, Text, Text, Text> {
  47. @Override
  48. protected void map(Object key, Text value, Context context)
  49. throws IOException, InterruptedException {
  50. //System.out.println(value);
  51.  
  52. // InputSplit inputSplit = context.getInputSplit();
  53. //String fileName = ((FileSplit) inputSplit).getPath().toString();
  54.  
  55. //System.out.println(fileName);
  56. context.write(value, value);
  57. }
  58. }
  59.  
  60. private static class ProvinceReducer extends
  61. Reducer<Text, Text, Text, Text> {
  62. @Override
  63. protected void reduce(Text key, Iterable<Text> values, Context context)
  64. throws IOException, InterruptedException {
  65. for (Text va : values) {
  66. // System.out.println("reduce " + key);
  67. context.write(key, key);
  68. }
  69. }
  70. }
  71.  
  72. public static void main(String[] args) throws Exception {
  73. ToolRunner.run(new Configuration(), new compress(), args);
  74. }
  75.  
  76. public static final String REDUCES_PER_HOST = "mapreduce.sort.reducesperhost";
  77.  
  78. @Override
  79. public int run(String[] args) throws Exception {
  80. log.info("我的服务查询开始.....................................");
  81.  
  82. long beg = System.currentTimeMillis();
  83. int result = 0;
  84. Configuration conf = new Configuration();
  85.  
  86. conf.set(
  87. "io.compression.codecs",
  88. "org.apache.hadoop.io.compress.DefaultCodec,org.apache.hadoop.io.compress.GzipCodec,com.hadoop.compression.lzo.LzopCodec");
  89. conf.set("io.compression.codec.lzo.class",
  90. "com.hadoop.compression.lzo.LzoCodec");
  91.  
  92. conf.setBoolean("mapreduce.map.output.compress", true);
  93. conf.setClass("mapreduce.map.output.compression.codec", SnappyCodec.class, CompressionCodec.class);
  94. // conf.setBoolean("mapreduce.output.fileoutputformat.compress", true); // 是否压缩输出
  95. conf.setClass("mapreduce.output.fileoutputformat.compress.codec", SnappyCodec.class, CompressionCodec.class);
  96.  
  97. String[] argArray = new GenericOptionsParser(conf, args)
  98. .getRemainingArgs();
  99.  
  100. if (argArray.length != 2) {
  101. System.err.println("Usage: compress <in> <out>");
  102. System.exit(1);
  103. }
  104.  
  105. // Hadoop总共有5个Job.java
  106. // /hadoop-2.0.0-cdh4.5.0/src/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java
  107. Job job = new Job(conf, "compress");
  108. job.setJarByClass(compress.class);
  109. job.setMapperClass(ProvinceMapper.class);
  110. job.setReducerClass(ProvinceReducer.class);
  111. job.setMapOutputKeyClass(Text.class);
  112. job.setMapOutputValueClass(Text.class);
  113. job.setOutputKeyClass(Text.class);
  114. job.setOutputValueClass(Text.class);
  115.  
  116. //job.setInputFormatClass(LzoTextInputFormat.class); // TextInputFormat
  117. // MyFileinput
  118.  
  119. // 使用lzo索引文件作为输入文件
  120. // job.setInputFormatClass(LzoTextInputFormat.class);
  121. job.setInputFormatClass(SequenceFileInputFormat.class);
  122.  
  123. // SequenceFileOutputFormat.set(job, LzoCodec.class);
  124.  
  125. // 测试块大小
  126. // FileInputFormat.setMinInputSplitSize(job, 150*1024*1024);
  127. // FileInputFormat.setMinInputSplitSize(job, 301349250);
  128. // FileInputFormat.setMaxInputSplitSize(job, 10000);
  129.  
  130. // 推测执行的开关 另外还有针对map和reduce的对应开关
  131. // job.setSpeculativeExecution(false);
  132. FileInputFormat.addInputPath(job, new Path(argArray[0]));
  133. FileOutputFormat.setOutputPath(job, new Path(argArray[1]));
  134.  
  135. String uri = argArray[1];
  136. Path path = new Path(uri);
  137. FileSystem fs = FileSystem.get(URI.create(uri), conf);
  138. if (fs.exists(path)) {
  139. fs.delete(path);
  140. }
  141.  
  142. result = job.waitForCompletion(true) ? 0 : 1;
  143.  
  144. // try {
  145. // result = job.waitForCompletion(true) ? 0 : 1;
  146. // } catch (ClassNotFoundException | InterruptedException e) {
  147. // e.printStackTrace();
  148. // }
  149. long end = (System.currentTimeMillis() -beg) ;
  150. System.out.println("耗时:" + end);
  151. return result;
  152. }
  153. }

测试结果

文件大小 544M(未使用任何压缩)
耗时:73805

使用 seqencefile(block使用lzo压缩, 中间结果使用snappy压缩)

44207s

MR中使用sequnceFIle输入文件的更多相关文章

  1. MR中的combiner和partitioner

    1.combiner combiner是MR编程模型中的一个组件: 有些任务中map可能会产生大量的本地输出,combiner的作用就是在map端对输出先做一次合并,以减少map和reduce节点之间 ...

  2. 总结的MR中连接操作

    1 reduce side join在map端加上标记, 在reduce容器保存,然后作笛卡尔积缺点: 有可能oom 2 map side join  2.1 利用内存和分布式缓存,也有oom风险 2 ...

  3. MR中简单实现自定义的输入输出格式

    import java.io.DataOutput; import java.io.IOException; import java.util.HashMap; import java.util.Ma ...

  4. MR操作

    MR操作————Map.Partitioner.Shuffle.Combiners.Reduce 1.Map步骤 1.1 读取输入文件,解析成k-v对,其中每个k-v对调用一次map函数 1.2 写自 ...

  5. 【转】Hive配置文件中配置项的含义详解(收藏版)

    http://www.aboutyun.com/thread-7548-1-1.html 这里面列出了hive几乎所有的配置项,下面问题只是说出了几种配置项目的作用.更多内容,可以查看内容问题导读:1 ...

  6. MapReduce中的Join

    一. MR中的join的两种方式: 1.reduce side join(面试题) reduce side join是一种最简单的join方式,其主要思想如下: 在map阶段,map函数同时读取两个文 ...

  7. Hive配置文件中配置项的含义详解(收藏版)

    这里面列出了hive几乎所有的配置项,下面问题只是说出了几种配置项目的作用.更多内容,可以查看内容 问题导读: 1.hive输出格式的配置项是哪个? 2.hive被各种语言调用如何配置? 3.hive ...

  8. Spark中Task,Partition,RDD、节点数、Executor数、core数目的关系和Application,Driver,Job,Task,Stage理解

    梳理一下Spark中关于并发度涉及的几个概念File,Block,Split,Task,Partition,RDD以及节点数.Executor数.core数目的关系. 输入可能以多个文件的形式存储在H ...

  9. Spark中Task,Partition,RDD、节点数、Executor数、core数目(线程池)、mem数

    Spark中Task,Partition,RDD.节点数.Executor数.core数目的关系和Application,Driver,Job,Task,Stage理解 from:https://bl ...

随机推荐

  1. android中的Touch研究

    android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件,有必要对它进行深入的了解. 一个最简单的屏幕触摸动作触发了一系列Touch事件:ACTION_DOWN-& ...

  2. 2018上半年DDoS攻击报告:流量峰值达1.7Tbps

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 2018年上半年DDoS攻防仍如火如荼发展,以IoT设备为反射点的SSDP反射放大尚未平息,Memcached DDoS又异军突起,以最高可 ...

  3. 用 Python 构建 web 应用

    用 Python 构建 web 应用 如果说仅仅要用 Python 构建 web 应用,可以将 socket 连接.HTTP 原始请求和响应格式等涉及网络基础的东西交给现成的库来实现,只需要专注于 w ...

  4. bnu 28890 &zoj 3689——Digging——————【要求物品次序的01背包】

    Digging Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 36 ...

  5. IIS7部署网站出现500.19错误(权限不足)的解决方案

    错误摘要 HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息 模块 IIS Web Core 通知 未知 处理 ...

  6. C#学习笔记8

    1.泛型的约束: (1)接口约束: (2)基类约束,基类约束必须放在第一(假如有多个约束): (3)struct/class约束: (4)多个参数类型的约束,每个类型参数都要用where关键字: (5 ...

  7. SelectedItems的用法讲解

    在做俄罗斯方块的时候写了下面一段代码: private void listView1_SelectedIndexChanged(object sender, EventArgs e)         ...

  8. vim右键粘贴 等杂

    putty连上linux,vim编辑个文件,我去,右键不能用用上下面的命令,就好了. set mouse-=a 今天发现mysql倒入utf-8的文件网站显示出来都是乱码,不过用utf-8的控制台看是 ...

  9. 在MVC中使用Bundle打包压缩js和css

    第一步:安装 安装“System.Web.Optimization”:在中“NuGet”中搜索 安装. 第二步:配置 配置“Views”目录下的“web.config”文件增加“System.Web. ...

  10. C#实现Stream与byte[]之间的转换实例教程

    一.二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(m ...