Python连接Hive相关配置_MY测试之道的博客-CSDN博客

"""
hive数据库相关操作
"""

from pyhive import hive

class HiveUtils():

    def __init__(self, host, port=10000, username=None, password=None, database=None, auth=None):
        try:
            self.conn = hive.Connection(host=host,port=port, username=username, password=password, database=database, auth=auth)
            self.cur = self.conn.cursor()
            print("初始化连接hive数据库成功!")
        except Exception as e:
            raise f"初始化连接hive失败,失败原因: {e}"

    def execute_sql(self, sql: str):
        """执行SQL语句"""
        if not isinstance(sql, str):
            raise ValueError("参数:sql 数据类型错误!应是str类型")
        try:
            results = None
            self.cur.execute(sql)
            if "show" in sql or "select" in sql:
                results = self.cur.fetchall()
            self.conn.commit()
            print("SQL语句执行成功!")
            return results
        except Exception as e:
            raise f"SQL语句执行失败!,失败原因: {e}"

    def close(self):
        """关闭游标和hive连接"""
        self.cur.close()
        self.conn.close()

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