Install thrift of python connection to hbase under ambari installation cluster

Introduction:  

  Python needs to connect to hbase through thrift connection. It seems that there is no thrift installed with hbase in the service installed by ambari. I saw that there is no thrift in the configuration name of hbase, but the cdh version has it, so I installed it myself thrift.

1. thrift installation:

1. Download thrift dependencies 

yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel openssl-devel

2. Install boost_1_53_0.tar.gz

[[email protected] ~]# wget http://sourceforge.net/projects/boost/files/boost/1.53.0/boost_1_53_0.tar.gz
[[email protected] ~]# tar xvf boost_1_53_0.tar.gz
[[email protected] ~]# cd boost_1_53_0
[[email protected] boost_1_53_0]# ./bootstrap.sh
[[email protected] boost_1_53_0]# ./b2 install
[[email protected] boost_1_53_0]# cp /usr/local/lib/libboost_unit_test_framework.a /usr/lib64/
[[email protected] boost_1_53_0]# make
[[email protected] boost_1_53_0]# make install

3. Download the latest version of thrift from http://thrift.apache.org
 
4. Move to the default installation directory and unzip it

[[email protected] ~]# mv thrift-0.11.0.tar.gz /usr/local
[[email protected] ~]# cd /usr/local
[[email protected] local]# tar -zxvf thrift-0.11.0.tar.gz
[[email protected] local]# mv thrift-0.11.0 thrift
[[email protected] local]# cd thrift
[[email protected] local]# ./configure --libdir=/usr/lib --without-java --without-python --without-c_glib
[[email protected] local]# make
[[email protected] local]# make install

 2. Start thrift

1. Find the bin execution folder of hbase. The location of my execution is /usr/hdp/2.6.3.0-235/hbase/bin under the folder where ambari is installed by default

2. Start thrift, the default port is 9090

[[email protected] ~]# cd /usr/hdp/2.6.3.0-235/hbase/bin
[[email protected] bin]# bin/hbase-daemon.sh start thrift

3. Use python to connect hbase

1. Install python dependencies

pip install thrift
pip install hbase-thrift

2. demo program

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

from hbase import Hbase
from hbase.ttypes import *

transport = TSocket.TSocket('localhost', 9090)

transport = TTransport.TBufferedTransport (transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)
transport.open()

contents = ColumnDescriptor(name='cf:', maxVersions=1)
# client.deleteTable('test')
client.createTable('test', [contents])

print client.getTableNames()

# insert data
transport.open()

row = 'row-key1'

mutations = [Mutation(column="cf:a", value="1")]
client.mutateRow('test', row, mutations)

# get one row
tableName = 'test'
rowKey = 'row-key1'

result = client.getRow(tableName, rowKey)
print result
for r in result:
    print 'the row is ', r.row
    print 'the values is ', r.columns.get('cf:a').value
transport.close()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325848020&siteId=291194637