hadoop之使用LZO压缩并支持分片

1.简介:

安装LZO:

lzo并不是Linux系统原生支持,所以需要下载安装软件包,这里至少需要安装3个软件包。

lzo, lzop, hadoop-gpl-packaging。

增加索引:
gpl-packaging主要作用是对压缩的lzo文件创建索引,否则的话,无论压缩的文件是否大于hdfs上的block大小,都只会是一个分片处理。

2.安装lzo并生成数据:
2.1生成未压缩的测试数据
先生成一个大于128M的测试数据,这样在使用lzo压缩后就能保证压缩文件大小大于数据块大小,方便后续测试分片效果。 我是通过 cat a >> b,cat b >> a 方式快速实现的数据生成。
2.2安装lzo相关工具

[root@hadoop-01 ~]# yum install -y svn ncurses-devel
[root@hadoop-01 ~]# yum install -y gcc gcc-c++ make cmake
[root@hadoop-01 ~]# yum install -y openssl openssl-devel svn ncurses-devel zlib-devel libtool 
[root@hadoop-01 ~]# yum install -y lzo lzo-devel lzop autoconf automake cmake

2.3使用lzop工具压缩测试数据
lzo压缩:lzop -v file
lzo解压:lzop -dv file

[hadoop@hadoop-01 date]$lzop -v page_views.dat
compressing page_views.dat into page_views.dat.lzo

[hadoop@hadoop-01 data]$ du -sh lzodate.txt.lzo 
276M    page_views.dat.lzo

#将lzodate.txt文件压缩成lzo格式

3.编译hadoop-lzo

3.1下载并配置 hadoop—lzo

[hadoop@hadoop-01 software]$ wget https://github.com/twitter/hadoop-lzo/archive/master.zip

#解压:
[hadoop@hadoop-01 software]$ unzip master.zip

#进入解压目录
[hadoop@hadoop-01 app]$ cd hadoop-lzo-master/
[hadoop@hadoop-01 hadoop-lzo-master]$ 

#因为hadoop使用的是2.6.0;所以版本修改为2.6.0:
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <hadoop.current.version>2.6.0</hadoop.current.version>
    <hadoop.old.version>1.0.4</hadoop.old.version>
</properties>

[hadoop@hadoop-01 hadoop-lzo-master]$ export CFLAGS=-m64
[hadoop@hadoop-01 hadoop-lzo-master]$ export CXXFLAGS=-m64

#修改为自己hadoop的实际路径
[hadoop@hadoop-01 hadoop-lzo-master]$ export C_INCLUDE_PATH=/home/hadoop/app/hadoop-2.6.0-cdh5.7.0/lzo/include
[hadoop@hadoop-01 hadoop-lzo-master]$ export LIBRARY_PATH=/home/hadoop/app/hadoop-2.6.0-cdh5.7.0/lzo/lib

3.2mvn编译源码

[hadoop@hadoop-01 hadoop-lzo-master]$ mvn clean package -Dmaven.test.skip=true

[INFO] Building jar: /home/hadoop/software/hadoop-lzo-master/target/hadoop-lzo-0.4.21-SNAPSHOT-javadoc.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8:43.929s
[INFO] Finished at: Sun Apr 14 16:42:02 CST 2019
[INFO] Final Memory: 25M/61M
[INFO] ---------------------------------------------------------------------

[hadoop@hadoop-01 hadoop-lzo-master]$ 
#进入target文件夹
[hadoop@hadoop-01 hadoop-lzo-master]$ cd target/native/Linux-amd64-64/

[hadoop@hadoop-01 Linux-amd64-64]$ mkdir ~/app/hadoop-lzo-files
[hadoop@hadoop-01 Linux-amd64-64]$ tar -cBf - -C lib . | tar -xBvf - -C ~/app/hadoop-lzo-files

[hadoop@hadoop-01 hadoop-lzo-files]$ cp ~/app/hadoop-lzo-files/libgplcompression* $HADOOP_HOME/lib/native/

#将文件都复制到 app/hadoop-lzo-files中

上传hadoop-lzo.jar 这一步很重要

#将hadoop-lzo-0.4.21-SNAPSHOT.jar 复制到每台hadoop的common目录 

[hadoop@hadoop-01 hadoop-lzo-master]$ cp hadoop-lzo-0.4.21-SNAPSHOT.jar ~/app/hadoop-2.6.0-cdh5.7.0/share/hadoop/common/ 

[hadoop@hadoop-01 hadoop-lzo-master]$ ll ~/app/hadoop-2.6.0-cdh5.7.0/share/hadoop/common/hadoop-lzo* -rw-rw-r--. 1 hadoop hadoop 180667 Apr 14 08:52 /home/hadoop/app/hadoop-2.6.0-cdh5.7.0/share/hadoop/common/hadoop-lzo-0.4.21-SNAPSHOT.jar

3.3配置core.site.xml

# 停止hadoop
[hadoop@hadoop-01 hadoop-lzo-master]$ stop-all.sh 

#编辑core-site.xml添加或修改如下内容 [hadoop@hadoop-01 ~]$ vim ~/app/hadoop-2.6.0-cdh5.7.0/etc/hadoop/core-site.xml 

