0039-如何使用Python Impyla客户端连接Hive和Impala

温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。

1.文档编写目的



继上一章讲述如何在CDH集群安装Anaconda&搭建Python私有源后,本章节主要讲述如何使用Pyton Impyla客户端连接CDH集群的HiveServer2和Impala Daemon,并进行SQL操作。

  • 内容概述

1.依赖包安装

2.代码编写

3.代码测试

  • 测试环境

1.CM和CDH版本为5.11.2

2.RedHat7.2

  • 前置条件

1.CDH集群环境正常运行

2.Anaconda已安装并配置环境变量

3.pip工具能够正常安装Python包

4.Python版本2.6+ or 3.3+

5.非安全集群环境

2.Impyla依赖包安装



Impyla所依赖的Python包

  • six
  • bit_array
  • thrift (on Python 2.x) orthriftpy (on Python 3.x)
  • thrift_sasl
  • sasl

1.首先安装Impyla依赖的Python包

[[email protected] ~]# pip install bit_array
[[email protected] ~]# pip install thrift==0.9.3
[[email protected] ~]# pip install six
[[email protected] ~]# pip install thrift_sasl
[[email protected] ~]# pip install sasl

注意:thrift的版本必须使用0.9.3,默认安装的为0.10.0版本,需要卸载后重新安装0.9.3版本,卸载命令pip uninstall thrift

2.安装Impyla包

impyla版本,默认安装的是0.14.0,需要将卸载后安装0.13.8版本

 [[email protected] ec2-user]# pip install impyla==0.13.8
Collecting impyla
  Downloading impyla-0.14.0.tar.gz (151kB)
    100% |████████████████████████████████| 153kB 1.0MB/s
Requirement already satisfied: six in /opt/cloudera/parcels/Anaconda-4.2.0/lib/python2.7/site-packages (from impyla)
Requirement already satisfied: bitarray in /opt/cloudera/parcels/Anaconda-4.2.0/lib/python2.7/site-packages (from impyla)
Requirement already satisfied: thrift in /opt/cloudera/parcels/Anaconda-4.2.0/lib/python2.7/site-packages (from impyla)
Building wheels for collected packages: impyla
  Running setup.py bdist_wheel for impyla ... done
  Stored in directory: /root/.cache/pip/wheels/96/fa/d8/40e676f3cead7ec45f20ac43eb373edc471348ac5cb485d6f5
Successfully built impyla
Installing collected packages: impyla
Successfully installed impyla-0.14.0

3.编写Python代码



Python连接Hive(HiveTest.py)

from impala.dbapi importconnect

conn = connect(host=‘ip-172-31-21-45.ap-southeast-1.compute.internal‘,port=10000,database=‘default‘,auth_mechan

ism=‘PLAIN‘)

print(conn)

cursor = conn.cursor()

cursor.execute(‘show databases‘)

print cursor.description # prints the result set‘s schema

results = cursor.fetchall()

print(results)

cursor.execute(‘SELECT * FROM test limit 10‘)

print cursor.description # prints the result set‘s schema

results = cursor.fetchall()

print(results)

Python连接Impala(ImpalaTest.py)

from impala.dbapi importconnect

conn = connect(host=‘ip-172-31-26-80.ap-southeast-1.compute.internal‘,port=21050)

print(conn)

cursor = conn.cursor()

cursor.execute(‘show databases‘)

print cursor.description # prints the result set‘s schema

results = cursor.fetchall()

print(results)

cursor.execute(‘SELECT * FROM test limit 10‘)

print cursor.description # prints the result set‘s schema

results = cursor.fetchall()

print(results)

4.测试代码



在shell命令行执行Python代码测试

1.测试连接Hive

[email protected]_ec2-user# python HiveTest.py

<impala.hiveserver2.HiveServer2Connection_object at 0x7f66eee00250>_

(‘database_name‘, ‘STRING‘, None, None, None, None, None)

(‘default‘,)

(‘test.s1‘, ‘STRING‘,None, None, None, None, None), (‘test.s2‘, ‘STRING‘, None, None, None, None, None)

(‘name1‘, ‘age1‘), (‘name2‘, ‘age2‘), (‘name3‘, ‘age3‘), (‘name4‘, ‘age4‘), (‘name5‘, ‘age5‘), (‘name6‘, ‘age6‘), (‘name7‘, ‘age7‘), (‘name8‘, ‘age8‘), (‘name9‘, ‘age9‘), (‘name10‘, ‘age10‘)

[[email protected] ec2-user]#

2.测试连接Impala

[email protected]_ec2-user# python ImpalaTest.py

<impala.hiveserver2.HiveServer2Connection_object at 0x7f7e1f2cfad0>_

(‘name‘, ‘STRING‘, None, None, None, None, None), (‘comment‘, ‘STRING‘, None, None, None, None, None)

(‘_impala_builtins‘, ‘Systemdatabase for Impala builtin functions‘), (‘default‘, ‘Default Hive database‘)

(‘s1‘, ‘STRING‘, None, None, None,None, None), (‘s2‘, ‘STRING‘, None, None, None,None, None)

(‘name1‘, ‘age1‘), (‘name2‘, ‘age2‘), (‘name3‘, ‘age3‘), (‘name4‘, ‘age4‘), (‘name5‘, ‘age5‘), (‘name6‘, ‘age6‘), (‘name7‘, ‘age7‘), (‘name8‘, ‘age8‘), (‘name9‘, ‘age9‘), (‘name10‘, ‘age10‘)

[[email protected] ec2-user]#

5.常见问题



1.错误一

building ‘sasl.saslwrapper‘ extension
    creating build/temp.linux-x86_64-2.7
    creating build/temp.linux-x86_64-2.7/sasl
    gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Isasl -I/opt/cloudera/parcels/Anaconda/include/python2.7 -c sasl/saslwrapper.cpp -o build/temp.linux-x86_64-2.7/sasl/saslwrapper.o
    unable to execute ‘gcc‘: No such file or directory
    error: command ‘gcc‘ failed with exit status 1

    ----------------------------------------
Command "/opt/cloudera/parcels/Anaconda/bin/python -u -c "import setuptools, tokenize;__file__=‘/tmp/pip-build-kD6tvP/sasl/setup.py‘;f=getattr(tokenize, ‘open‘, open)(__file__);code=f.read().replace(‘\r\n‘, ‘\n‘);f.close();exec(compile(code, __file__, ‘exec‘))" install --record /tmp/pip-WJFNeG-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-kD6tvP/sasl/

解决方法:

[[email protected] ec2-user]# yum -y install gcc
[[email protected] ec2-user]# yum install gcc-c++ 

2.错误二

gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Isasl -I/opt/cloudera/parcels/Anaconda/include/python2.7 -c sasl/saslwrapper.cpp -o build/temp.linux-x86_64-2.7/sasl/saslwrapper.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
In file included from sasl/saslwrapper.cpp:254:0:
sasl/saslwrapper.h:22:23: fatal error: sasl/sasl.h: No such file or directory
#include <sasl/sasl.h>
                   ^
compilation terminated.
error: command ‘gcc‘ failed with exit status 1

解决方法:

[[email protected] ec2-user]# yum -y install python-devel.x86_64 cyrus-sasl-devel.x86_64

醉酒鞭名马,少年多浮夸! 岭南浣溪沙,呕吐酒肆下!挚友不肯放,数据玩的花!
温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。



推荐关注Hadoop实操,第一时间,分享更多Hadoop干货,欢迎转发和分享。


原创文章,欢迎转载,转载请注明:转载自微信公众号Hadoop实操

原文地址:http://blog.51cto.com/14049791/2320848

时间: 2024-07-29 17:32:01

0039-如何使用Python Impyla客户端连接Hive和Impala的相关文章

客户端连接hive

g公司地址23.100 登陆23.100 su unimas cd cd apache-hive-1.2.1-bin/ bin/beeline beeline> !connect jdbc:hive2://10.68.23.100:10000 show databases; show tables;

python TCP多客户端连接

