python如何通过jdbc来连接impala?

2021-09-15 14:15发布

python如何通过jdbc来连接impala?需要装什么包么?

python如何通过jdbc来连接impala?需要装什么包么?

14条回答
欧文诺啊
2021-09-16 14:16

1)安装impyla


pip install impyla


工具安装完成后,继续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

1

2

3

4

5

def ExecQuery(self,sql):

cur = self.__GetConnect()

cur.execute(sql)

resList = cur.fetchall()


#查询完毕后必须关闭连接

self.conn.close()

return resList

1

2

3

def ExecNonQuery(self,sql):

cur = self.__GetConnect()

cur.execute(sql)

self.conn.commit()

self.conn.close()

————————————————

版权声明:本文为CSDN博主「liming89」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/liming89/article/details/109609610


一周热门 更多>