日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

在win10 python3用pyhive连接hive

發布時間:2023/12/31 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。