MapReduce使用简单的gzip格式进行文件的压缩

package example;import java.io.IOException;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.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.GzipCodec;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;public class countData {public static final String INPUT_PATH="hdfs://hadoop0:9000/test/";public static final String OUTPUT_PATH="hdfs://hadoop0:9000/testout/";public static void main(String[] args) throws Exception {Configuration conf=new Configuration();
     //设置输出压缩conf.setBoolean("mapred.compress.map.out", true);//设置map输出压缩conf.setBoolean("mapred.output.compress", true);//设置输出压缩conf.setClass("mapred.output.compression.codec", GzipCodec.class, CompressionCodec.class);//设置压缩算法Job job = new Job(conf,countData.class.getSimpleName());//input pathFileInputFormat.setInputPaths(job, new Path(INPUT_PATH));//input formatjob.setInputFormatClass(TextInputFormat.class);//Mapper classjob.setMapperClass(MyMapper.class);//map output formatjob.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(LongWritable.class);//排序、分组、规约、分区//set reducejob.setReducerClass(MyReducer.class);//reduce output formatjob.setOutputKeyClass(Text.class);job.setOutputValueClass(LongWritable.class);//output pathFileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH));//output formatjob.setOutputFormatClass(TextOutputFormat.class);job.waitForCompletion(true);}static class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable>{@Overrideprotected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {String[] split = value.toString().split(",");int count=0;for(int i=0;i<split.length;i++){if(i==split.length-1){count=1;}context.write(new Text(split[i]),new LongWritable(count));}}}static class MyReducer extends Reducer<Text, LongWritable, Text, LongWritable>{@Overrideprotected void reduce(Text k2, Iterable<LongWritable> v2s,Context context)throws IOException, InterruptedException {int count=0;int sum=0;for (LongWritable value : v2s) {count+=Integer.parseInt(value.toString());sum+=1;}if(count>0){count=0;for (int i=0;i<sum;i++) {count+=1;}context.write(k2, new LongWritable(count));}}}
}

gzip压缩是一个比较简单的压缩方式,但它有很多缺点(不支持分割,导致mapreduce效率低;压缩和解压速度慢);所以下面有一个更好的压缩格式:lzo

mapreduce简单的gzip压缩相关推荐

  1. nginx的gzip压缩功能

    我们在开发网站的时候,应该要考虑到pv,因为pv比较大可能会造成服务器带宽不够用,进而导致用户体验变差. 这个时候我们就可以考虑用nginx的gzip功能. 在nginx中开启gzip压缩功能很简单, ...

  2. 从 Gzip 压缩 SVG 说起 — 论如何减小资源文件的大小

    原文地址:Of SVG, Minification and Gzip 原文作者:Anton Khlynovskiy 译文出自:掘金翻译计划 本文永久链接:github.com/xitu/gold-m- ...

  3. godaddy php5.ini,Godaddy主机如何开启GZIP压缩 | Godaddy美国主机中文指南

    为什么要开启GZIP压缩呢?原因很简单:使用GZIP压缩技术能让用户感受更快的速度.这一般是指WWW服务器中安装的一个功能,当有人来访问这个服务器中的网站时,服务器中的这个功能就将网页内容压缩后传输到 ...

  4. ASP.NET页面进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)

    在介绍之前,先简单说一说ASP.NET服务端GZIP压缩模块的作用及工作原理,很多人编写网页的时候页面因为使用了大量的JS特效又或者放置很多大型动态广告导致了页面或脚本体积庞大,通常都会使用一些压缩工 ...

  5. thinkphp框架开启页面gzip压缩

    Thinkphp下开启gzip压缩很简单,不管你是哪个版本,只要在你的入口文件index.PHP中加入以下两行,如果你的服务器支持,那么就OK了. define ( "GZIP_ENABLE ...

  6. 启用Gzip压缩(IIS)提高客户端网站访问速度

    IIS上启用Gzip压缩(HTTP压缩) 详解 一.摘要 本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示页面的速度. 二.前言. 本文的知识点是从互联 ...

  7. 在IIS上启用Gzip压缩 (HTTP压缩)方法

    本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小, 提高用户显示页面的速度. 一.摘要 本文总结了如何为使用IIS托管的网站启用Gzip压缩, 从而减少网页网络传输大小 ...

  8. Apache开启Gzip压缩,LAMP网页压缩

    源自http://hi.baidu.com/mrlbz/blog/item/69447759beedafc19d82046b.html 我Wordpress的主机为LAMP架构,即Linux+ Apa ...

  9. asp.net实现GZip压缩和GZip解压

    最近在开发一个网站doc.115sou.com,使用到了GZip压缩技术,经过多次搜索找到asp.net中用GZip对数据压缩和解压缩非常方便,当我第一次拿到这个类的时候却感觉很迷茫,无从下手.主要是 ...

最新文章

  1. 设计模式学习(六):重构与模式,推荐书籍(完)
  2. python 钉钉消息推送_python3实现钉钉消息推送的方法示例
  3. 数学--数论--积性函数(初步)
  4. 2014年英语一作文partB漫画作文
  5. 从放牛娃到北大博士,这篇论文后记刷屏
  6. AC_Dream 1216 G - Beautiful People
  7. wp7 --缓动动画
  8. linux编译多个函数,Swift on Linux —— 多文件协同编译
  9. 边界安全技术简要说明
  10. cuda无法在电脑上运行_办公技巧 | 专治PPT在别的电脑上无法播放的神器!
  11. Android Studio中导入第三方库
  12. 分析图片相似度的软件,图片相似度比对算法
  13. IE8 “Automation 服务器不能创建对象”问题解决方法
  14. 0人报名!清华转专业20+学科无人问津引热议,网友:一切为了吃饭
  15. 用c语言如何以图形方式显示家谱,数据结构_家谱管理系统
  16. python pip 安装包失败问题,下载缓慢问题
  17. oeasy教您玩转 linux 010213 中文 fcitx
  18. 用java获取本机IP地址
  19. windows 安装Abin
  20. Any Takers For AIG's Asian Arm?

热门文章

  1. eltree ref什么时候有_DBA:为什么你老写慢SQL
  2. centos7搭建Linux集群,CentOS 7下Kafka集群安装
  3. Swift--字符串和字符(二)
  4. ios 防止按钮快速点击造成多次响应的避免方法。
  5. NSNotification、delegate和KVO的区别
  6. JavaScript30秒, 从入门到放弃之Array(三)
  7. 《文明之光 第二册》一一10.1 罗卡尔角的夕阳—— 葡、西的殖民时代(1)
  8. 发现以前的一些记录 shutdown AP CPU
  9. Windbg学习 (0x0007) 命令-会话控制
  10. 修改Mysql默认 编码