当前位置:首页 » 密码管理 » python访问hive

python访问hive

发布时间: 2022-05-16 11:51:58

A. python 访问 hive pyhs2 端口号 是多少

2、JDBC连接的方式,当然还有其他的连接方式,比如ODBC等, 这种方式很常用,可以在网上随便找到,就不再累赘了。不稳定,经常会被大数据量冲挂,不建议使用。 3、这种方式是直接利用Hive的 Driver class 来直接连接,感觉这种方式不通过JDBC,应该速度会比较快一点(未经验证)。我只是在local模式下测试过。

B. 如何用python链接hive

看你的需求应该是一个python新手,建议你看一下django这个python的web框架,它应该能满足你的需求,同时也能让你对python的web开发有一个了解。如果解决了您的问题请采纳!如果未解决请继续追问

C. windows下python怎么连接hive数据库

setting.py:都要配置下USER, PASSWORD, HOST

Mysql插件 :MySQL-python version 1.2.1p2 or higher.
ENGINE :django.db.backends.mysql
NAME:你的数据库名字

PostgreSQL插件:postgresql_psycopg2
ENGINE :django.db.backends.postgresql_psycopg2
NAME:你的数据库名字
例如我的oracle配置:

D. python连接hive的时候必须要依赖sasl类库吗

客户端连接Hive需要使用HiveServer2。HiveServer2是HiveServer的重写版本,HiveServer不支持多个客户端的并发请求。当前HiveServer2是基于Thrift RPC实现的。它被设计用于为像JDBC、ODBC这样的开发API客户端提供更好的支持。Hive 0.11版本引入的HiveServer2。

HiveServer2的启动

启动HiveServer2

HiveServer2的启动十分简便:

$ $HIVE_HOME/bin/hiveserver2

或者

$ $HIVE_HOME/bin/hive --service hiveserver2

默认情况下,HiverServer2的Thrift监听端口是10000,其WEB UI端口是10002。可通过来查看HiveServer2的Web UI界面,这里显示了Hive的一些基本信息。如果Web界面不能查看,则说明HiveServer2没有成功运行。

使用beeline测试客户端连接

HiveServer2成功运行后,我们可以使用Hive提供的客户端工具beeline连接HiveServer2。

$ $HIVE_HOME/bin/beeline

beeline > !connect jdbc:hive2://localhost:10000

如果成功登录将出现如下的命令提示符,此时可以编写HQL语句。

0: jdbc:hive2://localhost:10000>

报错:User: xxx is not allowed to impersonate anonymous

在beeline使用!connect连接HiveServer2时可能会出现如下错误信息:

12Caused by: org.apache.hadoop.ipc.RemoteException:User: xxx is not allowed to impersonate anonymous

这里的xxx是我的操作系统用户名称。这个问题的解决方法是在hadoop的core-size.xml文件中添加xxx用户代理配置:

123456789<spanclass="hljs-tag"><<spanclass="hljs-title">property><spanclass="hljs-tag"><<spanclass="hljs-title">name>hadoop.proxyuser.xxx.groups<spanclass="hljs-tag"></<spanclass="hljs-title">name><spanclass="hljs-tag"><<spanclass="hljs-title">value>*<spanclass="hljs-tag"></<spanclass="hljs-title">value><spanclass="hljs-tag"></<spanclass="hljs-title">property><spanclass="hljs-tag"><<spanclass="hljs-title">property><spanclass="hljs-tag"><<spanclass="hljs-title">name>hadoop.proxyuser.xxx.hosts<spanclass="hljs-tag"></<spanclass="hljs-title">name><spanclass="hljs-tag"><<spanclass="hljs-title">value>*<spanclass="hljs-tag"></<spanclass="hljs-title">value><spanclass="hljs-tag"></<spanclass="hljs-title">property></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

重启HDFS后,再用beeline连接HiveServer2即可成功连接。

常用配置

HiveServer2的配置可以参考官方文档《Setting Up HiveServer2》