Python TCP服务端代码: # coding=utf-8 # !/usr/bin/env python from socket import * from time import ctime import threading import time HOST = ''   #主机地址 PORT = 3046  #端口 BUFSIZ = 1024  #缓冲区大小 ADDR = (HOST, PORT)  #地址及端口 tcpSerSock = socket(AF_INET, SOCK_STR

centos pptp 客户端连接vpn

vpn翻墙现在很普遍,无论是个人还是公司,都会用的到.对于个人实现很容易,直接购买一个云主机,现在的云主机都有这样的服务,购买后直接就可以使用.有的vpn对使用也有一定数量限制,如:只允许多少个客户端连接,但对于公司来说,根本无法满足. 所以经过摸索总于有了答案.通过跳转的方法实现,在云主机上搭建pptp vpn,再在公司内部找一台机器搭建shadowsocks,通过它可以实现智能翻墙,很方便,一个账号多人可以使用,vpn账号也很安全,在公司外是无法使用的.是不是很棒. 下面介绍公司内网机器vp

PL/SQL在win7/win8 x64位下使用客户端连接oracle

新做了win8 x64位的系统.需要使用plsql连接oracle数据库.之前的机器装有64位的oracle11g,plsql可以直接找到oracle_home连接到数据库. 这次不想在本地安装数据库,因此只装了一个32位的数据库客户端.配置环境变量时遇到一些小问题,在此记录下来. 1.下载oracle客户端,然后解压到任意目录下 2.安装好plsql,然后配置plsql的oracle_home和oci库的地址 plsql的preference设置 ORACLE_HOME:F:\oracle\p

使用windows server2012时FileZilla客户端连接时报150 Opening data channel for directory listing of &quot;/&quot; 响应:425 Can&#39;t open data connection

425 Can't open data connection 和 读取目录列表失败 问题解决 这个问题主要是由于使用Passive Mode模式造成的,解决这个问题很简单: 1.在ftp服务软件中设置指定端口地址范围,允许Passive Mode使用,比如60000-60020 2.然后在ftp服务器的系统防火墙上打开这些tcp端口,比如是60000-60020,如果使用windows自带的防火墙,就一条一条的增加,20行有点麻烦,但是可以解决. 如果ftp用户较多,可以扩大端口范围. 3.如果

Centos6.7+openvpn配置Linux客户端连接,基于证书访问(三)

Linux 做为客户端连接 openvpn 服务,同样需要安装 openvpn 的软件及相关软件. 和服务器端的配置一样,先做时间同步,并添加定时任务 /usr/sbin/ntpdate time.windows.com echo '#time sync' >> /var/spool/cron/root echo '*/5 * * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1' >> /var/spool

配置ORACLE 客户端连接到数据库

--================================= -- 配置ORACLE 客户端连接到数据库 --================================= Oracle 客户端连接到Oracle 数据库服务器貌似不同于SQL serve中的网络配置,其实不然,只不过所有的SQL server 运行于Windows平台,故很多配置直接集成到了操作系统之中.所以无需配置客户端即可连接到服务器.Oracle 客户端连接到数据库依赖于Oracle Net.Oracle提供

Redis 客户端连接

Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接,当一个连接建立后,Redis 内部会进行以下一些操作: 首先,客户端 socket 会被设置为非阻塞模式,因为 Redis 在网络事件处理上采用的是非阻塞多路复用模型. 然后为这个 socket 设置 TCP_NODELAY 属性,禁用 Nagle 算法 然后创建一个可读的文件事件用于监听这个客户端 socket 的数据发送 最大连接数 在 Redis2.4 中,最大连接数是被直接硬编码在代码里面的

Ruby操作MongoDB(进阶)-创建数据库客户端连接

在Ruby的MongoDB2.4.3驱动版本中,通过创建一个Mongo::Client对象来构建一个Ruby的数据库连接.Mongo::Client构造器提供两种构造方式:一是通过提供主机列表和一些可选参数,另外还有通过一个连接URI.创建好的数据库连接默认连接到admin数据库. 1.使用Mongo::Client创建数据库客户端连接 1.1. 单服务器模式创建数据库连接 在单服务器模式下创建数据库连接,只需提供一个主机连接参数.另外,还可以通过消除自动发现步骤强制将集群拓扑转换为单机模式.可