TEL操作使用函数:
截取出一个字段中的数字部分
select taskId,regexp_extract(regCapital,'([a-zA-Z0-9]+)',1) from test001;
使用函数过滤将*号替换(hive中没有replace函数)
select regexp_replace(businessscope,'*','') from test003;
环境准备:
Anaconda整合python(用的3.7版本)
有两种方式:
基于pyhive连接hive。
基于impyla连接hive。
基于pyhive安装依赖包(注意:sasl没有下载成功开始是缺少c++包 自己在网上下载的)
sasl包下载
pip install sasl
pip install thrift
pip install thrift-sasl
pip install PyHive
pyhive连接代码:
from pyhive import hive
conn = hive.Connection(host='192.168.2.200', port=10000, username='www', database='xcc_company_name', auth="NOSASL")
cursor = conn.cursor()
cursor.execute('select businessscope from ods_basic_infor limit 20')
报错整理:
执行上面代码会报出如下错误:
Traceback (most recent call last):
File "D:/satori/1.py", line 14, in <module>
conn = connect(host='47.94.174.89', port=10000, database="default", auth_mechanism='PLAIN')
File "C:\python38\lib\site-packages\impala\dbapi.py", line 144, in connect
service = hs2.connect(host=host, port=port,
File "C:\python38\lib\site-packages\impala\hiveserver2.py", line 825, in connect
transport.open()
File "C:\python38\lib\site-packages\thrift_sasl\__init__.py", line 75, in open
self._send_message(self.START, chosen_mech)
File "C:\python38\lib\site-packages\thrift_sasl\__init__.py", line 94, in _send_message
self._trans.write(header + body)
TypeError: can't concat str to bytes
根据提示信息:我们进入thrift_sasl模块下的__init__.py中,找到85行
值得一提的是,python中的缩进应该是四个空格,但是thrift_sasl用的两个空格,不过不影响
def _send_message(self, status, body):
header = struct.pack(">BI", status, len(body))
self._trans.write(header + body)
self._trans.flush()
# 我们看到报错的原因是header + body、因为字符串和字节无法相加
# 将上面代码改成如下
def _send_message(self, status, body):
header = struct.pack(">BI", status, len(body))
if isinstance(body, str):
body = bytes(body, encoding="utf-8")
self._trans.write(header + body)
self._trans.flush()
执行的时候会报出如下错误
thrift.transport.TTransport.TTransportException: Could not start SASL: b'Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callback: 2'
在linux上可以通过yum install cyrus-sasl-plain cyrus-sasl-devel cyrus-sasl-gssapi解决
这是sasl不支持Windows机型所导致的,我们可以将auth改成NOSASL
from pyhive import hive
conn = hive.Connection(host='47.94.174.89',
port=10000,
database='default',
auth="NOSASL")
cursor = conn.cursor()
cursor.execute('select * from girls')
print(cursor.fetchall())
但是这样又会报出如下错误
thrift.transport.TTransport.TTransportException: TSocket read 0 bytes
需要同时要求,hive的hive-site.xml中增加如下配置
<property>
<name>hive.server2.authentication</name>
<value>NOSASL</value>
</property>
将hive中的认证给修改掉,所以才说Windows上使用pyhive的局限性非常大,因为它要求服务器上的hive不能使用认证,但是服务器上的hive一般都是使用Kerberos认证的。
更改Kerberos认证之后对hiveservice2连接的更改
进入beeline之后使用它连接
!connect jdbc:hive2://127.0.0.1:10000/default/;auth=noSasl
在hive-site.xml中添加(对于spark整合hive还没有通)
更改Kerberos认证之后对kettle连接hive2的更改
需要更改配置core-site.xml
kettle的设置修改如下:需要在数据库的名称后面添加;auth=noSasl
总结pyhive的安装参考链接
https://www.cnblogs.com/traditional/p/12534719.html