Skip to content

Instantly share code, notes, and snippets.

@xiaoshuai
Created November 16, 2018 16:27
Show Gist options
  • Save xiaoshuai/92e64d73f3580950e7d0e20cbfc59c3b to your computer and use it in GitHub Desktop.
Save xiaoshuai/92e64d73f3580950e7d0e20cbfc59c3b to your computer and use it in GitHub Desktop.
hive.py : call hive command via python3
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""hive.py: call hive command via python3"""
__author__ = "xiaoshuai, http://github.com/xiaoshuai"
__license__ = "GPL"
__version__ = "1.0.1"
import subprocess
import sys
from subprocess import check_output, CalledProcessError
PY3 = sys.version_info[0] == 3
if not PY3:
print('Only Python3.6 above supported.', file=sys.stderr)
sys.exit(1)
class HiveCommandError(RuntimeError):
def __init__(self, message):
super(HiveCommandError, self).__init__(message)
self.message = message
print(self.message, file=sys.stderr)
class HiveClient:
def __init__(self, hive_cmd='hive -S', is_return_byte=False, debug=False):
self.hive_cmd = hive_cmd.split(' ')
self.is_return_byte = is_return_byte
self.debug = debug
@staticmethod
def __local_file_exists(local_path):
import os.path
isfile = os.path.isfile(local_path)
return isfile
def __run_hive(self, args, check_return_code=True):
cmd = self.hive_cmd + args
if self.debug:
debug_message = 'hive command "{0}" will call.'.format(subprocess.list2cmdline(cmd))
print(debug_message, file=sys.stderr)
try:
stdout = check_output(cmd)
except CalledProcessError as err:
message = 'hive command "{0}" failed with error code {1}.'.format(' '.join(err.cmd), err.returncode)
if check_return_code:
raise HiveCommandError(message) from err
else:
stdout = err.stdout
if not self.is_return_byte:
stdout = stdout.decode('utf-8')
return stdout
def run_hive_cmd(self, cmd, check_return_code=True):
"""
执行hive查询,返回结果,结果末尾会有换行符
"""
return self.__run_hive(['-e', cmd], check_return_code)
def run_hive_script(self, script_file_path):
"""
执行hive脚本,返回结果,结果末尾会有换行符
"""
if not self.__local_file_exists(script_file_path):
raise HiveCommandError('Hive script: {0} does not exist.'.format(script_file_path))
return self.__run_hive(['-f', script_file_path])
if __name__ == '__main__':
# /opt/python36/bin/python3 test_hive.py
hive = HiveClient(debug=True)
hsq_str = '''SELECT current_date from dual;'''
output = hive.run_hive_cmd(hsq_str)
with open('hive_result_viapy.txt', mode='w', encoding='utf-8') as f:
f.write(output)
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment