需要借助exe_command
函数,该函数通过subprocess执行 shell 命令,并实时打印输出。
from subprocess import Popen, PIPE, STDOUTdef exe_command(command):"""执行 shell 命令并实时打印输出:param command: shell 命令:return: process, exitcode"""print(command)process = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True)with process.stdout:for line in iter(process.stdout.readline, b''):print(line.decode().strip())exitcode = process.wait()return process, exitcodedef exe_sql_command(sql_command, redirect=None):"""执行 hive sql 命令并转储到文件:param sql_command: sql 命令:param redirect: 是否重定向输出到文件:return: process, exitcode"""command = "hive -e \"%s\"" % sql_commandif redirect:command += f" > {redirect}"return exe_command(command)def exe_sql_file(sql_file, redirect=None):"""执行 hive sql 文件并转储到文件:param sql_file: sql 文件:param redirect: 是否重定向输出到文件:return: process, exitcode"""command = f"hive -f {sql_file}"if redirect:command += f" > {redirect}"return exe_command(command)
如果觉得《Python执行 SQL 命令并实时打印输出》对你有帮助,请点赞、收藏,并留下你的观点哦!