检查HDFS文件是否压缩的命令

Command to check HDFS file is compressed or not

在linux中使用命令“file <filename>”显示文件是否被压缩。如何为驻留在 HDFS 文件系统中的文件实现此目的?

file 620591952596020.gz
620591952596020.gz: gzip compressed data, from FAT filesystem (MS-DOS, OS/2, NT)

file 269146229598756
269146229598756: ASCII text, with very long lines

这将帮助我避免压缩已经作为通过 Apache Oozie 调用的 Shell 脚本的一部分压缩的文件 (GZip)。

#!/bin/bash
HDFS_IN_PATH=;
IS_COMPRESS_FILE=true;

for archiveDir in 'ARCHIVE1' 'ARCHIVE2' ;
do
    HDFS_OUT_PATH=${HDFS_IN_PATH}/$archiveDir;

    for ls_entry in $(hdfs dfs -ls -C "$HDFS_IN_PATH"/$archiveDir);
    do
        fileAbsPath=$ls_entry;
        jobName=$(basename "${fileAbsPath}");

        if (hadoop fs -test -f "$fileAbsPath") ; then
            echo "Its not a directory ${fileAbsPath}"
            continue;
        fi

        for file in $(hdfs dfs -ls -C "$fileAbsPath");
        do
            filename=$(basename "${file}");

            if [ "$IS_COMPRESS_FILE" = true ]; then

              if(<<***COMMAND TO CHECK HDFS FILE ${file} IS COMPRESSED***>>); then
                  echo "File Name: ${file} is already compressed.."
                  continue;
              fi

              hadoop fs -cat "${file}" | gzip | hadoop fs -put - "${file}".gz;

              echo "Successfully compressed file..!";
            fi
        done

        hadoop archive -archiveName "${jobName}".har -p "${HDFS_OUT_PATH}" "${jobName}" "${HDFS_OUT_PATH}";
    done
done

HDFS 在 Linux 中没有像 file 这样的命令。相反,阅读扩展名可能有效:if [ "$file" == "*.gz" ]

需要在 python 或 Java 中编码的其他选项是:

  • 管理ZipFileInputFormat以确保zip文件是真正的压缩内容。
  • PySpark 似乎有一个可以以 zipfile.ZipFile(in_memory_data, "r").
  • 形式使用的选项

两者都在 中得到了解决。