在win10 python3用pyhive连接hive
文章目录
- Hive部分
- Python部分
需要在两个部分分别进行设置
Hive部分
在虚拟机端安装使用hive时,初始是没有设置用户名和密码,
在我们使用pyhive连接hive时需要用到用户名和密码
此时,我们就要启用hive的自定义用户名密码验证
- 第一步:根据hiveServer2服务提供的接口,实现他
To implement custom authentication for HiveServer2, create a custom Authenticator class derived from the following interface:
从这段话看出来我们要实现一个接口:PasswdAuthenticationProvider (org.apache.hive.service.auth.PasswdAuthenticationProvider)我们来看看这个接口
发现有一个方法要实现,实现了这个接口就可以自定义验证用户名密码了
public interface PasswdAuthenticationProvider { /** * The Authenticate method is called by the HiveServer2 authentication layer * to authenticate users for their requests. * If a user is to be granted, return nothing/throw nothing. * When a user is to be disallowed, throw an appropriate {@link AuthenticationException}. * * For an example implementation, see {@link LdapAuthenticationProviderImpl}. * * @param user - The username received over the connection request * @param password - The password received over the connection request * @throws AuthenticationException - When a user is found to be * invalid by the implementation */ void Authenticate(String user, String password) throws AuthenticationException;
}
接口实现代码:
将代码打包为jar包,放在hive根目录的lib目录下
package org.apache.hadoop.hive.contrib.auth;import javax.security.sasl.AuthenticationException;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
import org.slf4j.Logger;public class CustomPasswdAuthenticator implements org.apache.hive.service.auth.PasswdAuthenticationProvider{private Logger LOG = org.slf4j.LoggerFactory.getLogger(CustomPasswdAuthenticator.class);private static final String HIVE_JDBC_PASSWD_AUTH_PREFIX="hive.jdbc_passwd.auth.%s";private Configuration conf=null;@Overridepublic void Authenticate(String userName, String passwd) throws AuthenticationException { LOG.info("user: "+userName+" try login."); String passwdConf = getConf().get(String.format(HIVE_JDBC_PASSWD_AUTH_PREFIX, userName)); if(passwdConf==null){ String message = "user's ACL configration is not found. user:"+userName; LOG.info(message); throw new AuthenticationException(message); } if(!passwd.equals(passwdConf)){ String message = "user name and password is mismatch. user:"+userName; throw new AuthenticationException(message); } } public Configuration getConf() { if(conf==null){ this.conf=new Configuration(new HiveConf()); } return conf; } public void setConf(Configuration conf) { this.conf=conf; }}
- 第二步:修改hive-site.xml
<!--自定义远程连接用户名和密码-->
<property>
<name>hive.server2.authentication</name>
<value>CUSTOM</value><!--默认为none,修改成CUSTOM-->
</property><!--指定解析jar包-->
<property>
<name>hive.server2.custom.authentication.class</name>
<value>org.apache.hadoop.hive.contrib.auth.CustomPasswdAuthenticator</value>
</property> <!--设置用户名和密码-->
<property><name>hive.jdbc_passwd.auth.hive</name><!--用户名为最后一个:hive--><value>123456</value><!--密码-->
</property>
该部分参考博客来源:
https://www.iteye.com/blog/liyonghui160com-2187838
https://blog.csdn.net/alan_liuyue/article/details/90299035
Python部分
- 需要先安装几个包
pip install sasl
pip install thrift
pip install thrift-sasl
pip install PyHive
在安装sasl时会遇到安装失败的问题:
解决方法:
到sasl下载地址根据自身的python版本(3.6就下载cp36,3.7就下载cp37)下载whl,放在项目中,再pip,就可成功下载。
- 开始连接测试
代码:
from pyhive import hivehost='leader'//此处使用了ip映射,若无设置映射,填入ip即可
username='hive'
password='123456'
port=10000
data_base_name='report'conn = hive.Connection(host=host,port=port,auth="CUSTOM",database=data_base_name,username=username,password=password)
在连接时,我遇到的错误信息:
Could not start SASL: b'Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callback: 2'
解决方法:
用管理员身份打开cmd(进入c盘>Windows>system32)找到cmd右键以管理员身份运行,输入:
解决原理参考 Windows下pyhive无法使用的解决方案
FOR /F "usebackq delims=" %A IN (`python -c "from importlib import util;import os;print(os.path.join(os.path.dirname(util.find_spec('sasl').origin),'sasl2'))"`) DO (REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Carnegie Mellon\Project Cyrus\SASL Library" /v SearchPath /t REG_SZ /d "%A"
)
即可连接成功
- 使用pandas读取数据(测试一下读取数据)
from pyhive import hive
import pandas as pdhost='leader'//此处使用了ip映射,若无设置映射,填入ip即可
username='hive'
password='123456'
port=10000
data_base_name='report'conn = hive.Connection(host=host,port=port,auth="CUSTOM",database=data_base_name,username=username,password=password)sql_order = 'select * from u_data limit 10'
df = pd.read_sql(sql_order, conn)conn.close
该部分参考文章:
https://blog.csdn.net/weixin_43142260/article/details/115198097
在win10 python3用pyhive连接hive相关推荐
- python3通过pyhive连接hive
python连接hive有两种方式,pyhive和impyla,impyla连接较为方便,pyhive在linux上很方便,在windows上较麻烦,本文主要介绍pyhive的安装方法 1.从官网下载 ...
- 使用pyhive:连接hive(python3+)
1.安装相关依赖 sudo yum install cyrus-sasl-devel -y sudo yum install gcc-c++ -ypip3 install sasl pip3 inst ...
- 大数据学习-python通过Pyhive连接hive数据库
1.hbase和hive结合 (1)hbase建表添加数据 #test是表名,name是列族 #hbase可以一个列族里边多个字段 create 'test','name'#添加数据 put 'tes ...
- pycharm使用pyhive连接hive
导入pyhive #导入pyhive相关模块from pyhive import hiveif __name__ == '__main__':conn =hive.Connection(host=&q ...
- Pyhive连接hive时出错:thrift.transport.TTransport.TTransportException: Could not start SASL
项目场景: 安装Pyhive包及其依赖:sasl,thrift,thrift-sasl, PyHive pip install sasl pip install t ...
- python win10 连接hive_使用win10+python3.5+impyla 连接大数据平台hive表的步骤与问题解决...
环境硬件配置及Hadoop,Hive版本 一.安装步骤 pip install pure-sasl Downloading https://pypi.tuna.tsinghua.edu.cn/pack ...
- windows平台python3使用impyla连接hive问题汇总
①背景 windows+python3+连接已有hive集群且已经开启hiveserver2 服务 linux下用pyhive可能会方便一些,windows下pyhive需要在集群和连接代码中同时指定 ...
- pyhive 连接 Hive 时错误
一.User: xx is not allowed to impersonate xxx' 解决办法:修改 core-site.xml 文件,加入下面的内容后重启 hadoop. <proper ...
- pyhive连接kerberos认证的hive
目前,大多数的大数据集群之前是采用kerberos认证的,公司的大数据集群目前正在升级,认证方式由LDAP认证方式改变成为kerberos进行认证: 以下介绍如何将LDAP认证连接升级至KERBERO ...
- Python使用pyhive,impala,JayDeBeApi连接Hive(含Kerberos)
环境 pip 22.1.2,python 3.6.4 安装依赖 yum install cyrus-sasl-devel -y yum install gcc-c++ -y pip3 install ...
最新文章
- Microbiome | 中科院张惠明团队揭示RNA介导的DNA甲基化影响植物根部微生物群落!...
- 专访丨李开复:AI时代下努力工作未必能买车买房
- linux 初始化工作进程 systemd简介
- 安装centos系统时,修改默认网卡名
- 如何比较 Java 的字符串
- c语言delay_利用C语言编程单片机,制作可以根据环境光照调整亮度的智能灯
- java pgsql connection 是否可同时执行sql_Java教程:解读JDBC是什么
- L3-006 迎风一刀斩 (30 分)-PAT 团体程序设计天梯赛 GPLT
- 2016年开源软件排名TOP50,最受IT公司欢迎的50款开源软件
- java文件读取的几个操作-2
- LAMP架构简介与配置
- 苹果电脑python编译器_Mac版-python环境配置(二):编译器pycharm下载安装
- HTML顶部状态栏更改背景,适合做导航栏背景的图片
- 用计算机计算数学公式,Formula Calculator公式计算器
- 语音机器人空号识别介绍
- switch游戏服务器设置
- 声卡驱动win7安装失败解决方法
- 腾讯微信客服电话号码是多少呢/腾讯微信人工服务热线
- 服务器和电脑主机的区别
- Xcon2014 Geekpwn2014