參考博客:http://blog.csdn.net/jiedushi/article/details/7531722
1. 安裝Thrift
1.安裝依賴庫
yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel
- 下載鏈接thrift-0.10.0
- 解壓安裝
tar xvf thrift-0.10.0.tar.gz
cd thrift-0.10.0
./configure
make
sudo make install
./configure
報錯configure: error: Bison version 2.5 or higher must be installed on the system!
yum的base源默認安裝的bison版本為2.4.1太低,下載bison-2.5安裝包編譯安裝
wget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz tar xvf tar xvf
bison-2.5.1.tar.gz cd bison-2.5.1 ./configure make sudo make install
回到thrift文件夾下重新執行./configure
2. 復制Hive文件
cp -r $HIVE_PATH/lib/py/* /usr/local/lib/python2.7/site-packages
3. 啟動hiveserver2
hiveserver2 &
4. Python中編程
Hive官網的示例代碼:https://cwiki.apache.org/confluence/display/Hive/HiveClient#HiveClient-Python
import sys
from hive_service import ThriftHive
from hive_service.ttypes import HiveServerException
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
try:
transport = TSocket.TSocket('localhost', 10000)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = ThriftHive.Client(protocol)
transport.open()
client.execute("CREATE TABLE r(a STRING, b INT, c DOUBLE)")
client.execute("LOAD TABLE LOCAL INPATH '/path' INTO TABLE r")
client.execute("SELECT * FROM r")
while (1):
row = client.fetchOne()
if (row == None):
break
print row
client.execute("SELECT * FROM r")
print client.fetchAll()
transport.close()
except Thrift.TException, tx:
print '%s' % (tx.message)
不過收不到響應,問題待解決