栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python连接Impala实现步骤解析

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python连接Impala实现步骤解析

Impyla是用于分布式查询引擎的HiveServer2实现(如Impala、Hive)的python客户端

1)安装impyla

pip install impyla

安装报错

解决办法:

根据提示下载对应的工具

https://visualstudio.microsoft.com/zh-hans/downloads/

直接下载安装即可

工具安装完成后,继续pip install impyla

安装成功

代码测试:

from impala.dbapi import connect
conn = connect(host='xxx.xxx.xxx.xxx', port=21050)
cur = conn.cursor()
cur.execute('show databases;')
database_list=cur.fetchall()
for data in database_list:
  print(data)

OK 正常连接

参照以前的Mysql连接工具类,写了个连接Impala的工具类:

from impala.dbapi import connect

class IMPALA:
  def __init__(self,host,port,user,pwd,db):
    self.host = host
    self.port = port
    self.user = user
    self.pwd = pwd
    self.db = db



  def __GetConnect(self):
    if not self.db:
      raise(NameError,"没有设置数据库信息")
    self.conn = connect(host=self.host,port=self.port,user=self.user,password=self.pwd,database=self.db)

    cur = self.conn.cursor()
    if not cur:
      raise(NameError,"连接数据库失败")
    else:
      return cur

  def ExecQuery(self,sql):
    cur = self.__GetConnect()
    cur.execute(sql)
    resList = cur.fetchall()

    #查询完毕后必须关闭连接
    self.conn.close()
    return resList

  def ExecNonQuery(self,sql):
    cur = self.__GetConnect()
    cur.execute(sql)
    self.conn.commit()
    self.conn.close()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

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

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

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