【Hadoop】hdfs dfs -test命令

使用方法

hdfs dfs -test -[defswrz] <path> :
  Answer various questions about <path>, with result via exit status.
    -d  return 0 if <path> is a directory.
    -e  return 0 if <path> exists.
    -f  return 0 if <path> is a file.
    -s  return 0 if file <path> is greater         than zero bytes in size.
    -w  return 0 if file <path> exists         and write permission is granted.
    -r  return 0 if file <path> exists         and read permission is granted.
    -z  return 0 if file <path> is         zero bytes in size, else return 1.

获取返回值

1. shell

$?
[示例]

[root@emr-worker-2 ~]# hadoop fs -test -e /tmp
[root@emr-worker-2 ~]# echo $?
0
[root@emr-worker-2 ~]# hadoop fs -test -e /tmp1
[root@emr-worker-2 ~]# echo $?
1

2. Java

    val cmd = Array("sh", "-c", s"hdfs dfs -test -e /tmp " + " ; echo $?")
    val checkDirExistsProcess = Runtime.getRuntime.exec(cmd)
    val stream = checkDirExistsProcess.getInputStream
    val reader = new BufferedReader(new InputStreamReader(stream))
    var result = ""
    var line = reader.readLine()
    while (line != null) {
      result += line
      line = reader.readLine()
    }
    val exitCode = checkDirExistsProcess.waitFor()
    println(s"checkDirExistsProcess exit code: $exitCode")
    stream.close()
    reader.close()

关于cmd为什么这样写,可以查看这篇博客 https://blog.csdn.net/Sonsay/article/details/109491248

查看方法

hdfs dfs -help
posted @ 2022-12-29 17:09  梦醒江南·Infinite  阅读(229)  评论(0编辑  收藏  举报