Python 使用thrift连接hbase || 远程连接hbase
来源:互联网 发布:java 二维数组 length 编辑:程序博客网 时间:2024/06/04 20:13
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 Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.protocol import TCompactProtocolfrom hbase import Hbasefrom hbase.ttypes import *#192.168.110.78:8020transport1 = 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 datafrom thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import Hbasefrom 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/pythonfrom thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import Hbasefrom 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 resultprint 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 Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import Hbasefrom 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 result2print 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 验证
然后再执行 : 端口号为你设置的端口号;
0 0
- Python 使用thrift连接hbase || 远程连接hbase
- pyspark Python 连接 HBase thrift
- python通过thrift连接Hbase
- [原创]Python通过Thrift连接HBase
- python操作hbase 远程连接
- Thrift安装配置及使用python通过thrift连接HBase测试
- Python 通过thrift接口连接Hbase读取存储数据
- Python通过thrift连接Hbase(详细配置步骤)
- php 通过 thrift-0.8.0 连接hbase 以及函数使用
- ubuntu使用thrift来连接PHP和HBase
- python连接Hbase
- Python 连接 Hbase
- python连接HBase
- C#通过thrift连接hbase操作步骤
- eclipse远程连接hbase错误
- 使用Phoenix连接Hbase
- python hbase thrift
- 通过Python使用HBase Thrift接口 url
- JSP自定义标签-嵌套
- Windows远程桌面连接Ubuntu 14.04
- NSoperation-( 多线程2)
- 中缀表达式转换为前缀及后缀表达式并求值【摘】
- 字符char、字节byte傻傻分不清(2015年10月13日)
- Python 使用thrift连接hbase || 远程连接hbase
- jQuery-autoComplete实例
- Java打印完整的堆栈信息
- Java多线程编程总结
- ios- block循环引用问题
- delphi正则表达式学习笔记(一)
- json字符串转换
- vs2010+win7 64bit OpenGL程序无法启动此应用程序,因为计算机中丢失glut32.dll
- git 查看 本地仓库的commit记录