beeline 连接hive数据库 2021-02-24

beeline 连接hive数据库 2021-02-24,第1张

打开beeline:

beeline

交互式登录

命令登录

beeline -u jdbc:hive2://127.0.0.1:10000 -n lenmom -p 123456

[Hadoop@master ~]$ cd $HIVE_HOME--进入Hive目录

[hadoop@master hive]$ bin/hive --进入hive环境

hive>show databases 显示已有数据库

hive>use test 使用数据库

hive>select * from test_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_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")1234567891011121314151617181920212223242526272812345678910111213141516171819202122232425262728

或者是这样的:

#!/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)

12345678910111213141516171819202122232425262728293031323334351234567891011121314151617181920212223242526272829303132333435

但是都解决不了问题,从 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 i123456789101112131415161718192021123456789101112131415161718192021

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

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

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


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存