欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

在win10 python3用pyhive连接hive

发布时间:2023/12/31 python 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 在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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。