Kat*_*ler 5 python hadoop fabric
因此,我一直在 Python 中使用 Fabric 包来运行各种 HDFS 任务的 shell 脚本。
但是,每当我运行任务来检查 HDFS 中是否已存在文件/目录时,它都会退出 shell。这是一个示例(我使用的是 Python 3.5.2 和 Fabric3==1.12.post1)
from fabric.api import local
local('hadoop fs -stat hdfs://some/nonexistent/hdfs/dir/')
Run Code Online (Sandbox Code Playgroud)
如果目录不存在,此代码将产生
[localhost] 本地:hadoop fs -stat hdfs://some/nonexistent/hdfs/dir/ stat: `hdfs://some/nonexistent/hdfs/dir/': 没有这样的文件或目录
致命错误:local() 在执行“hadoop fs -stat hdfs://some/nonexistent/hdfs/dir/”时遇到错误(返回代码 1)
正在流产。
我也尝试过local('hadoop fs -test -e hdfs://some/nonexistent/hdfs/dir/'),但它引起了同样的问题。
如何使用 Fabric 生成一个布尔变量来告诉我 hdfs 中是否存在目录或文件?
您只需检查succeeded从返回的结果对象的标志即可local。
from fabric.api import local
from fabric.context_managers import settings
file_exists = False
with settings(warn_only=True):
result = local('hadoop fs -stat hdfs://some/nonexistent/hdfs/dir/', capture=True)
file_exists = result.succeeded
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7478 次 |
| 最近记录: |