`
lihaosu
  • 浏览: 1466 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

一个适合MapReduce处理的gz压缩方式

阅读更多
最近在筹备hadoop,测试集群只有普通的6个虚拟机,每个1G内存,100G硬盘。所以在yarn进行资源调度的时候比较纠结,硬盘空间也有限。在执行作业的时候就希望能够尽量对输入数据进行压缩。


hadoop可以直接处理gz格式的压缩文件,但不会产生split,而是不论多大都直接交给一个Mapper去做,因为gz在算法上不支持split。虽然bzip2支持split,但压缩速度又比较慢,gz可以说是最常用的压缩方式了。


一开始想当然的尝试压缩分卷,结果当然是失败,因为不管分多少个卷,gz还是要以一个整体来进行解压。


因为我只是处理文本数据,而且都是基于文本行,每一行之间不像xml那样会具有什么嵌套关系,所以动手写了一个压缩程序,在对大文件进行压缩的时候,如果产生的压缩文件大于一个设定值,那就再新建一个文件继续压缩。

package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;

public class CompressUtils
{
	/**
	 * 将文件压缩成GZIP分片
	 * @param inputFile 输入文件
	 * @param outputDir 输出目录
	 * @param outputFileName 输出文件名
	 * @param splitSize 分片大小
	 */
	public static void compressToSplitsUseGZIP(File inputFile, File outputDir, String outputFileName, int splitSize)
		throws Exception
	{
		String separator = System.getProperty("line.separator");
		int split = 0;
		long limit = splitSize * 1024 * 1024L;
		File outputSplit = new File(outputDir, outputFileName + split + ".gz");
		outputSplit.createNewFile();
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "UTF-8"));
		PrintWriter out = new PrintWriter(new GZIPOutputStream(new FileOutputStream(outputSplit)), false);
		String line = null;
		long fileLength = outputSplit.length();
		long maxInc = 0L;
		while (true)
		{
			line = br.readLine();
			if (line == null)
			{
				break;
			}
			
			if (fileLength + maxInc > limit)
			{
				if (out != null)
				{
					out.close();
					out = null;
					outputSplit = new File(outputDir, outputFileName + (++split) + ".gz");
					outputSplit.createNewFile();
					fileLength = outputSplit.length();
					out = new PrintWriter(new GZIPOutputStream(
							new FileOutputStream(outputSplit)), false);
				}
			}

			for (byte b : line.getBytes())
			{
				out.write(b);
			}
			for (byte b : separator.getBytes())
			{
				out.write(b);
			}
			out.flush();
			
			long currentLength = outputSplit.length();
			long inc = currentLength - fileLength;
			if (inc >= maxInc)
			{
				maxInc = inc;
			}
			fileLength = currentLength;
		}
		br.close();
		try
		{
			out.close();
		}
		catch (Exception e)
		{
		}
	}
	
	public static void main(String[] args)
		throws Exception
	{
		File inputFile = new File(args[0]);
		File outputDir = new File(args[1]);
		String outputFileName = args[2];
		int splitSize = Integer.parseInt(args[3]);
		compressToSplitsUseGZIP(inputFile, outputDir, outputFileName, splitSize);
	}
}


命令行参数:D:\temp\test.txt D:\temp test 64

这样产生的压缩文件每一个都会小于64MB,最多相差不到100k。考虑的因素比较少,这里只是把大致的算法写了一下,倒是满足需求了。
分享到:
评论

相关推荐

    22、MapReduce使用Gzip压缩、Snappy压缩和Lzo压缩算法写文件和读取相应的文件

    22、MapReduce使用Gzip压缩、Snappy压缩和Lzo压缩算法写文件和读取相应的文件 网址:https://blog.csdn.net/chenwewi520feng/article/details/130456088 本文的前提是hadoop环境正常。 本文最好和MapReduce操作常见...

    用mapreduce进行文本处理

    用mapreduce进行文本处理,发表在SIGIR2009

    mapreduce.tar.gz

    仅供参考学习,严禁作于开发,只是第一部分

    mapreduce mapreduce mapreduce

    mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce ...

    MapReduce:超大机群上的简单数据处理

    计算利用一个输入key/value对集,来产生一个输出key/value对集.MapReduce库的用户用两个函数表达这个计算:map和reduce. 用户自定义的map函数,接受一个输入对,然后产生一个中间key/value对集.MapReduce库把所有具有...

    一个MapReduce简单程序示例

    一个MapReduce简单程序示例 MapReduce hadoop

    MapReduce讲义

    MapReduce研究生课程讲义,讲述mapreduce概念定义以及应用等,可作为授课资料

    搭建Hadoop集群,写mapreduce程序处理数据

    搭建了一个完全分布式Hadoop集群,并通过Java写了mapreduce程序处理数据,需要下载的可以找我要具体数据。

    MapReduce海量数据处理

    为了更加有效和简洁的处理此类问题,Google 提出了 MapReduce 编程模型,它可以隐藏并行化、容错、数据分布、负载均衡等细节,把这些公共的细节抽象到一个库中,由一个运行时系统来负责。而将对数据的操作抽象为 map...

    云计算中大数据的MapReduce处理方法简析.pdf

    云计算中大数据的MapReduce处理方法简析.pdf

    实验项目 MapReduce 编程

    MapReduceExample 下建立新包 com.xijing.mapreduce,模仿内置的 WordCount 示例,自己编写一个 WordCount 程序,最后打包成 JAR 形式并在 Hadoop 集群上运行该 MR-App,查看运行结果。 4 分别在自编 MapReduce 程序...

    第一个Mapreduce程序.pdf

    本文介绍了用Java编写并运行第一个mapreduce作业的步骤及遇到的问题和解决方案。

    【MapReduce篇07】MapReduce之数据清洗ETL1

    【MapReduce篇07】MapReduce之数据清洗ETL1

    Mapreduce实验报告.doc

    面对一个规模庞大的问题,要 处理是以TB计的数据,Mapreduce采用"输入"------"分解"------"解决"----- -"聚合"------"输出结果"的基本过程。 2. 基本原理 Map和Reduce是两个核心操作,用户定义的map函数接收被切割过...

    大数据处理引擎MapReduce.ppt

    1、传统的海量数据分析方案 2、Apache Hadoop项目 3、HDFS设计 4、MapReduce 5、Pig & Hive 6、Spark ……

    大数据平台构建:一个简单的MapReduce程序.pptx

    一个简单的MapReduce程序 WordCount单词统计 1 代码实现 2 目 录 一个非常经典的MapReduce案例——WordCount单词统计。 什么是MapReduce 一、WordCount单词统计 二、代码实现 对于map函数的方法。 Mapper的实现 ...

    MapReduce简介

    大规模数据处理时,MapReduce在三个层面上的基本构思 如何对付大数据处理:分而治之 对相互间不具有计算依赖关系的大数据,实现并行最自然的办法就是采取分而治之的策略 上升到抽象模型:Mapper与Reducer MPI等...

    大数据实验5实验报告:MapReduce 初级编程实践

    林子雨大数据原理与技术第三版实验5实验报告 大数据技术与原理实验报告 ...并剔除其中重复的内容,得到一个新的输出文件 C。下面是输入文件和输出文件的一个样例 供参考。 输入文件 A 的样例如下:

    HBase与MapReduce处理操作(基于JavaAPI)

    该案例中主要使用MapReduce作为处理组件进行数据处理,实现的案例有如通过javaapi实现hbase数据写入hdfs、hbase表数据复制到另一个表中等操作 对应(《HBase分布式存储系统应用》胡鑫喆 张志刚著)教材中案例

    大数据、大数据处理模型及MapReduce

    文档简要介绍了大数据、大数据处理模型及MapReduce的相关知识

Global site tag (gtag.js) - Google Analytics