Skip to content

Instantly share code, notes, and snippets.

@YoshihitoAso
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YoshihitoAso/9632814 to your computer and use it in GitHub Desktop.
Save YoshihitoAso/9632814 to your computer and use it in GitHub Desktop.
[Hive][HiveServer2][Python]pythonからhiveserver2に接続する方法

PythonからHiveServer2に接続する方法

HiveServer2について

HiveServer2 http://www.slideshare.net/schubertzhang/hiveserver2

HiveServer2 Clients - Apache Hive - Apache Software Foundation https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients

HiveServer2の起動

CDH4をインストール済みであれば、HiveServer2の起動は以下のようにして行える。

$ service hive-server2 start

起動状態の確認

# jps
:
:
2901 RunJar ←HiveServer2のプロセス
:

PythonからHiveServer2に接続(pyhs2)

PythonからHiveServer2に接続して操作するためには、HiveSever2用クライアントドライバをインストールする必要がある。

オフィシャルドキュメント( https://cwiki.apache.org/confluence/display/Hive/Setting+Up+HiveServer2#SettingUpHiveServer2-PythonClientDriver )にそって、ここではpyhs2を利用することにする。

pyhs2のインストールはpipでOK。

$ pip install pyhs2

HiveServer2を起動した状態で以下の様なサンプルプログラム(thrift_sample.py)を動かしてみる。

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import pyhs2

conn = pyhs2.connect(host='localhost', 
					port=10000,
					authMechanism="PLAIN", 
					user='root', 
					password='test', 
					database='default')
cur = conn.cursor()
cur.execute("show tables")
for i in cur.fetch():
	print i
cur.close()
conn.close()

しかし、今の状態だと以下の様なエラーが出ると思われる。

$ python thrift_sample.py
Traceback (most recent call last):
  File "thrift_sample.py", line 27, in <module>
    database='default')
  File "/usr/local/lib/python2.7/dist-packages/pyhs2/__init__.py", line 6, in connect
    from .connections import Connection
  File "/usr/local/lib/python2.7/dist-packages/pyhs2/connections.py", line 6, in <module>
    import sasl
ImportError: No module named sasl

pyhs2の依存パッケージ(sasl)をインストールする必要がある。そのままsaslをpipでインストールしてみると以下のようなエラーが出るはず。

$ pip install sasl
:
:
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Isasl -I/usr/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]

sasl/saslwrapper.cpp:21:23: fatal error: sasl/sasl.h: No such file or directory

compilation terminated.

error: command 'gcc' failed with exit status 1
:

libsasl2-devをインストールする必要がある。

$ sudo apt-get install libsasl2-dev

もう一度実行⇒OK

$ pip install sasl

これでもう一度上記のサンプルプログラム(thrift_sample.py)を実行すればうまく動く(はず)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment