栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

python / jupyter notebook 连接hive数据库sql

python / jupyter notebook 连接hive数据库sql

使用的是impala包。
如何安装impala:https://blog.csdn.net/weixin_43713105/article/details/111921221

def read_sql(sql):
    '''
    读取hive的sql,二维列表
    参数:
        sql:sql语句
    返回:
        Dataframe
    '''
    from impala.dbapi import connect
    import pandas as pd
    import time
    
    start = time.time()           # 开始时间
    
    host_name = '主机地址'
    port = 10000
    user = '用户名'
    password = '密码'

    conn = connect(host=host_name, port=port, auth_mechanism='PLAIN', user=user, password=password)

    cur = conn.cursor()
    cur.execute(sql)                          # 执行查询语句
    sql_data = cur.fetchall()                 # 获取查询结果:二维列表
    
    end = time.time()                         # 结束时间
    print('耗时%s秒'%(end - start))           # 计算耗时

    columns = [i[0] for i in cur.description]            # dataframe的列名
    sql_data = pd.Dataframe(sql_data, columns=columns)   # 将二维列表转为dataframe,并设置列名
    
    cur.close()
    conn.close()
    
    return sql_data							   # 返回Dataframe


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/600577.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号