从python脚本调用hive -e

时间:2015-05-12 09:05:54

标签: python hive cloudera-cdh

我想从我的python脚本中运行一个非常简单的hive命令。我正在尝试使用hive -e,但我收到错误

def hive():
    cmd = "hive -e \"msck repair table dashboard_report\""
    print(cmd)
    check_call(cmd)

这是我得到的错误

hive -e "msck repair table dashboard_report"
Traceback (most recent call last):
File "/home/yosi/work/source/slg/tiger/src/main/resources/python/tiger.py", line 59, in <module>
hive()
File  "/home/yosi/work/source/slg/tiger/src/main/resources/python/tiger.py", line 57, in hive
check_call(cmd)
File "/usr/lib/python2.7/subprocess.py", line 535, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

2 个答案:

答案 0 :(得分:1)

您的check_call函数正在调用subprocess.Popen。如果要将参数传递给该函数,则必须在列表中传递它们。

大概:

cmd = ["hive", "-e", "\"msck repair table dashboard_report\""]
check_call(cmd)

会工作吗?也许在调用堆栈中需要一些重构来接受列表而不是字符串。

答案 1 :(得分:0)

如果您使用的是python2.7,则下面的代码段将起作用。

import subprocess
command = [""" hive -e "msck repair table dashboard_report" """]
print subprocess.check_output(command,shell=True)