Python使用JDBC连接Hive

场景描述:业务需要,客户要求。

上网搜了半天,只有连oracle或者mysql的,只能是借鉴一下看看能不能用了。

大多数文章上说要用JayDeBeApi 包

直接用

直接进cmd pip install JayDeBeApi 

报错了,

error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

解决办法

1. 安装 Microsoft visual c++ 14.0

https://964279924.ctfile.com/fs/1445568-239446865

安装完成之后再

pip install JayDeBeApi 

样例代码如下:

根据网上其他人写的一些进行修改,主要是 jarFile ,链接oracle 只需要引入ojdbc的jar包就行了,hive这里需要使用列表,把所有相关的jar都放到jarFile中。

# Mike Sun
import jaydebeapi

url = 'jdbc:hive2:/127.0.0.1:10000/default'
user = 'hive'
password = 'hive'
dirver = 'org.apache.hive.jdbc.HiveDriver'
jarFile = [
'D:\\Maven\\org\\apache\\hive\\hive-jdbc\\1.2.1\\hive-jdbc-1.2.1.jar',
'D:\\Maven\\org\\apache\\hive\\hive-exec\\1.2.1\\hive-exec-1.2.1.jar',
'D:\\Maven\\org\\apache\\hive\\hive-metastore\\1.2.1\\hive-metastore-1.2.1.jar',
'D:\\Maven\\org\\apache\\hive\\hive-service\\1.2.1\\hive-service-1.2.1.jar',
'D:\\Maven\\org\\apache\\hadoop\\hadoop-common\\2.7.4\\hadoop-common-2.7.4.jar',
'D:\\Maven\\org\\apache\\thrift\\libfb303\\0.9.2\\libfb303-0.9.2.jar',
'D:\\Maven\\commons-logging\\commons-logging\\1.2\\commons-logging-1.2.jar','D:\\Maven\\org\\slf4j\\slf4j-api\\1.7.25\\slf4j-api-1.7.25.jar',
'D:\\Maven\\org\\apache\\httpcomponents\\httpclient\\4.5.5\\httpclient-4.5.5.jar','D:\\Maven\\org\\apache\\httpcomponents\\httpcore\\4.4.9\\httpcore-4.4.9.jar',
'D:\\Maven\\org\\slf4j\\slf4j-simple\\1.7.25\\slf4j-simple-1.7.25.jar']

sqlStr = 'select * from test_table'

conn = jaydebeapi.connect(dirver, url, [user, password], jarFile)
curs = conn.cursor()
curs.execute(sqlStr)
result = curs.fetchall()
print(result)
curs.close()
conn.close()


参考文章
https://blog.csdn.net/cakecc2008/article/details/79073181
https://blog.csdn.net/weixin_42057852/article/details/80857948

猜你喜欢

转载自www.cnblogs.com/MikeSunny/p/10021215.html
今日推荐