本文介绍用 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. python递归获取目录下指定文件

    获取一个目录下所有指定格式的文件是实际生产中常见需求. import os #递归获取一个目录下所有的指定格式的文件 def get_jsonfile(path,file_list): dir_lis ...

  2. Linux 多线程按照线程顺序打印字符

    #include <stdio.h> #include <pthread.h> #include <unistd.h> ; pthread_mutex_t mute ...

  3. linux下无root源码安装软件

    先进入源码文件夹下指定安装路径 ./configure --prefix=/public/home/ztu/usr/samtools 编译 make 安装 make install 写入环境变量 vi ...

  4. 【黑马JavaWeb】.1.2反射机制

    文章目录 反射:框架设计的灵魂 获取Class类对象的方式 学习视频:https://www.bilibili.com/video/av47886776?p=10 本来一万行的代码,使用框架以后简化到 ...

  5. selenium爬虫使用

    1. 网页的打开 from selenium import webdriver import time driver = webdriver.Chrome(executable_path=r" ...

  6. Android jni/ndk编程一:jni初级认识与实战体验

    Android平台很多地方都可以看到jni的身影,比如之前接触到一个投屏的项目,主要的代码是c/c++写的,然后通过Jni供Java层调用;另外,就拿Android系统中的Service来说,很多的S ...

  7. SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

    问题描述: 已经安装了android-sdk 和gradle环境,并配置了环境变量,如下所示: android环境 root@wangju-HP--G4:/home/wangju/Desktop/5i ...

  8. IDEA "Library source does not match the bytecode for class"问题

    问题描述 Jar包更新后,报错信息:"Library source does not match the bytecode for class" 经检查,发现Jar内容还是旧版本的 ...

  9. PJzhang:python基础入门的7个疗程-three

    猫宁!!! 参考链接:易灵微课-21天轻松掌握零基础python入门必修课-售价29元人民币 https://www.liaoxuefeng.com/wiki/1016959663602400 第七天 ...

  10. Linux常用命令详解(1)

    基础命令: ls man pwd cd mkdir echo touch cp mv rm rmdir cat more less head tail clear poweroff reboot 命令 ...