Python中执行Hql脚本
来源:互联网 发布:网络舆情监测平台 编辑:程序博客网 时间:2024/06/06 01:42
该python脚本是用于执行hive脚本的,需要设置hive的可执行环境变量,其实质转化为shell下命令 hive -e ‘sql语句’ 的方式执行,然后把结果重定向到控制台显示。注:由于该脚本是直接调用shell中的hive命令,所以需要在安装hive的服务器上执行。
使用前置条件:(1)安装hadoop和hive,并启动完hadoop;(2)已配置好hive的环境变量,确保在shell中能正常执行hive。
#!/usr/bin/python#-*-coding:utf-8 -*-import subprocessimport tracebacksql = """# 书写hql脚本;"""cmd = 'hive -e """'+sql.replace('"', "\'")+'"""'print cmdtry: p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) while True: buff = p.stdout.readline() print buff if buff == '' and p.poll() != None: breakexcept Exception,re: print "message is:%s" %(str(re)) traceback.print_exc();
脚本举例
#!/usr/bin/python#-*-coding:utf-8 -*-import subprocessimport tracebacksql = """ select * from app_tianhe_zym_item_reason_dtl_da where order_dt = '2016-01-26' limit 10;"""cmd = 'hive -e """'+sql.replace('"', "\'")+'"""'print cmdtry: p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) while True: buff = p.stdout.readline() print buff if buff == '' and p.poll() != None: breakexcept Exception,re: print "message is:%s" %(str(re)) traceback.print_exc();
0 0
- Python中执行Hql脚本
- 如何在IIS中执行Python脚本
- Python脚本中执行shell命令
- 如何执行python脚本
- Python脚本定期执行
- Python脚本定期执行
- python linux 脚本执行
- Python 执行MySQL 脚本
- centos执行Python脚本
- python自动执行脚本
- Python执行shell脚本
- nodejs 执行python脚本
- crontab 中 python 脚本执行失败的解决方法
- 不安装IronPython在C#中执行python脚本
- python脚本中获得执行终端命令返回的内容
- crontab 中 python 脚本执行失败的解决方法
- C/C++中调用执行python脚本文件
- 在crontab中执行python脚本系统环境变量问题
- poj1426bfs
- spring boot 1.4 整合 mybatis druid
- LESS学习笔记(初学实用)
- CF 13C
- 魅族公布苏宁818战报:魅蓝3s斩获单品销量冠军
- Python中执行Hql脚本
- 仿京东商城侧边导航栏写法
- caffe学习(5)------caffe for ubuntu环境配置
- 网易有道2017内推编程题-Java实现
- Difference between HashMap, LinkedHashMap and TreeMap
- java8 四个核心函数式接口
- 11. Container With Most Water
- SVM技术详解(上)
- 数据结构之图---最小生成树Kruskal算法---C++实现