本文介绍用 python 远程连接 hive,此时需要 hive 启动 hiveserver2 服务

windows 下报如下错误

thrift.transport.TTransport.TTransportException: Could not start SASL: Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callback: 2

不好玩,还是 linux 吧

安装依赖包

pip install sasl
pip install thrift
pip install thrift-sasl
pip install PyHive

最麻烦的是安装 sasl,出现异常参考下面的异常记录

python 操作 hive

官网方法-pyhs2

import pyhs2

## 连接 hve 和 数据库
with pyhs2.connect(host='192.168.10.10',
port=10000,
authMechanism="PLAIN",
user='postgres',
password='postgres',
database='hive1') as conn:
with conn.cursor() as cur:
#Show databases
print cur.getDatabases() #Execute query
cur.execute("show tables") # 注意没有 分号 #Return column info from query
print cur.getSchema() cur.execute('select * from hive_01 limit 3')
#Fetch table results
for i in cur.fetch():
print i

其他方法-pyhive

from pyhive import hive

conn = hive.Connection(host='192.168.10.10', port=10000, username="postgres", password='postgres', auth='CUSTOM')
cursor = conn.cursor()
cursor.execute('SHOW DATABASES')
# 打印结果
for result in cursor.fetchall():
print(result)

auth

其他方法-impyla

其他依赖

pip install six
pip install bit_array
pip install thriftpy

demo

from impala.dbapi import connect
conn = connect(host ='****',port = ****)
cursor = conn.cursor()
cursor.execute('SELECT * FROM mytable LIMIT 100')
print cursor.description # 打印结果集的schema
results = cursor.fetchall()

异常记录

安装 sasl 报错如下

gcc: error trying to exec 'cc1plus': execvp: No such file or directory
error: command 'gcc' failed with exit status 1

解决方案

Ubuntu 系统的话可能需要先装好 libsasl2-dev

apt-get install libsasl2-dev

CentOS 系统需要预先装好 python-devel 和 cyrus-sasl-devel

yum install gcc-c++ python-devel.x86_64 cyrus-sasl-devel.x86_64

参考资料:

https://cwiki.apache.org/confluence/display/Hive/Setting+Up+HiveServer2#SettingUpHiveServer2-PythonClientDriver  官网

https://www.lfd.uci.edu/~gohlke/pythonlibs/#sasl  第三方包下载地址

https://www.jianshu.com/p/a0ff0a4495a1  Python 连接hive(Linux)

https://www.jianshu.com/p/99581dce8309  本文后面有编译python脚本

https://blog.csdn.net/clany888/article/details/82989068  pyhive  and impyla 两种方式

https://www.cnblogs.com/chushiyaoyue/p/5628882.html  pyhs2 方式,写的代码多

http://www.zhangdongshengtech.com/article-detials/267  pyhive

https://www.cnblogs.com/free-easy0000/p/9638982.html  异常记录

https://www.jianshu.com/p/9dd3a741a8ba  也总结了几种方式

https://www.360kuai.com/pc/9026a62a8775d368f?cota=4&kuai_so=1&tj_url=so_rec&sign=360_57c3bbd1&refer_scene=so_1

https://blog.csdn.net/bigdataf/article/details/78479331  Windows pyhs2下模块安装,没试过

Hive 教程(九)-python with hive的更多相关文章

  1. HIVE教程

    完整PDF下载:<HIVE简明教程> 前言 Hive是对于数据仓库进行管理和分析的工具.但是不要被“数据仓库”这个词所吓倒,数据仓库是很复杂的东西,但是如果你会SQL,就会发现Hive是那 ...

  2. 【Hadoop/Hive/mapreduce】系列之使用union all 命令之后如何对hive表格使用python进行去重

    业务场景大概是这样的,这里由两个hive表格,tableA 和 tableB, 格式内容都是这样的: uid cate1 cate2 在hive QL中,我们知道union有着自动去重的功能,但是那是 ...

  3. python 连接 hive

    1.python连接hive,其实是连接hiveserver,连接的包的impyla impyla安装: error: cc1plus 没有文件或目录 需要安装gcc 和g++,并且版本保持一致 er ...

  4. Hive 教程(十)-UDF

    hive 虽然自带了很多函数,但是毕竟有限,无法满足所有业务场景,用户可以自定义函数来实现特定功能 UDF user define function,用户自定义函数 可以分为 3 类 UDF:一进一出 ...

  5. Hive 教程(二)-认知hive

    在大数据领域,hive 的位置非常重要,排名前三的大数据工具为 spark.hive.kafka 什么是hive 在大数据领域有 3 种需求场景:传输.存储.计算: hive 是一个处理海量的结构化数 ...

  6. python 读取hive数据

    话不多说,直接上代码 from pyhive import hivedef pyhive(hql): conn = hive.Connection(host='HiveServer2 host', p ...

  7. Hive(九)Hive 执行过程实例分析

    一.Hive 执行过程概述 1.概述 (1) Hive 将 HQL 转换成一组操作符(Operator),比如 GroupByOperator, JoinOperator 等 (2)操作符 Opera ...

  8. Hive扩展功能(九)--Hive的行级更新操作(Update)

    软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这三部机, 每部主机的用户名都为centos ...

  9. Hive教程(1)

    1. 介绍 Apache Hive可以使用SQL来读,写,管理分布式存储的大数据集,结构可以投射到已经存储的数据上,命令行工具和JDBC驱动可以让用户连接到Hive. 2. 安装和配置 你可以下载Hi ...

随机推荐

  1. [Deep Learning] GELU (Gaussian Error Linerar Units)

    (转载请注明出处哦~) 参考链接: 1. 误差函数的wiki百科:https://zh.wikipedia.org/wiki/%E8%AF%AF%E5%B7%AE%E5%87%BD%E6%95%B0 ...

  2. 遍历 ArrayList与Map集合

    import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * 遍历 ArrayList * @ ...

  3. leetcode题目15.三数之和(中等)

    题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...

  4. Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.

    2019-07-24 11:09:15.231+08:00 LISA.Common.Utilities.LogUtil - System.ObjectDisposedException: Instan ...

  5. python 构建自己的log系统

    python的logging模块提供了记录程序运行情况的日志功能,类似于Apache的log4j,很好很强大,这里我们就来看一下Python中内置的日志模块logging用法详解 logging模块简 ...

  6. Android studio 下 NDK Jni 开发 简单例子

    1. 创建一个新的工程 2. 创建一个新的类 JniText.java  点击Build--Make Project  后     选中工程 点击F4键 sdk location 中 Android ...

  7. Simple Cel Shading 钟馗

    Made with Unity Unannouced project Character Art by Chris P

  8. javascript之for-in语句

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Android studio2 中的 SDK Manager的使用-------Android SDK 的安装与更新(Install missing platform(s) and sync project 编译错误解决)

    最近在编写Android程序,其中有一个问题就是对旧应用的导入,此时往往你的Android SDK中并没有老版本的Android SDK, 此时往往会提示你出现错误 Install missing p ...

  10. java文件分片上传,断点续传

    百度的webUploader的前端开源插件实现的大文件分片上传功能 前端部分 前端页面代码如下,只需要修改自己的文件上传地址接口地址: <!DOCTYPE html> <html l ...