这里列举一些hive-site.xml的常用配置:

hive.server2.thrift.port:监听的TCP端口号。默认为10000。

hive.server2.thrift.bind.host:TCP接口的绑定主机。

hive.server2.authentication:身份验证方式。默认为NONE(使用 plain SASL),即不进行验证检查。可选项还有NOSASL, KERBEROS, LDAP, PAM and CUSTOM.

hive.server2.enable.doAs:是否以模拟身份执行查询处理。默认为true。

Python客户端连接HiveServer2

python中用于连接HiveServer2的客户端有3个:pyhs2,pyhive,impyla。官网的示例采用的是pyhs2,但pyhs2的官网已声明不再提供支持,建议使用impyla和pyhive。我们这里使用的是impyla。

impyla的安装

impyla必须的依赖包括:

  • six

  • bit_array

  • thriftpy(python2.x则是thrift)

  • 为了支持Hive还需要以下两个包:

  • sasl

  • thrift_sasl

  • 可在Python PI中下载impyla及其依赖包的源码

    impyla示例

    以下是使用impyla连接HiveServer2的示例:

    1234567891011
  • fromimpala.dbapi import<span class="hljs-keyword">connectconn =<span class="hljs-keyword">connect(host=<span class="hljs-string">'127.0.0.1', port=<span class="hljs-number">10000, database=<span class="hljs-string">'default', auth_mechanism=<span class="hljs-string">'PLAIN')cur =conn.cursor()cur.execute(<span class="hljs-string">'SHOW DATABASES')<span class="hljs-keyword">print(cur.fetchall())cur.execute(<span class="hljs-string">'SHOW Tables')<span class="hljs-keyword">print(cur.fetchall())</span></span></span></span></span></span></span></span></span></span>

E. python如何增量读取hive数据,每次执行脚本把上次的结果做基准,打印出新增的部分

1.读取文本文件数据(.txt结尾的文件)或日志文件(.log结尾的文件)list0与list1分别为文档中的第一列数据与第二列数据。

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

由于版本的不同,Python 连接 Hive 的方式也就不一样。
在网上搜索关键字 python hive 的时候可以找到一些解决方案。大部分是这样的,首先把hive 根目录下的$HIVE_HOME/lib/py拷贝到 python 的库中,也就是 site-package 中,或者干脆把新写的 python 代码和拷贝的 py 库放在同一个目录下,然后用这个目录下提供的 thrift 接口调用。示例也是非常简单的。类似这样:
import sys
from hive_service import ThriftHive
from hive_service.ttypes import HiveServerException
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

def hiveExe(sql):

try:
transport = TSocket.TSocket('127.0.0.1', 10000)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = ThriftHive.Client(protocol)
transport.open()

client.execute(sql)

print "The return value is : "
print client.fetchAll()
print "............"
transport.close()
except Thrift.TException, tx:
print '%s' % (tx.message)

if __name__ == '__main__':
hiveExe("show tables")171819202122232425262728

或者是这样的:
#!/usr/bin/env python

import sys

from hive import ThriftHive
from hive.ttypes import HiveServerException
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

try:
transport = TSocket.TSocket('14.18.154.188', 10000)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = ThriftHive.Client(protocol)
transport.open()

client.execute("CREATE TABLE r(a STRING, b INT, c DOUBLE)")
client.execute("LOAD TABLE LOCAL INPATH '/path' INTO TABLE r")
client.execute("SELECT * FROM test1")
while (1):
row = client.fetchOne()
if (row == None):
break
print rowve
client.execute("SELECT * FROM test1")
print client.fetchAll()

transport.close()

except Thrift.TException, tx:
print '%s' % (tx.message)


