动机:希望通过Java API将日志行压缩到lzo文件,然后上传到HDFS。因为kevinweil-hadoop-lzo提供了LzopOutpuStream,所以这一切很容易实现,需要注意的是该jar包依赖native code和lzo本身。本人试过Karmasphere的lzo-java项目,该项目用java重写了lzo压缩算法,但是该项目好久不更新,提问没人回,性能也只有原生lzo压缩性能一半,最难以忍受的是会有压缩后文件损坏的情况,果断放弃。
工具:lzo
地址:http://www.oberhumer.com/opensource/lzo/download/lzo-2.06.tar.gz
或者到 http://rpmfind.net 查找rpm包,下面两个是32bit redhat4和5的rpm包
ftp://rpmfind.net/linux/dag/redhat/el4/en/i386/dag/RPMS/lzo-2.06-1.el4.rf.i386.rpm
ftp://rpmfind.net/linux/dag/redhat/el5/en/i386/dag/RPMS/lzo-2.06-1.el5.rf.i386.rpm
工具:kevinweil-hadoop-lzo
地址:https://github.com/kevinweil/hadoop-lzo
-----------------------
1. 安装lzo和kevinweil-hadoop-lzo
详见:http://heipark.iteye.com/blog/1172759
2. 设置环境变量
3. 写java代码
public class TestLzo { @Test public void test(String input, String output) throws IOException { BufferedReader textBr = new BufferedReader(new InputStreamReader(new FileInputStream(input))); int lzoBufferSize = 256 * 1024; LzoCompressor.CompressionStrategy strategy = LzoCompressor.CompressionStrategy.LZO1X_1; LzoCompressor lzoCompressor = new LzoCompressor(strategy, lzoBufferSize); LzopOutputStream lzopOut = new LzopOutputStream(new FileOutputStream(output), lzoCompressor, lzoBufferSize, strategy); String textLine; while ((textLine = textBr.readLine()) != null) { textLine += "\n"; byte[] bytes = textLine.getBytes(); lzopOut.write(bytes, 0, bytes.length); } textBr.close(); lzopOut.close(); } public static void main(String[] args) throws IOException { TestLzo lzo = new TestLzo(); lzo.test(args[0], args[1]); } }
4. 运行
/usr/java/latest/bin/java -cp .:hadoop-lzo-0.4.15.jar:hadoop-core-0.20.2-cdh3u4.jar:commons-logging-1.1.1.jar com/hadoop/compression/lzo/TestLzo abc.txt abc.txt.lzo
--heipark