bash 使用shell脚本查找文件在HDFS中是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43900364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:08:28  来源:igfitidea点击:

Find whether file exists or not in HDFS using shell script

linuxbashshellhadoophdfs

提问by Ambrish

I have a shell scriptlike below. This script prints the path of a file located in HDFS

我有一个shell script喜欢下面。此脚本打印位于HDFS

#!/bin/bash

TIMESTAMP=`date "+%Y-%m-%d"`
path=/user/$USER/logs/${TIMESTAMP}.fail_log
path1=/user/$USER/logs/`date -d "-1 days" '+%Y-%m-%d'`.fail_log

echo filePath=$path
echo filePath1=$path1

In the script the pathsprovided are hdfslocations

在脚本中paths提供的是hdfs位置

In this script I am getting the filepath.

在这个脚本中,我得到了文件路径。

Now I want to know whether the file actually exists or not in HDFS.

现在我想知道该文件是否确实存在于HDFS.

If the file exists then only print the filepath or else do nothing.

如果文件存在,则只打印文件路径,否则什么都不做。

How can I do that?

我怎样才能做到这一点?

回答by Ambrish

You can try -testoption to achieve the same.

您可以尝试-test选择来实现相同的目标。

hdfs dfs -test -[defszrw] HDFS_PATH
  • -d:if the path is a directory, return 0.
  • -e:if the path exists, return 0.

Since 2.7.0

  • -f:if the path is a file, return 0.
  • -s:if the path is not empty, return 0.
  • -r:if the path exists and read permission is granted, return 0.

since 2.8.0

  • -w:if the path exists and write permission is granted, return 0.
  • -z:if the file is zero-length, return 0.
hdfs dfs -test -[defszrw] HDFS_PATH
  • -d:如果路径是目录,则返回 0。
  • -e:如果路径存在,返回0。

从 2.7.0 开始

  • -f:如果路径是文件,则返回 0。
  • -s:如果路径不为空,则返回0。
  • -r:如果路径存在并且被授予读取权限,则返回0。

自 2.8.0

  • -w:如果路径存在并且被授予写权限,则返回0。
  • -z:如果文件长度为零,则返回 0。

Example:

例子:

if hdfs dfs -test -e $HDFS_PATH; then
    echo "[$HDFS_PATH] exists on HDFS"
    hdfs dfs -ls $HDFS_PATH
fi

Reference: https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/FileSystemShell.html#test

参考:https: //hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/FileSystemShell.html#test