前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python执行hive sql

Python执行hive sql

作者头像
py3study
发布2020-01-07 10:31:03
4K0
发布2020-01-07 10:31:03
举报
文章被收录于专栏:python3python3

该python脚本是用于执行hive脚本的,需要设置hive的可执行环境变量,其实质转化为shell下命令 hive -e 'sql语句’ 的方式执行,然后把结果重定向到控制台显示。注:由于该脚本是直接调用shell中的hive命令,所以需要在安装hive的服务器上执行。

使用前置条件:(1)安装hadoop和hive,并启动完hadoop;(2)已配置好hive的环境变量,确保在shell中能正常执行hive。

代码语言:javascript
复制
#!/usr/bin/python
#-*-coding:utf-8 -*-

import subprocess
import traceback

sql = """
# 书写hql脚本
;
"""
cmd = 'hive -e """'+sql.replace('"', "\'")+'"""'
print cmd
try:
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    while True:
        buff = p.stdout.readline()
        print buff
        if buff == '' and p.poll() != None:
            break

except Exception,re:
    print "message is:%s" %(str(re))
    traceback.print_exc();

脚本举例

代码语言:javascript
复制
#!/usr/bin/python
#-*-coding:utf-8 -*-
import subprocess
import traceback
sql = """
  select * from app_tianhe_zym_item_reason_dtl_da where order_dt = '2016-01-26' limit 10;
"""
cmd = 'hive -e """'+sql.replace('"', "\'")+'"""'
print cmd
try:
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    while True:
        buff = p.stdout.readline()
        print buff
        if buff == '' and p.poll() != None:
            break
except Exception,re:
    print "message is:%s" %(str(re))
    traceback.print_exc();
这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档