python如何通过jdbc来连接impala?

2021-09-15 14:15发布

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

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

12条回答

image.png1

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)

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

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

一个Ai
4楼 · 2021-09-16 13:45

环境:Centos6.5 python2.7

1、下载并安装Python package:impyla-0.14.0.tar.gz


https://pypi.python.org/packages/6c/30/da9fe733561eb948a07aaef3ae0240ac6a5466cfea5e6872525515634544/impyla-0.14.0.tar.gz

python setup.py install

或者pip install impyla

2、python客户端与impala交互


连接impala:部署的ip--hive,默认端口:21050

from impala.dbapi import connect

conn = connect(host='192.168.0.10', port=21050)

cur = conn.cursor()

cur.execute('select name as num from user ;')

data_list=cur.fetchall()

for data in data_list:

    #用户列表

   print "用户名称:",data

欧文诺啊
5楼 · 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


aijingda
6楼 · 2021-09-18 15:37

打开python安装路径下的thriftpy文件夹,找到parse文件,修改第488行的if url_scheme==''

为 if len(url_scheme)<=1 即可

保存后重新启动你的python shell或者jupyter notebook

大工告成!!

from impala.dbapi import connect
from impala.util import as_pandas
import pandas as pd
import numpy as np
#连接数据
conn=connect(host='地址啊喂',port=端口号啊喂)
cur=conn.cursor()
cur.execute('show tables')
print(cur.fetchall())

#查看输出结果吧,有表名就都连上啦~


爱学习的小巴
7楼 · 2021-09-22 09:24

环境:Centos6.5 python2.7

1、下载并安装Python package:impyla-0.14.0.tar.gz

2、python客户端与impala交互

连接impala:部署的ip--hive,默认端口:21050

from impala.dbapi import connect

conn = connect(host='192.168.0.10', port=21050)

cur = conn.cursor()

cur.execute('select name as num from user ;')

data_list=cur.fetchall()

for data in data_list:

    #用户列表

   print "用户名称:",data

嘿呦嘿呦拔萝卜
8楼 · 2021-09-22 10:07

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)

不吃鱼的猫
9楼 · 2021-09-25 10:00

导入jar吧,开启驱动、输入账号密码登入。