但是都解决不了问题,从 netstat 中查看可以发现 TCP 连接确实是建立了,但是不执行 hive 指令。也许就是版本的问题。
还是那句话,看各种中文博客不如看官方文档。
项目中使用的 hive 版本是0.13,此时此刻官网的最新版本都到了1.2.1了。中间间隔了1.2.0、1.1.0、1.0.0、0.14.0。但是还是参考一下官网的方法试试吧。
首先看官网的 setting up hiveserver2
可以看到启动 hiveserver2 可以配置最大最小线程数,绑定的 IP,绑定的端口,还可以设置认证方式。(之前一直不成功正式因为这个连接方式)然后还给了 python 示例代码。
import pyhs2

with pyhs2.connect(host='localhost',
port=10000,
authMechanism="PLAIN",
user='root',
password='test',
database='default') as conn:
with conn.cursor() as cur:
#Show databases
print cur.getDatabases()

#Execute query
cur.execute("select * from table")

#Return column info from query
print cur.getSchema()

#Fetch table results
for i in cur.fetch():
print

在拿到这个代码的时候,自以为是的把认证信息给去掉了。然后运行发现跟之前博客里介绍的方法结果一样,建立了 TCP 连接,但是就是不执行,也不报错。这是几个意思?然后无意中尝试了一下原封不动的使用上面的代码。结果可以用。唉。。。
首先声明一下,hive-site.xml中默认关于 hiveserver2的配置我一个都没有修改,一直是默认配置启动 hiveserver2。没想到的是默认配置是有认证机制的。
然后再写一点,在安装 pyhs2的时候还是遇到了点问题,其实还是要看官方文档的,我只是没看官方文档直接用 pip安装导致了这个问题。安装 pyhs2需要确定已经安装了几个依赖包。直接看在 github 上的 wiki 吧。哪个没安装就补上哪一个就好了。
To install pyhs2 on a clean CentOS 6.4 64-bit desktop....

(as root or with sudo)

get ez_setup.py from https://pypi.python.org/pypi/ez_setup
python ez_setup.py
easy_install pip
yum install gcc-c++
yum install cyrus-sasl-devel.x86_64
yum install python-devel.x86_64
pip install

写了这么多,其实是在啰嗦自己遇到的问题。下面写一下如何使用 python
连接 hive。
python 连接 hive 是基于 thrift 完成的。所以需要服务器端和客户端的配合才能使用。
在服务器端需要启动 hiveserver2 服务,启动方法有两种, 第二种方法只是对第一种方法的封装。
1. $HIVE_HOME/bin/hive --server hiveserver2
2. $HIVE_HOME/bin/hiveserver21212

默认情况下就是hiveserver2监听了10000端口。也可以通过修改 hive-site.xml 或者在启动的时候添加参数来实现修改默认配置。
另外一方面,在客户端需要安装 python 的依赖包 pyhs2。安装方法在上面也介绍了,基本上就是用 pip install pyhs2,如果安装不成功,安装上面提到的依赖包就可以了。
最后运行上面的示例代码就可以了,配置好 IP 地址、端口、数据库、表名称就可以用了,默认情况下认证信息不需要修改。
另外补充一点 fetch 函数执行速度是比较慢的,会把所有的查询结果返回来。可以看一下 pyhs2 的源码,查看一下还有哪些函数可以用。下图是 Curor 类的可以使用的函数。

一般 hive 表里的数据比较多,还是一条一条的读比较好,所以选择是哟功能 fetchone函数来处理数据。fetchone函数如果读取成功会返回列表,否则 None。可以把示例代码修改一下,把 fetch修改为:
count = 0
while (1):
row = cur.fetchone()
if (row is not None):
count += 1
print count, row
else:
print "it's over"

G. 想用python链接hive,环境都搭好的,如何链接大佬们

这个你正常连接就可以呀,他们有上面有连接的提示。

H. python连接hive,怎么安装thrifthive

HiveServer2的启动

启动HiveServer2

HiveServer2的启动十分简便:

$ $HIVE_HOME/bin/hiveserver2

或者

$ $HIVE_HOME/bin/hive --service hiveserver2

