我正在运行 hive 0.12,我想运行多个查询并将结果作为 python 数组返回。
例如:
result=[]
for col in columns:
sql='select {c} as cat,count(*) as cnt from {t} group by {c} having cnt > 100;'.format(t=table,c=col)
result.append(hive.query(sql))
result=dict(result)
我缺少的是运行 SQL 查询的 hive
类。
如何做到这一点?
最佳答案
一种快速但肮脏的方法是从命令行自动化配置单元
hive -e "sql command"
这样的东西应该有效
def query(self,cmd):
"""Run a hive expression"""
cmd='hive -e "'+cmd+'"';
prc = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
ret=stdout.split('\n')
ret=[r for r in ret if len(r)]
if (len(ret)==0):
return []
if (ret[0].find('\t')>0):
return [[t.strip() for t in r.split('\t')] for r in ret]
return ret
关于python - 使用 python 自动化 Hive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29938124/