因此,我一直在使用Python中的fabric包来运行各种HDFS任务的shell脚本。
但是,每当我运行任务以检查HDFS中是否已存在文件/目录时,它只会退出外壳程序。这是一个示例(我正在使用Python 3.5.2和Fabric3 == 1.12.post1)
from fabric.api import local
local('hadoop fs -stat hdfs://some/nonexistent/hdfs/dir/')
如果目录不存在,则此代码将产生
[本地主机]本地:hadoop fs -stat hdfs:// some / nonexistent / hdfs / dir / stat:`hdfs:// some / nonexistent / hdfs / dir /':没有这样的文件或目录
致命错误:执行“ hadoop fs -stat hdfs:// some / nonexistent / hdfs / dir /”时,local()遇到错误(返回码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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句