Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)

            Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)

                                          作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.编辑配置文件(pml.xml)(我们这里配置的是对“cn.org.yinzhengjie.compress.TestCompressCodec”该包进行打包操作)

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>groupId</groupId>
<artifactId>yinzhengjieCode</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency> <dependency>
<groupId>org.anarres.lzo</groupId>
<artifactId>lzo-hadoop</artifactId>
<version>1.0.0</version>
</dependency> </dependencies> <!--将指定类的所有依赖打入到一个包中-->
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- main函数所在的类 -->
<mainClass>cn.org.yinzhengjie.compress.TestCompressCodec</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>

  注意事项如下:

Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)

   我们通过pom.xml配置文件不难看出我们需要打的包是“cn.org.yinzhengjie.compress.TestCompressCodec”,上述的配置主要是对该包打入相应的依赖包关系,且上述配置仅对该包有效哟。当然我所述的只是“<build></build>”标签里面里面的参数,它是对手动添加依赖的关键!

二.开始打包

1>.需要打包(cn.org.yinzhengjie.compress.TestCompressCodec)的代码如下:

 /*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Hadoop%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%B7%AF/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.compress; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.*;
import org.apache.hadoop.util.ReflectionUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; public class TestCompressCodec {
/**
* 设置路径动态传参
* @param args
*/
public static void main(String[] args) {
if(args == null || args.length == 0){
System.out.println("需要输入路径");
System.exit(-1);
}
Class[] classes = {
DefaultCodec.class,
GzipCodec.class,
BZip2Codec.class,
Lz4Codec.class,
LzopCodec.class,
SnappyCodec.class
};
for(Class clazz : classes){
testCompress(clazz, args[0]);
testDecompress(clazz,args[0]);
}
}
/**
* Gzip压缩
* @throws Exception
*/
public static void testCompress(Class clazz, String path) {
try {
long start = System.currentTimeMillis();
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "file:///");
CompressionCodec codec = (CompressionCodec)ReflectionUtils.newInstance(clazz, conf);
FileInputStream fis = new FileInputStream(path);
//获取扩展名
String ext = codec.getDefaultExtension();
//创建压缩输出流
CompressionOutputStream cos = codec.createOutputStream(new FileOutputStream(path+ext));
IOUtils.copyBytes(fis,cos,1024);
fis.close();
cos.close();
System.out.print("压缩类型:"+ ext+"\t"+ "压缩时间:" + (System.currentTimeMillis() - start)+ "\t");
File f = new File(path+ext);
System.out.print("文件大小:"+ f.length() + "\t");
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Gzip解压
* @throws Exception
*/
public static void testDecompress(Class clazz,String path) {
try {
long start = System.currentTimeMillis();
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "file:///");
CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(clazz, conf);
//扩展名
String ext = codec.getDefaultExtension();
//压缩输入流
CompressionInputStream cis = codec.createInputStream(new FileInputStream(path+ext));
FileOutputStream fos = new FileOutputStream(path+ext+".txt");
IOUtils.copyBytes(cis,fos,1024);
cis.close();
fos.close();
System.out.println("解压时间:" + (System.currentTimeMillis() - start));
} catch (Exception e) {
e.printStackTrace();
}
}
}

2>.点击“package进行打包操作”

Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)

3>.打包后的产物

  在打包的过程需要一定的时间,我们需要耐心等待

Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)

  打包过程中如果没有遇到错误,就会出现以下的界面

Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)

   打包完成之后,会有两个文件生成,如下图:

Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)

三.Hadoop压缩格式综合测试

  由于我在Windows测试说当前Hadoop版本不支持snappy压缩格式,官网上也没有相应的下载版本(链接:https://pan.baidu.com/s/1Clhsvv-gzvVX7lQOQ27vng 密码:d367),不过有大神编译了Hadoop支持snappy格式的,如果有时间了我也得去研究研究,到时候会把笔记共享给大家。好了,下图就是我使用支持snappy压缩格式的版本进行测试的。

Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)

   我们将上面的核心信息抽取如下:

压缩类型:.deflate    压缩时间:        文件大小:    解压时间:
压缩类型:.gz 压缩时间: 文件大小: 解压时间:
压缩类型:.bz2 压缩时间: 文件大小: 解压时间:
压缩类型:.lz4 压缩时间: 文件大小: 解压时间:
压缩类型:.lzo 压缩时间: 文件大小: 解压时间:
压缩类型:.snappy 压缩时间: 文件大小: 解压时间:

  根据结果反推理论:(以上实验是对一个890M的文件进行处理,生成环境最好以实际生成环境为准,这个数据仅供参考!)

    压缩时间从小到大:
      lz4 < lzo < gz < snappy < deflate < bz2;

    压缩大小从小到大:

      defalte < gz < lz4 < bz2 < lzo < snappy;

    解压时间从小到大:

      zo < lz4 < deflate < gz < snappy < bz2;

上一篇:【BZOJ3328】PYXFIB 数论+矩阵乘法


下一篇:腾讯QQ积分CSRF导致积分任意挥霍(我的积分为什么少了)