python写数据到hive_Python数据篇之Pyhive

Python提供了操作hive数据仓库的API接口,一下记录Windows和Linux系统环境的环境配置和语法。目前笔者在Windows上不能成功运行程序,依然写下Windows平台配置。

1,安装三方库:

Windows:

sasl/thrift/thriftsasl/pyhive,其中thrift/thrift_sasl/pyhive可直接通过pip或者conda直接安装;sasl需要到如下网址下载whl文件安装:https://www.lfd.uci.edu/~gohlke/pythonlibs/,搜索下载对应版本的xxx.whl文件到本地,通过pip install xxx.whl来安装。

Linux:

sasl/thrift/thriftsasl/pyhive,直接通过pip即可安装。

2,代码实例:

from pyhive import hive

import sasl

import thrift

import thrift_sasl

import pysnooper

@pysnooper.snoop()

def read_hive(host, port, username, password, auth):

conn = hive.Connection(host=host, port=port, username=username, password=password, auth=auth)

cursor = conn.cursor()

query_sql = 'select * from user_table limit 10'

cursor.execute(query_sql)

result = cursor.fetchall()

cursor.close()

conn.close()

for res in result:

print(res)

return result

实例化Connection传参,auth为‘CUSTOM’,如果运行结果报错:thrift.transport.TTransport.TTransportException: TSocket read 0 bytes,则在hive集群机器修改配置文件:

打开hive-site.cml,增加如下内容:

hive.server2.authentication

NOSASL

3,以上步骤执行下来,Linux系统代码运行OK,Windows系统代码运行依然报错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值