知识点
相关文章
更多最近更新
更多查看Hadoop里的LZO文件的内容
2019-03-28 13:20|来源: 网络
最近常常需要查看LZO文件里面的内容,这些文件通常很大,放在hdfs上。我没有好的方法,我以前偶尔查看其中内容都是直接get到本地然后用lzop解压缩然后再more的。这样做当你偶尔使用的时候即使文件稍微大点,也许也是可以接受的。但现在我需要常常grep里面的内容,就不那么欢乐了。
所以写了个shell脚本lzoc[ lzo cat],用来专门查看HDFS里LZO文件的内容,正常情况下它不输出任何多余的东西,这样就可以和more 、 head、tail等工具一起结合使用了。
代码如下:
它有三个选项:
- -c 指示删除已经存在当前目录的同名文件,这往往是为了删除旧的副本而制定的,
- -d 指示最后阶段删除当前目录里中间文件,因为我们会把文件从hdfs中get出来
- -i 指示输出一些交互信息,如果你cat出来的内容要用作它用,那么你不要使用这个选项
使用示例:
$./lzoc /user/Hadoop/output/filename.lzo | more
#! /bin/sh
#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
相关问答
更多-
hadoop怎么查看本地文件[2021-04-18]
命令:hadoop fs -ls / webUI:namenode节点:50070 如果是要看节点上的文件的话,是看不到的,毕竟是一个分布式文件系统,你要看本地文件,你也得知道是在哪个主机上呀 -
为什么启动hadoop后在hdfs上系统里创建input的文件夹的命令没有用 命令为hadoop[2023-07-08]
mkdir Usage: hadoop fs -mkdir [-p] Takes path uri’s as argument and creates directories. Options: The -p option behavior is much like Unix mkdir -p, creating parent directories along the path. Example: hadoop fs -mkdir /user/hadoop/dir1 /user/hadoop/dir2 h ... -
hadoop 函数包放在哪个文件里[2022-02-17]
网上下载hadoop压缩包(以1.0.1为例),解压后:src/mapred/org/apache/hadoop/mapreduce 和src/core/org/apache/hadoop/conf (fs、io、util) -
如何查看文件损坏Hadoop[2022-03-03]
问题原因:在hdfs-site中配置的副本书为1,而且把两台机子的文件给清理了,导致一部分数据丢失,而且无法恢复,报错导致hbase无法访问60010端口 解决办法:使用 hadoop fsck / 列出损坏文件,损坏的文件无法恢复,只能删除损坏的文件 hadoop fsck -delete -
我通过首先创建一个RDD来识别索引,然后使用from_json函数将每行转换为StructType ,从而有效地生成spark.read.json(...)类似结果, posts_rdd = sc.newAPIHadoopFile(posts_dir, 'com.hadoop.mapreduce.LzoTextInputFormat', 'org.apache.hadoop.io. ...
-
以下内容将显示指定的行数而不解压整个文件: hadoop fs -cat /hdfs_location/part-00000.gz | zcat | head -n 20 以下内容将对该文件进行分页,也不需要先解压缩整个文件: hadoop fs -cat /hdfs_location/part-00000.gz | zmore I ended up writing a pig script. A = LOAD '/myfolder/part-r-00024.gz' USING PigStorage(' ...
-
如何用-c开关在子lzop中启动lzop二进制文件 ,然后逐行读取其STDOUT? How about starting an lzop binary in a subprocess with -c switch and then read its STDOUT line by line?
-
对我的第一个问题的简短回答: AWS不会自动编制索引。 我已经用自己的工作证实了这一点,并且在他们的论坛上也从Andrew @ AWS中读到了相同的内容。 以下是如何进行索引编制的方法: 要索引一些LZO文件,你需要使用我自己的从hadoop-lzo项目构建的Jar。 如果要直接使用EMR进行索引,则需要在某处构建Jar,然后上传到Amazon S3。 另外,Cloudera对在您自己的群集上进行此设置的所有步骤都有很好的说明。 我在我的本地群集上做了这个,这允许我构建Jar并上传到S3。 如果您不想自己构 ...
-
在普通Java中用原始文件创建lzo文件和Lzo索引文件(Create lzo file and Lzo index file from raw file in plain Java)[2023-12-13]
因此,在hadoop世界之外没有可用的库来创建lzo和lzo索引文件。 这基本上让我们使用像hadoop-lzo这样的开源项目(调用native c ++ lzo library)和lzo-java(其中包含lzo压缩的java实现) So there aren't libraries readily available outside hadoop world to create lzo and lzo index files. Which basically leaves us to use open ... -
我得到了与你相同的结果,最终这对我有用: export C_INCLUDE_PATH=/usr/local/Cellar/lzo/2.09/include/lzo:/usr/local/Cellar/lzo/2.09/include export LIBRARY_PATH=/usr/local/lib pip install python-lzo (显然,您可能需要调整这些,具体取决于您安装的lzo brew发行版的版本。) 哪个回答了我的问题,但我真的不明白为什么没有正确配置... 我也安装了Xco ...