Python 使用thrift连接hbase || 远程连接hbase

1.首先下载Python & thrift &hbase

有两种安装thrift的方式:1.下载的thrift-0.9.3.tar.gz >> 解压 tar xzvf thrift-0.9.3.tar.gz  >> ./configure >> make >> make install >>thrift -version 验证
    2.解压之后进入lib/py/   >> 执行Python setup.py install 安装 。这种方式不需要软连接到python的site-packages/ >> python >> import thrift 验证。


2.解压hbase:

tar xzvf hbase-1.1.2.tar.gz >> cd hbase-thrift  >> thrift -gen py /root/zhutong/hbase-1.1.2/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift 会生成一个 gen-py的文件 >> cd gen-py >> cp -R hbase /usr/local/python2.7/lib/python2.7/site-packages/ 
 



3.启动thrift:

单机模式启动:首先启动hbase >>cd bin  >>  ./start-hbase.sh >> 启动thrift  ./hbase-daemon.sh start thrift  # 默认的端口是9090   
配置hbase的rootdir 
<property>
<name>hbase.rootdir</name>
<value>/root/zhutong/hbase_data</value>
</property>

环境配置好了 现在准备测试:
编写test.py 文件

#!/usr/bin/python
#-*- coding:utf-8 -*-
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.protocol import TCompactProtocol
from hbase import Hbase
from hbase.ttypes import *
#192.168.110.78:8020
transport1 = TSocket.TSocket('localhost',9090 )
transport = TTransport.TBufferedTransport(transport1)
protocol = TCompactProtocol.TCompactProtocol(transport);
client = Hbase.Client(protocol)
transport.open()
contents=ColumnDescriptor(name='cf:',maxVersions=1)
tablename='ta'
client.createTable(tablename,[contents])
print client.getTableNames()

异常:如果报错找不到thrift模块 ,查看Python安装目录site-packages下没有thrift*.egg 文件 ,使用easy-install 或者第一步中第二种方式安装。
            如果提示主机名错误异常;请前去/etc/hosts文件中添加映射关系。

成功之后再对hbase进行操作;
#!/usr/bin/python
#insert data
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()
for i in range(10):
        row = 'row-key1'+str(i)

        mutations = [Mutation(column="cf:a", value=str(i))]
        client.mutateRow('ta', row, mutations, None)

#!/usr/bin/python

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()

tableName = 'ta'
rowKey = 'row-key1'

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

#!/usr/bin/python
# get some data 
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()

scan = TScan()
tableName = 'ta'
id = client.scannerOpenWithScan(tableName, scan, None)

result2 = client.scannerGetList(id, 10)

print result2
print type(result2)

for i in result2:
        print i.row


4.远程连接hbase

 修改Python文件的IP地址和端口进行连接会出现如下的错误:



或者



这是因为远程访问的hbase也是需要thrift来进行访问的,所以需要在访问的hbase服务器上启动thrift:
nohup hbase thrift -p 9999 start  &  启动 >> ps aux | grep thrift 验证
然后再执行 : 端口号为你设置的端口号;

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python使用Thrift连接HBase,您需要按照以下步骤进行设置: 1. 安装所需的依赖项: 您需要安装`thrift`和`happybase`这两个Python库。可以使用以下命令进行安装: ```bash pip install thrift happybase ``` 2. 生成HBaseThrift代码: 使用Thrift工具生成HBaseThrift代码。您可以使用以下命令: ```bash thrift -r --gen py hbase.thrift ``` 这将生成PythonThrift代码文件。 3. 创建HBase连接: 在Python脚本中,您需要首先创建一个HBase连接。示例代码如下: ```python import happybase connection = happybase.Connection(host='localhost', port=9090) ``` 4. 执行HBase操作: 在创建了HBase连接之后,您可以使用`connection`对象执行各种HBase操作,例如创建表、插入数据、获取数据等。以下是一些示例代码: - 创建表: ```python connection.create_table( 'mytable', { 'cf': dict(max_versions=10), } ) ``` - 插入数据: ```python table = connection.table('mytable') table.put( b'row1', { b'cf:col1': b'value1', b'cf:col2': b'value2', } ) ``` - 获取数据: ```python table = connection.table('mytable') row = table.row(b'row1') print(row) ``` - 删除数据: ```python table = connection.table('mytable') table.delete(b'row1') ``` 这只是一些示例代码,您可以根据需要使用其他HappyBase方法来执行更多操作。 5. 关闭连接: 当您完成HBase操作后,记得关闭连接以释放资源: ```python connection.close() ``` 请注意,为了成功执行这些操作,您需要确保HBase正在运行并且在指定的主机和端口上进行监听。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值