windows下怎么用python连接hive数据库

windows下怎么用python连接hive数据库,第1张

由于版本的不同,Python 连接 Hive 的方式也就不一样。

在网上搜索关键字 python hive 的时候可以找到一些解决方案。大部分是这样的,首先把hive 根目录下的$HIVE_HOME/lib/py拷贝到 python 的库中,也就是 site-package 中,或者干脆把新写的 python 代码和拷贝的 py 库放在同一个目录下,然后用这个目录下提供的 thrift 接口调用。示例也是非常简单的。类似这样:

import sys

from hive_service import ThriftHive

from hive_servicettypes import HiveServerException

from thrift import Thrift

from thrifttransport import TSocket

from thrifttransport import TTransport

from thriftprotocol import TBinaryProtocol

def hiveExe(sql):

try:

transport = TSocketTSocket('127001', 10000)

transport = TTransportTBufferedTransport(transport)

protocol = TBinaryProtocolTBinaryProtocol(transport)

client = ThriftHiveClient(protocol)

transportopen()

clientexecute(sql)

print "The return value is : "

print clientfetchAll()

print ""

transportclose()

except ThriftTException, tx:

print '%s' % (txmessage)

if __name__ == '__main__':

hiveExe("show tables")1234567891011121314151617181920212223242526272812345678910111213141516171819202122232425262728

或者是这样的:

#!/usr/bin/env python

import sys

from hive import ThriftHive

from hivettypes import HiveServerException

from thrift import Thrift

from thrifttransport import TSocket

from thrifttransport import TTransport

from thriftprotocol import TBinaryProtocol

try:

transport = TSocketTSocket('1418154188', 10000)

transport = TTransportTBufferedTransport(transport)

protocol = TBinaryProtocolTBinaryProtocol(transport)

client = ThriftHiveClient(protocol)

transportopen()

clientexecute("CREATE TABLE r(a STRING, b INT, c DOUBLE)")

clientexecute("LOAD TABLE LOCAL INPATH '/path' INTO TABLE r")

clientexecute("SELECT FROM test1")

while (1):

row = clientfetchOne()

if (row == None):

break

print rowve

clientexecute("SELECT FROM test1")

print clientfetchAll()

transportclose()

except ThriftTException, tx:

print '%s' % (txmessage)

12345678910111213141516171819202122232425262728293031323334351234567891011121314151617181920212223242526272829303132333435

但是都解决不了问题,从 netstat 中查看可以发现 TCP 连接确实是建立了,但是不执行 hive 指令。也许就是版本的问题。

还是那句话,看各种中文博客不如看官方文档。

项目中使用的 hive 版本是013,此时此刻官网的最新版本都到了121了。中间间隔了120、110、100、0140。但是还是参考一下官网的方法试试吧。

首先看官网的 setting up hiveserver2

可以看到启动 hiveserver2 可以配置最大最小线程数,绑定的 IP,绑定的端口,还可以设置认证方式。(之前一直不成功正式因为这个连接方式)然后还给了 python 示例代码。

import pyhs2

with pyhs2connect(host='localhost',

port=10000,

authMechanism="PLAIN",

user='root',

password='test',

database='default') as conn:

with conncursor() as cur:

#Show databases

print curgetDatabases()

#Execute query

curexecute("select from table")

#Return column info from query

print curgetSchema()

#Fetch table results

for i in curfetch():

print i123456789101112131415161718192021123456789101112131415161718192021

在拿到这个代码的时候,自以为是的把认证信息给去掉了。然后运行发现跟之前博客里介绍的方法结果一样,建立了 TCP 连接,但是就是不执行,也不报错。这是几个意思?然后无意中尝试了一下原封不动的使用上面的代码。结果可以用。唉。。。

首先声明一下,hive-sitexml中默认关于 hiveserver2的配置我一个都没有修改,一直是默认配置启动 hiveserver2。没想到的是默认配置是有认证机制的。

然后再写一点,在安装 pyhs2的时候还是遇到了点问题,其实还是要看官方文档的,我只是没看官方文档直接用 pip安装导致了这个问题。安装 pyhs2需要确定已经安装了几个依赖包。直接看在 github 上的 wiki 吧。哪个没安装就补上哪一个就好了。

To install pyhs2 on a clean CentOS 64 64-bit desktop

(as root or with sudo)

get ez_setuppy from >

以上就是关于windows下怎么用python连接hive数据库全部的内容,包括:windows下怎么用python连接hive数据库、python连接数据库用什么,给个下载地址、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9841122.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存