linux下查看lzo格式压缩文件的内容
1.安装lzop
使用yum安装命令:yum install lzop
[root@cm bin]# yum install lzop
Loaded plugins: fastestmirror, security
Setting up Install Process
Determining fastest mirrors
epel/metalink | 3.8 kB 00:00
* epel: dl.fedoraproject.org
base | 3.7 kB 00:00
flink | 2.9 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package lzop.x86_64 0:1.02-0.9.rc1.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================================================================================================
Installing:
lzop x86_64 1.02-0.9.rc1.el6 base 50 k
Transaction Summary
=========================================================================================================================================================================================================
Install 1 Package(s)
Total download size: 50 k
Installed size: 93 k
Is this ok [y/N]: y
Downloading Packages:
lzop-1.02-0.9.rc1.el6.x86_64.rpm | 50 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : lzop-1.02-0.9.rc1.el6.x86_64 1/1
Verifying : lzop-1.02-0.9.rc1.el6.x86_64 1/1
Installed:
lzop.x86_64 0:1.02-0.9.rc1.el6
Complete!
2.编写查看脚本
用户bin目录下新建脚本:lzoc.sh
#!/usr/bin/env bash
#description:
# cat the lzo file on hadoop
filePath="" #full Path of the hadoop lzo file
lzoFileName="" #file with .lzo as extension after hadoop fs -get ....
fileName="" #file name without extension-name
deleteAfterExecute=N #has -c option, which indicates that old files should be deleted
deleteBeforeExecute=N #has -d option, which indicates that related files should be deleted in the final state
interactiveMsg=N #only the text of the file should print
if [ $# -lt 1 ]
then
echo "must has aleast one parameter, which is the fileName."
exit -1
else
#normal command style
eval filePath=\${$#} #get the last parameter, must guarantee that it is less then 9
lzoFileName=${filePath##*/}
fileName=${lzoFileName%.lzo*}
fi
#parase options
if [ $# -gt 1 ]
then
while getopts cdi OPTION
do
case $OPTION
in
c)
deleteBeforeExecute=Y;;
d)
deleteAfterExecute=Y;;
i)
interactiveMsg=Y;;
\?)
echo "illegal option:$OPTION";
exit -2;;
esac
done
fi
#delete old file if needed
if [ $deleteBeforeExecute == "Y" ]; then
if [ -e $fileName ]; then
echo "delete old file"
rm $fileName;
fi
if [ -e $lzoFileName ]; then
echo "delete old lzo file"
rm $lzoFileName
fi
fi
#make sure hadoop is on
which hadoop > /dev/null 2>&1
if [ $? -eq 1 ]; then
echo "Command not exist,hadoop may not have been started."
exit -3
fi
#make sure fileExist,should not be a directory
hadoop fs -test -e $filePath > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "No such file for directory:"$filePath
exit -4
fi
#can not cat a directory
hadoop fs -test -d $filePath > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Can not cat a directory:"$filePath
exit -4
fi
#make sure lzop is installed
which lzop > /dev/null 2>&1
if [ $? -eq 1 ]; then
echo "Tool missed:lzop is not installed."
exit -5
fi
#test whether lzo file exist
if [ -e $lzoFileName ]; then
if [ $interactiveMsg == "Y" ]; then
echo "LZO file already exist."
fi
else
if [ $interactiveMsg == "Y" ]; then
echo "LZO file not exist."
fi
#get the file from hadoop
hadoop fs -get $filePath .
fi
#test whether file exist
if [ -e $fileName ]; then
if [ $interactiveMsg == "Y" ]; then
echo "File already exist."
fi
else
if [ $interactiveMsg == "Y" ]; then
echo "File not exist."
fi
#decomopress the lzo file
lzop -dv $lzoFileName > /dev/null 2>&1
fi
#clear
#cat the file
cat $fileName
#delete files in the final state is needed
if [ $deleteAfterExecute == "Y" ]
then
if [ -e $fileName ]; then
rm $fileName
fi
if [ -e $lzoFileName ]; then
rm $lzoFileName
fi
if [ $interactiveMsg == "Y" ]; then
echo "files has been deleted"
fi
fi
3.测试使用
[root@cm bin]# lzoc.sh /XXXX/2022-02-16/pageview/log.1622990861441.lzo | more
{"ift":true,"url":"pages/index/index","appid":"13900","u":27665841,"wv":"2.7.2","s":"wx","lib":"ali","libv":"1.0.0","rl":"16cd6757-ef16-b55b-6e5e-08fdfb6344c6","net":"","et":1645022851081,"os":"","ua":
"","t":"info","a":"pageview"}
{"ift":true,"url":"pages/index/index","appid":"14000","u":27665841,"wv":"2.7.2","s":"wx","lib":"ali","libv":"1.0.0","rl":"509719c4-8b57-262b-2863-7ff6426c0849","net":"","et":1645022879881,"os":"","ua":
"","t":"info","a":"pageview"}
{"ift":true,"url":"pages/index/index","et":1645022885469,"appid":"13900","u":27665841,"wv":"2.7.2","s":"wx","lib":"ali","libv":"1.0.0","rl":"e6c56acb-b582-7f9d-56b7-d7e6af6b39b4","net":"","os":"","ua":
"","t":"info","a":"pageview"}
.....
注:/XXXX/2022-02-16/pageview/log.1622990861441.lzo 为要查看的lzo文件路径
完成
参考文章:
https://www.linuxidc.com/Linux/2012-11/74745.htm