默认情况下,HiverServer2的Thrift监听端口是10000,其WEB UI端口是10002。可通过http://localhost:10002来查看HiveServer2的Web UI界面,这里显示了Hive的一些基本信息。如果Web界面不能查看,则说明HiveServer2没有成功运行。

使用beeline测试客户端连接

HiveServer2成功运行后,我们可以使用Hive提供的客户端工具beeline连接HiveServer2。

$ $HIVE_HOME/bin/beeline

beeline > !connect jdbc:hive2://localhost:10000

如果成功登录将出现如下的命令提示符,此时可以编写HQL语句。

0: jdbc:hive2://localhost:10000>

报错:User: xxx is not allowed to impersonate anonymous

在beeline使用!connect连接HiveServer2时可能会出现如下错误信息:

Caused by: org.apache.hadoop.ipc.RemoteException:
User: xxx is not allowed to impersonate anonymous

这里的xxx是我的操作系统用户名称。这个问题的解决方法是在hadoop的core-size.xml文件中添加xxx用户代理配置:

<property> <name>hadoop.proxyuser.xxx.groups</name> <value>*</value></property><property> <name>hadoop.proxyuser.xxx.hosts</name> <value>*</value></property>

重启HDFS后,再用beeline连接HiveServer2即可成功连接。

常用配置

HiveServer2的配置可以参考官方文档《Setting Up HiveServer2》

这里列举一些hive-site.xml的常用配置:

hive.server2.thrift.port:监听的TCP端口号。默认为10000。

hive.server2.thrift.bind.host:TCP接口的绑定主机。

hive.server2.authentication:身份验证方式。默认为NONE(使用 plain SASL),即不进行验证检查。可选项还有NOSASL, KERBEROS, LDAP, PAM and CUSTOM.

hive.server2.enable.doAs:是否以模拟身份执行查询处理。默认为true。

Python客户端连接HiveServer2

python中用于连接HiveServer2的客户端有3个:pyhs2,pyhive,impyla。官网的示例采用的是pyhs2,但pyhs2的官网已声明不再提供支持,建议使用impyla和pyhive。我们这里使用的是impyla。

impyla的安装

impyla必须的依赖包括:

  • six

  • bit_array

  • thriftpy(python2.x则是thrift)

  • 为了支持Hive还需要以下两个包:

  • sasl

  • thrift_sasl

  • 可在Python PI中下载impyla及其依赖包的源码。

    impyla示例

    以下是使用impyla连接HiveServer2的示例:

  • from impala.dbapi import connect


  • conn = connect(host='127.0.0.1', port=10000, database='default', auth_mechanism='PLAIN')


  • cur = conn.cursor()


  • cur.execute('SHOW DATABASES')print(cur.fetchall())


  • cur.execute('SHOW Tables')print(cur.fetchall())

I. python 连接hive后处理导出excel 问题

你的原始数据里面有空值,因此导致的错误,在写入或者读取之前填充以下缺失值,或者先对要写入或者读取的数据判断下是否为空,再做操作。
要不然你就加入try except,来主动跳过

J. 关于python利用thrift远程连接hive的问题

你起的thrift服务确定启好了吗 你先在服务器上看下IP端口是不是开了,而且IP不是Localhost的 如果好了远程肯定可以连上。

热点内容
微博上传原图 发布:2024-05-05 10:20:05 浏览:747
服务器换电脑需要什么东西 发布:2024-05-05 09:52:28 浏览:754
老算盘算法 发布:2024-05-05 09:43:10 浏览:840
ps存储显示不含通道 发布:2024-05-05 09:32:35 浏览:102
如何用安卓做一个识物界面表 发布:2024-05-05 09:29:28 浏览:98
如何编译linux内核模块 发布:2024-05-05 09:27:25 浏览:169
为什么apple验证无法连接服务器 发布:2024-05-05 09:20:35 浏览:661
linuxscript 发布:2024-05-05 09:19:49 浏览:324
asp编程词典 发布:2024-05-05 09:09:50 浏览:526
引流群控脚本 发布:2024-05-05 08:42:14 浏览:178