赞
踩
Hadoop经常用于处理大量的数据,如果期间的输出数据、中间数据能压缩存储,对系统的I/O性能会有提升。综合考虑压缩、解压速度、是否支持split,目前lzo是最好的选择。LZO(LZO是Lempel-Ziv-Oberhumer的缩写)是一种高压缩比和解压速度极快的编码,它的特点是解压缩速度非常快,无损压缩,压缩后的数据能准确还原,lzo是基于block分块的,允许数据被分解成chunk,能够被并行的解压。LZO库实现了许多有下述特点的算法:
(1)、解压简单,速度非常快。
(2)、解压不需要内存。
(3)、压缩相当地快。
(4)、压缩需要64 kB的内存。
(5)、允许在压缩部分以损失压缩速度为代价提高压缩率,解压速度不会降低。
(6)、包括生成预先压缩数据的压缩级别,这样可以得到相当有竞争力的压缩比。
(7)、另外还有一个只需要8 kB内存的压缩级别。
(8)、算法是线程安全的。
(9)、算法是无损的。
一、安装lzop native library
1.在编译lzo包的时候,需要一些环境,可以用下面的命令安装好lzo编译环境
[root@hadoop000 ~]# yum -y install lzo-devel zlib-devel gcc autoconf automake libtool
2.下载、解压并编译lzo包
- [root@hadoop000 local]#wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.06.tar.gz
- [root@hadoop000 local]#tar -zxvf lzo-2.06.tar.gz
- [root@hadoop000 local]#cd lzo-2.06
- [root@hadoop000 local]#export CFLAGS=-m64
- [root@hadoop000 local]#./configure -enable-shared -prefix=/usr/local/hadoop-2.9.1/lzo/
- [root@hadoop000 local]#make && sudo make install
二、修改Hadoop配置
进入配置文件目录
[hadoop@hadoop000 hadoop]$ cd etc/hadoop
[hadoop@hadoop000 hadoop]$ vim hadoop-env.sh
export LD_LIBRARY_PATH=/usr/local/hadoop/lzo/lib
[hadoop@hadoop000 hadoop]$ vim core-site.xml
- <property>
- <name>io.compression.codecs</name>
- <value>org.apache.hadoop.io.compress.GzipCodec,
- org.apache.hadoop.io.compress.DefaultCodec,
- com.hadoop.compression.lzo.LzoCodec,
- com.hadoop.compression.lzo.LzopCodec,
- org.apache.hadoop.io.compress.BZip2Codec
- </value>
- </property>
-
- <property>
- <name>io.compression.codec.lzo.class</name>
- <value>com.hadoop.compression.lzo.LzoCodec</value>
- </property>
[hadoop@hadoop000 hadoop]$ vim mapred-site.xml
- <property>
- <name>mapred.compress.map.output</name>
- <value>true</value>
- </property>
-
- <property>
- <name>mapred.map.output.compression.codec</name>
- <value>com.hadoop.compression.lzo.LzoCodec</value>
- </property>
-
- <property>
- <name>mapred.child.env</name>
- <value>LD_LIBRARY_PATH=/usr/local/hadoop/lzo/lib</value>
- </property>
三、安装hadoop-lzo
下载hadoop-lzo
[root@hadoop000~]#wget https://github.com/twitter/hadoop-lzo/archive/master.zip
解压文件
[root@hadoop000~]#unzip master.zip
进入目录
[root@hadoop000~]# cd hadoop-lzo-master/
查看Hadoop的版本
- [hadoop@hadoop000 bin]$ ./hadoop version
- Hadoop 2.6.0-cdh5.15.1
- Subversion http://github.com/cloudera/hadoop -r 2d822203265a2827554b84cbb46c69b86ccca149
- Compiled by jenkins on 2018-08-09T16:23Z
- Compiled with protoc 2.5.0
- From source with checksum 96bc735f7d923171f18968309fa3c477
- This command was run using /home/hadoop/app/hadoop-2.6.0-cdh5.15.1/share/hadoop/common/hadoop-common-2.6.0-cdh5.15.1.jar
编辑pom.xml中的hadoop版本号跟集群中使用的版本号一致
- <repository>
- <id>cloudera</id>
- <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
- </repository>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <hadoop.current.version>2.6.0-cdh5.15.1</hadoop.current.version>
- <hadoop.old.version>1.0.4</hadoop.old.version>
- </properties>
开始编译
mvn clean package -Dmaven.test.skip=true
- Generating /home/hadoop/software/hadoop-lzo-master/target/apidocs/allclasses-noframe.html...
- Generating /home/hadoop/software/hadoop-lzo-master/target/apidocs/index.html...
- Generating /home/hadoop/software/hadoop-lzo-master/target/apidocs/overview-summary.html...
- Generating /home/hadoop/software/hadoop-lzo-master/target/apidocs/help-doc.html...
- [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: 01:27 h
- [INFO] Finished at: 2019-10-06T23:37:29+08:00
- [INFO] ------------------------------------------------------------------------
编译成功后执行如下命令
cd target/native/Linux-amd64-64
tar -cBf - -C lib . | tar -xBvf - -C ~
cp ~/libgplcompression* $HADOOP_HOME/lib/native/
到hadoop-lzo-master下把jar包导入Hadoop中
cp target/hadoop-lzo-0.4.21-SNAPSHOT.jar $HADOOP_HOME/share/hadoop/common/
cp target/hadoop-lzo-0.4.21-SNAPSHOT.jar $HADOOP_HOME/share/hadoop/mapreduce/lib
- create table lzo(id int,name string)
- STORED AS INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
- OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';
2、job日志方法
使用lzo压缩
- [hadoop@hadoop000 data]$ ll -h trackinfo_20191001.data
- -rw-r--r-- 1 hadoop hadoop 1.5G Oct 7 15:23 trackinfo_20191001.data
- [hadoop@hadoop000 data]$ lzop trackinfo_20191001.data
- [hadoop@hadoop000 data]$ ll -h trackinfo_20191001*
- -rw-r--r-- 1 hadoop hadoop 1.5G Oct 7 15:23 trackinfo_20191001.data
- -rw-r--r-- 1 hadoop hadoop 279M Oct 7 15:23 trackinfo_20191001.data.lzo
上传到HDFS
- [hadoop@hadoop000 data]$ hdfs dfs -put trackinfo_20191001.data.lzo /data/lzo
- [hadoop@hadoop000 data]$ hdfs dfs -ls /data/lzo
- Found 1 items
- -rw-r--r-- 1 hadoop supergroup 292080918 2019-10-07 15:25 /data/lzo/trackinfo_20191001.data.lzo
- [hadoop@hadoop000 data]$ hdfs dfs -ls -h /data/lzo
- Found 1 items
- -rw-r--r-- 1 hadoop supergroup 278.6 M 2019-10-07 15:25 /data/lzo/trackinfo_20191001.data.lzo
在web界面查看,该数据只有一个128M的分片
执行一个wordcount
hadoop jar hadoop-mapreduce-examples-2.6.0-cdh5.15.1.jar wordcount /data/lzo/trackinfo_20191001.data.lzo /data/lzo/output
日志提示使用了lzo库,但是分片只有一个
给文件建立索引
hadoop jar /home/hadoop/software/hadoop-lzo-master/target/hadoop-lzo-0.4.21-SNAPSHOT.jar com.hadoop.compression.lzo.DistributedLzoIndexer /data/lzo/trackinfo_20191001.data.lzo
- [hadoop@hadoop000 target]$ hdfs dfs -ls /data/lzo
- Found 2 items
- -rw-r--r-- 1 hadoop supergroup 292080918 2019-10-07 15:25 /data/lzo/trackinfo_20191001.data.lzo
- -rw-r--r-- 1 hadoop supergroup 47672 2019-10-07 15:48 /data/lzo/trackinfo_20191001.data.lzo.index
web页面的显示为
重新执行wordcount
注意此次的命令和上面的命令不一样,多了参数(-Dmapreduce.job.inputformat.class=com.hadoop.mapreduce.LzoTextInputFormat)
hadoop jar hadoop-mapreduce-examples-2.6.0-cdh5.15.1.jar wordcount -Dmapreduce.job.inputformat.class=com.hadoop.mapreduce.LzoTextInputFormat /data/lzo/trackinfo_20191001.data.lzo /data/output/lzo
文件trackinfo_20191001.data.lzo大小 278.6 M,分成3个splits,符合预期。
参考链接:
https://blog.csdn.net/weixin_43267534/article/details/84928645
https://blog.csdn.net/diyangxia/article/details/80845638
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。