欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python连接Impala实现步骤解析

时间:2021-06-07 08:55:30|栏目:Python代码|点击:

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()

上一篇:python实现自主查询实时天气

栏    目:Python代码

下一篇:python探索之BaseHTTPServer-实现Web服务器介绍

本文标题:Python连接Impala实现步骤解析

本文地址:http://www.codeinn.net/misctech/137267.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有