Python - 使用子进程的 Hive 命令 - 空结果

Python - Hive commands using Subprocess - empty results

我在 python 中对 运行 配置单元命令使用子进程,但得到的结果是空的。如果我 运行 来自 hive CLI 的相同命令,我会得到结果。

 query = "set hive.cli.print.header=true;use mydb;describe table1;"  
 process = subprocess.Popen( ["ssh", "hadoop" , "hive", "-e", "%r" % query], stdout = subprocess.PIPE, stderr = subprocess.PIPE )  
 data = [line.split('\t') for line in process.stdout]  
 cols = list(itertools.chain.from_iterable(data[:1]))  
 df = pd.DataFrame(data[1:], columns = cols)  
 print "==>"+df+"<----"  

它正在返回空数据帧。

请帮我解决这个问题

myfile=open("query_result.tsv", 'w')
p=subprocess.Popen("your query",
        shell=True,
        stdout=myfile,stderr=subprocess.PIPE)
stdout,stderr = p.communicate()
if p.returncode != 0:
    print stderr
    sys.exit(1)

我的文件是一个tsv文件,你可以使用pandas.read_csv(sep='\t') 并设置sep='\t' ,您可能需要查找 pandas api 以找到有关 read_csv() 的更多用法。

你应该查看 17.1.2 中关于 Popen 的子进程 api Object.it 给你一个关于 stdout=PIPE 的警告。 https://docs.python.org/2/library/subprocess.html#frequently-used-arguments