python 连接 hive
方法一
from pyhive import hive import traceback class Hive_tool: def __init__(self): self.conn = hive.Connection(host="******", port=1****, database='ods') self.cur = self.conn.cursor() def execute(self, sql): try: if sql: self.cur.execute(sql) result = self.cur.fetchall() return result except: traceback.print_exc() finally: self.cur.close() self.conn.close()
方法二
import pandas as pd from sqlalchemy import create_engine engine = create_engine('hive://IP:port/dbname') sql = """ 你的hive sql内容 """ data = pd.read_sql(sql,con=engine) data.head() len(data) data.to_csv('文件名字.csv',index=False)