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. CVPR 2019 | 惊艳的SiamMask:开源快速同时进行目标跟踪与分割算法
  2. 西南交大量子计算机,交大量子光电实验室
  3. DataSourceUtils(使用C3P0连接池的工具类)
  4. python时间格式_python 格式化日期
  5. C 语言取整的几种方法6,C语言有以下几种取整方法:
  6. 别再说Python简单!
  7. 停止不了_男性脂溢性脱发之坚持及停止止脱方法后的对比(顺便找发友组队再战)...
  8. LoadRunner11破解方法
  9. 不登陆QQ也能使用QQ截图工具
  10. Ubuntu操作系统的学习,从新手到老手的过渡
  11. 基因结构显示服务器,GSDS: a gene structure display serverGSDS: 基因结构显示系统
  12. 青海大学市计算机科学与技术,曹腾飞 - 青海大学 - 计算机技术与应用系
  13. prisma 连接不上Postgres
  14. ‘git clone‘ failed with status 128
  15. 关于_CameraDepthTexture的疑惑
  16. 飞鱼星方案助山东小城印象实现微信营销
  17. python学习之爬取ts流电影
  18. 2345浏览器如何显示浏览器推送内容
  19. ToggleButton图片按钮的两种制作方法
  20. Radeon Gpu源码分析

热门文章

  1. build settings参数详解
  2. 滴滴开源首次发布年度报告
  3. 【嵌入式】——对#define GPBCON (*(volatile unsigned long*)0x56000010)的理解
  4. silverlight5
  5. Windows Mobile系统弹出输入法时,自动调整窗口显示
  6. 1001 Calculate a + b
  7. 服务器:高端增势明显 行业大单涌现
  8. centos配置ntp时间同步_Linux CentOS配置ntpd时间同步
  9. struts2文件上传中,如何限制上传的文件类型
  10. 架构宣言: MDA 实战