您的位置:首页 > 编程语言 > Python开发

python连接hiveserver2

2017-10-25 13:11 603 查看

1.开启metastore和hiveserver2服务

$hive --service metastore &

$hive --service hiveserver2 &

2.beeline调试,远程连接到HiveServer2

$cd /home/hdfs/project/hive-2.1.1/bin

$./beeline

beeline> !connect jdbc:hive2://192.168.1.58:10000 hdfs hyxy123

报错: User: hdfs is not allowed to impersonate root (state=08S01,code=0)

3.停止集群和hive,修改配置,同步配置,重启集群和hive

$stop-all.sh

kill掉hive的两个进程

修改hadoop 配置文件 etc/hadoop/core-site.xml,加入如下配置项

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

Hadoop.proxyuser.root.hosts配置项名称中root部分为报错User:* 中的用户名部分

例如User: hdfs is not allowed to impersonate anonymous则需要将xml变更为如下格式

<property>

<name>hadoop.proxyuser.hdfs.hosts</name>

<value>*</value>

</property>

<property>

<name>hadoop.proxyuser.hdfs.groups</name>

<value>*</value>

</property>

重启hadoop和hive

$start-all.sh

$hive --service metastore &

$hive --service hiveserver2 &

4.beeline再次调试

$cd /home/hdfs/project/hive-2.1.1/bin

$./beeline

beeline> !connect jdbc:hive2://192.168.1.58:10000 hdfs hyxy123

0: jdbc:hive2://192.168.1.58:10000> show tables;

5.python连接hiveserver2代码

# -*- coding: utf-8 -*-

import pyhs2
import sys

default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)

class HiveClient:
def __init__(self,db_host,user,password,database,port=10008,authMechanism="PLAIN"):
"""
create connection to hive server2
"""
self.conn = pyhs2.connect(host=db_host,
port=port,
authMechanism=authMechanism,
user=user,
password=password,
database=database,)

def query(self, sql):

"""
query
"""
with self.conn.cursor() as cursor:
cursor.execute(sql)
return cursor.fetch()

def close(self):
"""
close connection
"""
self.conn.close()
if __name__ == '__main__':
hive_client = HiveClient(db_host='127.0.0.1',port=10000,user='hdfs',password='hyxy123',database='default', authMechanism='PLAIN')
sql = "show tables"
result = hive_client.query(sql)
print result
hive_client.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: