python - 使用 python 自动化 Hive

标签 python python-2.7 hadoop hive

我正在运行 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/

相关文章:

python - Spark,关于reduceByKey的小问题

python - Gtk3 接口(interface)中的 Gtk.main() 阻塞

python - Seaborn.countplot : order categories by count

python - 函数参数类型设置返回 SyntaxError

python - 使用 python 从 dynamoDB 获取项目的唯一值

python-2.7 - 如何测试行为中未捕获/未处理的异常?

python - 如何配置 hive.Connection() 以将我想要的设置传递给 Hive?

python - 将 pandas 列中的字典转换为数据框

scala - 在SPARK SQL中读取分区的HIVE表

hadoop - Hive中的行级事务