-----------------------开始-------------------------------
<property>
	<name>io.compression.codecs</name>
    <value>org.apache.hadoop.io.compress.GzipCodec,
    	org.apache.hadoop.io.compress.DefaultCodec,
		org.apache.hadoop.io.compress.BZip2Codec,
		org.apache.hadoop.io.compress.SnappyCodec,
		com.hadoop.compression.lzo.LzoCodec,
		com.hadoop.compression.lzo.LzopCodec
    </value>
    </property>
	<property>
		<name>io.compression.codec.lzo.class</name>
		<value>com.hadoop.compression.lzo.LzoCodec</value>
	</property>
	-----------------------结束------------------------------- 
	
	
#解析:主要是配置com.hadoop.compression.lzo.LzoCodec
			 com.hadoopcompression.lzo.LzopCodec压缩类

io.compression.codec.lzo.class必须指定为LzoCodec非LzopCodec,不然压缩后的文件不会支持分片的

3.4配置mapred-site.xml

#编辑mapred-site.xml添加或修改如下内容 

[hadoop@hadoop-01 ~]$ vim ~/app/hadoop-2.6.0-cdh5.7.0/etc/hadoop/mapred-site.xml 

-----------------------开始-------------------------------
中间阶段的压缩
<property>
<name>mapreduce.map.output.compress</name>
<value>true</value>
</property>
<property>
<name>mapreduce.map.output.compress.codec</name>
<value>org.apache.hadoop.io.compress.SnappyCodec</value>
</property>

最终阶段的压缩
<property>
<name>mapreduce.output.fileoutputformat.compress</name>
<value>true</value>
</property>
<property>
<name>mapreduce.output.fileoutputformat.compress.codec</name>
<value>org.apache.hadoop.io.compress.BZip2Codec</value>
</property>
-----------------------结束-------------------------------


#启动hadoop
[hadoop@hadoop-01 ~]$ start-all.sh

4.LZO文件测试
4.1LZO文件不支持分片

#创建LZO压缩文件测试表,若hadoop的common目录没有hadoop-lzo的jar,就会报类DeprecatedLzoTextInputFormat找不到异常

create table g6_access_copy_lzo (
cdn string,
region string,
level string,
time string,
ip string,
domain string,
url string,
traffic bigint
)row format delimited fields terminated by '\t'
STORED AS INPUTFORMAT "com.hadoop.mapred.DeprecatedLzoTextInputFormat"
OUTPUTFORMAT "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat";

#加载lzo格式的测试数据,注意:
LOAD DATA LOCAL INPATH '/home/hadoop/data/page_views.dat.lzo' OVERWRITE INTO TABLE g6_access_copy_lzo;

#查看数据:
[hadoop@hadoop-01 hadoop-2.6.0-cdh5.7.0]$ hadoop fs -du -s -h /user/hive/warehouse/myhive.db/g6_access_copy_lzo
275.9 M  275.9 M  /user/hive/warehouse/myhive.db/g6_access_copy_lzo

#查询测试
select count(1) from g6_access_copy_lzo;

控制台日志截取:
Stage-Stage-1: Map: 1  Reduce: 1   Cumulative CPU: 11.37 sec   HDFS Read: 289332141 HDFS Write: 8 SUCCESS

由日志我们可以看出只有一个map任务,而我们的数据文件是远大于128M的,说明当前lzo文件默认不支持数据切片

4.2LZO文件支持分片

注意若不是直接load的lzo文件,需要开启压缩,且压缩格式为LzopCodec,load数据并不能改变文件格式和压缩格式。

#开启压缩,生成的压缩文件格式必须设置为LzopCodec,lzoCode的压缩文件格式后缀为.lzo_deflate是无法创建索引的。

SET hive.exec.compress.output=true;

SET mapreduce.output.fileoutputformat.compress.codec=com.hadoop.compression.lzo.LzopCodec;

#创建LZO压缩文件测试表
create table g6_access_copy_lzo_split
STORED AS INPUTFORMAT "com.hadoop.mapred.DeprecatedLzoTextInputFormat"
OUTPUTFORMAT "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat"
as select *  from g6_access_copy_lzo;

#构建LZO文件索引,使用我们之前打的jar包中的工具类
hadoop jar ~/app/hadoop-2.6.0-cdh5.7.0/share/hadoop/common/hadoop-lzo-0.4.21-SNAPSHOT.jar \ com.hadoop.compression.lzo.LzoIndexer /user/hive/warehouse/myhive.db/g6_access_copy_lzo_split

#查询hdfs数据目录,可知在lzo文件同级目录有个.index索引文件
[hadoop@hadoop-01 hadoop-2.6.0-cdh5.7.0]$ hadoop fs -ls /user/hive/warehouse/myhive.db/g6_access_copy_lzo_split
Found 2 items

-rwxr-xr-x   1 hadoop supergroup  190593490 2019-04-23 17:32 /user/hive/warehouse/myhive.db/g6_access_copy_lzo_split/000000_0.lzo

-rw-r--r--   1 hadoop supergroup      22256 2019-04-23 17:32/user/hive/warehouse/myhive.db/g6_access_copy_lzo_split/000000_0.lzo.index


#执行统计分析
select count(1) from g6_access_copy_lzo_split;

控制台日志:
Stage-Stage-1: Map: 2  Reduce: 1   Cumulative CPU: 9.37 sec   HDFS Read: 190593490 HDFS Write: 8 SUCCESS

由日志可知 此时有两个map任务。即构建索引后支持数据分片。

总结:
大数据中常见的压缩格式只有bzip2是支持数据分片的,lzo在文件构建索引后才会支持数据分片

上一篇:split_lzo_lib.sh


下一篇:pyspark向lzo格式hive表插入数据