python操作数据库之Hive

2019-12-30  本文已影响0人  雪飘千里

集群环境:CDH5.8
python3版本:3.6.8
pip3版本:18.1

# 导包
from pyhive import hive
import pandas as pd
from pandas.tseries.offsets import *
import math

# 打开数据库连接
conn = hive.Connection(host='x.x.x.x', port=10000, username='admin',database='hello_world')
# 使用cursor()方法获取操作游标 
cursor = conn.cursor()
# sql语句
sql = "select date,product_id,number_sum from hello_world.product where date >='{}' ".format(day_before_30_str)

# 执行sql
cursor.execute(sql)

# 查询结果转换为DataFrame
columns = [col[0] for col in cursor.description]
result = [dict(zip(columns, row)) for row in cursor.fetchall()]
data = pd.DataFrame(result)

备注:

  # python版本是3.6.8,pip3 的版本是18.1
  pip3 install sasl thrift_sasl thrift pyhive pandas pymysql

如果安装sasl如果失败了,报gcc错误,如下图,则yum更新gcc,然后再安装

image.png
#安装sasl如果失败了,报gcc错误,则yum更新gcc
yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64

如果更新后还是安装失败,解决办法见pip3安装和卸载以及常用命令中4.1 安装sasl包失败处理

上一篇 下一篇

猜你喜欢

热点阅读