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()