Python应用与Hbase交互实例(来源找不着了)
2017-07-13 11:03
429 查看
1 生成Python语言的HBase
Thrift客户端函数库
为了建立Thrift客户端函数库,你需要安装Thrift。但是Thrift还没有打包,所以你必须基于源码编译它。因为Thrift可以通过Homebrew得到[4],所以在Mac机器上这一步就很简单:
$
brew install thrift
...
==> Summary
/usr/local/Cellar/thrift/0.8.0: 75 files, 5.4M, built in 2.4 minutes
那些运行其他平台的机器需要手工建立Thrift。可以查看Thrift需求[5]文档来了解针对你的平台的细节。
完成以后,请验证你的Thrift已经启动并且工作正常:
$
thrift -version
Thrift version 0.8.0
4000
你希望不用下载HBase源代码就可以读完这本书,对吗?很抱歉,会让你失望的。如果你需要生成Thrift客户端,你需要下载HBase的源代码:
$
wget http://www.apache.org/dist/hbase/hbase-0.92.1/hbase-0.92.1.tar.gz
...
Saving to: `hbase-0.92.1.tar.gz'
$
tar xzf hbase-0.92.1.tar.gz
在下载HBase源代码和安装Thrift后,你需要关注一个文件:src/ main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift。这就是描述HBase服务API和有关对象的IDL文件。请快速浏览一下这个文件——Thrift
IDL是容易读懂的。现在你准备好了用来生成Python客户端的所有东西。
先给自己创建一个项目目录,然后生成HBase客户端:
$
mkdir twitbase.py
$
cd twitbase.py
$
thrift -gen py ../hbase-0.92.1/src/main/resources/org/apache/hadoop/hbase/
thrift/Hbase.thrift
$
mv gen-py/* .
$
rm -r gen-py/
你创建了一个叫做twitbase.py的项目,然后生成了HBase Python函数库。Thrift在一个叫做gen-py的子目录里生成它的代码。把这些文件移动到你的项目里,你可以轻松把代码导入到应用里。看看生成了什么文件:
$
find .
./__init__.py
./hbase
./hbase/__init__.py
./hbase/constants.py
./hbase/Hbase-remote
./hbase/Hbase.py
./hbase/ttypes.py
你还需要安装Thrift Python函数库。这些是通过Python使用的所有Thrift服务的核心组件,所以你可以全局性安装它们:
$
sudo easy_install thrift==0.8.0
Searching for thrift==0.8.0
Best match: thrift 0.8.0
...
Finished processing dependencies for thrift
另外,这个函数库也是你编译的源代码的一部分。你可以像处理HBase客户端那样把这些文件复制到你的项目里。在twitbase.py目录下,你可以复制这些文件,如下所示。
$
mkdir thrift
$
cp -r ../thrift-0.8.0/lib/py/src/* ./thrift/
验证一切按照预期那样工作。先启动Python,然后导入Thrift和HBase函数库。没有输出信息意味着一切正常:
$
python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
...
>>>
import thrift
>>>
import hbase
确保在twitbase.py目录下运行这些命令,否则import声明会失败。当客户端函数库准备好以后,让我们开启服务组件。
2 启动HBase Thrift服务
Thrift服务端组件已经随HBase预装了,所以它没有涉及到客户端函数库所需要的安装过程。可以使用hbase命令,如同启动Shell一样启动Thrift服务:
$
hbase thrift
...
usage: Thrift [-b ] [-c] [-f] [-h] [-hsha | -nonblocking |
-threadpool] [-p ]
-b,--bind Address to bind the Thrift server to. Not supported by
the Nonblocking and HsHa server [default: 0.0.0.0]
-c,--compact Use the compact protocol
-f,--framed Use framed transport
-h,--help Print help information
-hsha Use the THsHaServer. This implies the framed transport.
-nonblocking Use the TNonblockingServer. This implies the framed
transport.
-p,--port Port to bind to [default: 9090]
-threadpool Use the TThreadPoolServer. This is the default.
先确定HBase已经启动,并且正在运行,再启动Thrift服务。默认设置应该可以正常工作:
$
hbase thrift start
...
ThriftServer: starting HBase ThreadPool Thrift server on /0.0.0.0:9090
在客户端和服务器都准备好以后,该测试它们了。在twitbase.py项目目录下打开一个终端窗口,再一次启动Python:
$
python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
...
>>>
from thrift.transport import TSocket
>>>
from thrift.protocol import TBinaryProtocol
>>>
from hbase import Hbase
>>>
transport = TSocket.TSocket('localhost', 9090)
>>>
protocol = TBinaryProtocol.TBinaryProtocol(transport)
>>>
client = Hbase.Client(protocol)
>>>
transport.open()
>>>
client.getTableNames()
['followers', 'twits', 'users']
走到这里花了一些时间,但是一切正常工作!现在你可以开始处理正事儿了。
Thrift客户端函数库
为了建立Thrift客户端函数库,你需要安装Thrift。但是Thrift还没有打包,所以你必须基于源码编译它。因为Thrift可以通过Homebrew得到[4],所以在Mac机器上这一步就很简单:
$
brew install thrift
...
==> Summary
/usr/local/Cellar/thrift/0.8.0: 75 files, 5.4M, built in 2.4 minutes
那些运行其他平台的机器需要手工建立Thrift。可以查看Thrift需求[5]文档来了解针对你的平台的细节。
完成以后,请验证你的Thrift已经启动并且工作正常:
$
thrift -version
Thrift version 0.8.0
4000
你希望不用下载HBase源代码就可以读完这本书,对吗?很抱歉,会让你失望的。如果你需要生成Thrift客户端,你需要下载HBase的源代码:
$
wget http://www.apache.org/dist/hbase/hbase-0.92.1/hbase-0.92.1.tar.gz
...
Saving to: `hbase-0.92.1.tar.gz'
$
tar xzf hbase-0.92.1.tar.gz
在下载HBase源代码和安装Thrift后,你需要关注一个文件:src/ main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift。这就是描述HBase服务API和有关对象的IDL文件。请快速浏览一下这个文件——Thrift
IDL是容易读懂的。现在你准备好了用来生成Python客户端的所有东西。
先给自己创建一个项目目录,然后生成HBase客户端:
$
mkdir twitbase.py
$
cd twitbase.py
$
thrift -gen py ../hbase-0.92.1/src/main/resources/org/apache/hadoop/hbase/
thrift/Hbase.thrift
$
mv gen-py/* .
$
rm -r gen-py/
你创建了一个叫做twitbase.py的项目,然后生成了HBase Python函数库。Thrift在一个叫做gen-py的子目录里生成它的代码。把这些文件移动到你的项目里,你可以轻松把代码导入到应用里。看看生成了什么文件:
$
find .
./__init__.py
./hbase
./hbase/__init__.py
./hbase/constants.py
./hbase/Hbase-remote
./hbase/Hbase.py
./hbase/ttypes.py
你还需要安装Thrift Python函数库。这些是通过Python使用的所有Thrift服务的核心组件,所以你可以全局性安装它们:
$
sudo easy_install thrift==0.8.0
Searching for thrift==0.8.0
Best match: thrift 0.8.0
...
Finished processing dependencies for thrift
另外,这个函数库也是你编译的源代码的一部分。你可以像处理HBase客户端那样把这些文件复制到你的项目里。在twitbase.py目录下,你可以复制这些文件,如下所示。
$
mkdir thrift
$
cp -r ../thrift-0.8.0/lib/py/src/* ./thrift/
验证一切按照预期那样工作。先启动Python,然后导入Thrift和HBase函数库。没有输出信息意味着一切正常:
$
python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
...
>>>
import thrift
>>>
import hbase
确保在twitbase.py目录下运行这些命令,否则import声明会失败。当客户端函数库准备好以后,让我们开启服务组件。
2 启动HBase Thrift服务
Thrift服务端组件已经随HBase预装了,所以它没有涉及到客户端函数库所需要的安装过程。可以使用hbase命令,如同启动Shell一样启动Thrift服务:
$
hbase thrift
...
usage: Thrift [-b ] [-c] [-f] [-h] [-hsha | -nonblocking |
-threadpool] [-p ]
-b,--bind Address to bind the Thrift server to. Not supported by
the Nonblocking and HsHa server [default: 0.0.0.0]
-c,--compact Use the compact protocol
-f,--framed Use framed transport
-h,--help Print help information
-hsha Use the THsHaServer. This implies the framed transport.
-nonblocking Use the TNonblockingServer. This implies the framed
transport.
-p,--port Port to bind to [default: 9090]
-threadpool Use the TThreadPoolServer. This is the default.
先确定HBase已经启动,并且正在运行,再启动Thrift服务。默认设置应该可以正常工作:
$
hbase thrift start
...
ThriftServer: starting HBase ThreadPool Thrift server on /0.0.0.0:9090
在客户端和服务器都准备好以后,该测试它们了。在twitbase.py项目目录下打开一个终端窗口,再一次启动Python:
$
python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
...
>>>
from thrift.transport import TSocket
>>>
from thrift.protocol import TBinaryProtocol
>>>
from hbase import Hbase
>>>
transport = TSocket.TSocket('localhost', 9090)
>>>
protocol = TBinaryProtocol.TBinaryProtocol(transport)
>>>
client = Hbase.Client(protocol)
>>>
transport.open()
>>>
client.getTableNames()
['followers', 'twits', 'users']
走到这里花了一些时间,但是一切正常工作!现在你可以开始处理正事儿了。
相关文章推荐
- Python-->logging....实例应用
- Python数据采集处理分析挖掘可视化应用实例
- python人人网登录应用实例
- python多线程应用实例
- Python3 练习实例1、2 来源:菜鸟教程
- Python 中json的应用实例----天气预报
- HBase应用笔记:通过Java Api与HBase交互(转自 Taobao QA Team)
- PYTHON 与C相互交互调用实例解析
- Python通过DOM和SAX方式解析XML的应用实例分享
- python基础之类和对象、对象之间的交互、类名称空间与对象/实例名称空间
- 零基础学python-17.4 参数匹配应用实例
- Python起步之turtle库应用实例
- hadoop+zookeeper+hbase安装、配置及应用实例
- python快速查找算法应用实例
- python之wxPython应用实例
- python中关于cur.connect()等方法的应用实例
- Axure的富交互应用实例
- python 应用实例
- [Python]网络爬虫(四):Opener与Handler的介绍和实例应用
- python子进程模块subprocess详解与应用实例 之二