利用python访问Hbase(Thrift模块安装与测试)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

hadoop环境介绍:

master服务:node1

slave服务器:node2,node3,node4

mysql服务器:node29

Thrift安装在node1服务器上!


相关软件版本:

hadoop版本:hadoop-0.20.2

sqoop版本:sqoop-1.2.0-CDH3B4

java版本:jdk1.7.0_67

mysql版本:5.1.65

Thrift版本:thrift-0.9.0

thrift安装链接:http://thrift.apache.org/download/

python版本:2.7.3

ps:python2.5版本使用thrift有问题



一:测试前的准备工作

1)首先把mysql数据库中数据加载到hbase中:

mysql数据如下:

wKiom1RKCu7Acn6aAAEPRQIQRa8209.jpg


将mysql的数据导入hbase的命令格式为:

sqoop import --connect jdbc:mysql://mysqlserver_IP/databaseName --username --password password --table datatable --hbase-create-table --hbase-table hbase_tablename --column-family col_fam_name --hbase-row-key key_col_name


说明:databaseName 和datatable 是mysql的数据库和表名,hbase_tablename是要导成hbase的表名,key_col_name可以指定datatable中哪一列作 为hbase新表的rowkey,col_fam_name是除rowkey之外的所有列的列族名


2)在node1上加载mysql数据(node29)到hbase中:

sqoop import --connect jdbc:mysql://172.16.41.29/sqoop --username sqoop --password routon --table students --hbase-create-table --hbase-table students --column-family stuinfo --hbase-row-key id


在hbase中验证是否加载成功:

wKioL1RKDCHRujBiAAJMEeBRwoU194.jpg


二 Thrift软件安装

python版本:2.7.3


步骤为:

1) 安装python2.7.3

说明:python2.7.3与thrift结合没问题,python2.5版本好像不行!



生成的Hbase.py文件中的语法rhel5 自带的python2.4 不支持

tar fvxj Python-2.7.3.tar.bz2

./configure --prefix=/usr/local/python2.7

make && make install

python2.7.3路径为:

/usr/local/python2.7/bin/python

修改默认的python版本为2.7

把python2.7设置为环境变量,系统默认python版本为2.4

rm -rf /usr/bin/python

ln -s /usr/local/python2.7/bin/python /usr/bin/python


[root@node1 thrift-0.9.0]# python -V

Python 2.7.3

 


2)安装thrift

tar fvxz thrift-0.9.0.tar.gz

cd thrift-0.9.0

./configure 

make && make install


thrift 0.9.0


Building C++ Library ......... : no

Building C (GLib) Library .... : yes

Building Java Library ........ : no

Building C# Library .......... : no

Building Python Library ...... : yes

Building Ruby Library ........ : no

Building Haskell Library ..... : no

Building Perl Library ........ : no

Building PHP Library ......... : no

Building Erlang Library ...... : no

Building Go Library .......... : no

Building D Library ........... : no


Python Library:

   Using Python .............. : /usr/bin/python


可以看到thrift支持很多语言,根据目前需求,支持python就可以了!


查看Thrift版本:

[root@node1 thrift-0.9.0]# thrift -version

Thrift version 0.9.0


3)让thrift支持hbase

执行以下命令:

thrift --gen py /usr/local/hbase-0.90.5/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift


会在当前目前下创建一个目录,目录名称为:

[hadoop@node1 ~]$ ll

total 7056

-rw-rw-r-- 1 hadoop hadoop    3045 Oct 14 13:55 access_log2.txt

-rw-r--r-- 1 hadoop hadoop 7118627 Feb  1  2012 access_log.txt

-rw-rw-r-- 1 hadoop hadoop    3500 Oct 22 10:17 derby.log

drwxrwxr-x 3 hadoop hadoop    4096 Oct 24 15:28 gen-py

-rw-rw-r-- 1 hadoop hadoop    3551 Oct 13 11:21 pig_1413170429087.log


gen-py目录结构如下:

[hadoop@node1 ~]$ tree gen-py/

gen-py/

|-- __init__.py

`-- hbase

    |-- Hbase-remote

    |-- Hbase.py

    |-- __init__.py

    |-- constants.py

    `-- ttypes.py


1 directory, 6 files



4)把gen-py目录复制到python相关目录中:

cp -r gen-py/hbase/ /usr/local/python2.7/lib/python2.7/site-packages/.


5)让python可以import thrift模块:


 [root@node1 ~]# ln -s /usr/lib/python2.7/site-packages/thrift* /usr/local/python2.7/lib/python2.7/site-packages/.

[root@node1 ~]# ls -l /usr/local/python2.7/lib/python2.7/site-packages/

total 12

drwxr-xr-x 2 root root 4096 Oct 24 15:32 hbase

-rw-r--r-- 1 root root  119 Oct 24 11:30 README

lrwxrwxrwx 1 root root   39 Oct 24 15:50 thrift -> /usr/lib/python2.7/site-packages/thrift

lrwxrwxrwx 1 root root   60 Oct 24 15:50 thrift-0.9.0-py2.7.egg-info -> /usr/lib/python2.7/site-packages/thrift-0.9.0-py2.7.egg-info



6)启动thrift服务:

hbase thrift -p 9090 start


7)在node1上编写python脚本,查看hbase中有哪些表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#! /usr/bin/env python
#coding=utf-8
 
import  sys
#Hbase.thrift生成的py文件放在这里
sys.path.append( '/usr/local/lib/python2.7/site-packages/hbase' )
from  thrift  import  Thrift
from  thrift.transport  import  TSocket
from  thrift.transport  import  TTransport
from  thrift.protocol  import  TBinaryProtocol
from  hbase  import  Hbase
#如ColumnDescriptor 等在hbase.ttypes中定义
from  hbase.ttypes  import  *
# Make socket
#此处可以修改地址和端口
transport  =  TSocket.TSocket( '172.16.41.26' 9090 )
# Buffering is critical. Raw sockets are very slow
# 还可以用TFramedTransport,也是高效传输方式
transport  =  TTransport.TBufferedTransport(transport)
# Wrap in a protocol
#传输协议和传输过程是分离的,可以支持多协议
protocol  =  TBinaryProtocol.TBinaryProtocol(transport)
#客户端代表一个用户
client  =  Hbase.Client(protocol)
#打开连接
transport. open ()
#打印表名
print (client.getTableNames())


执行脚本:

wKiom1RKEALwhL86AAEY9rF6T_g766.jpg


到此,通过thrift插件,python就可以和hbase进行通信!




本文转自 shine_forever 51CTO博客,原文链接:http://blog.51cto.com/shineforever/1567640

相关实践学习
云数据库HBase版使用教程
  相关的阿里云产品:云数据库 HBase 版 面向大数据领域的一站式NoSQL服务,100%兼容开源HBase并深度扩展,支持海量数据下的实时存储、高并发吞吐、轻SQL分析、全文检索、时序时空查询等能力,是风控、推荐、广告、物联网、车联网、Feeds流、数据大屏等场景首选数据库,是为淘宝、支付宝、菜鸟等众多阿里核心业务提供关键支撑的数据库。 了解产品详情: https://cn.aliyun.com/product/hbase   ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
2天前
|
测试技术 API 网络架构
Python的api自动化测试 编写测试用例
【4月更文挑战第18天】使用Python进行API自动化测试,可以结合`requests`库发送HTTP请求和`unittest`(或`pytest`)编写测试用例。以下示例: 1. 安装必要库:`pip install requests unittest` 2. 创建`test_api.py`,导入库,定义基础URL。 3. 创建继承自`unittest.TestCase`的测试类,包含`setUp`和`tearDown`方法。 4. 编写测试用例,如`test_get_users`,检查响应状态码和内容。 5. 运行测试:`python -m unittest test_api.py`
12 2
|
2天前
|
JSON 测试技术 API
Python的Api自动化测试使用HTTP客户端库发送请求
【4月更文挑战第18天】在Python中进行HTTP请求和API自动化测试有多个库可选:1) `requests`是最流行的选择,支持多种请求方法和内置JSON解析;2) `http.client`是标准库的一部分,适合需要低级别控制的用户;3) `urllib`提供URL操作,适用于复杂请求;4) `httpx`拥有类似`requests`的API,提供现代特性和异步支持。根据具体需求选择,如多数情况`requests`已足够。
8 3
|
3天前
|
开发者 Python
Python的os模块详解
Python的os模块详解
15 0
|
3天前
|
测试技术 持续交付 API
Python的UI自动化测试
【4月更文挑战第17天】Python UI自动化测试涉及Selenium(Web)、Appium(移动应用)和PyQt(桌面应用)等框架。基本步骤包括确定测试目标、选择合适框架、安装配置、编写测试脚本、运行调试以及集成到CI/CD流程。注意自动化测试不能完全取代人工测试,应根据需求平衡使用。
8 1
|
4天前
|
前端开发 测试技术 C++
Python自动化测试面试:unittest、pytest与Selenium详解
【4月更文挑战第19天】本文聚焦Python自动化测试面试,重点讨论unittest、pytest和Selenium三大框架。unittest涉及断言、TestSuite和覆盖率报告;易错点包括测试代码冗余和异常处理。pytest涵盖fixtures、参数化测试和插件系统,要注意避免过度依赖unittest特性。Selenium的核心是WebDriver操作、等待策略和测试报告生成,强调智能等待和元素定位策略。掌握这些关键点将有助于提升面试表现。
17 0
|
4天前
|
XML Web App开发 测试技术
python的Web自动化测试
【4月更文挑战第16天】Python在Web自动化测试中广泛应用,借助Selenium(支持多浏览器交互)、BeautifulSoup(解析HTML/XML)、Requests(发送HTTP请求)和Unittest(测试框架)等工具。测试步骤包括环境搭建、编写测试用例、初始化浏览器、访问页面、操作元素、验证结果、关闭浏览器及运行报告。注意浏览器兼容性、动态内容处理和错误处理。这些组合能提升测试效率和质量。
11 6
|
4天前
|
测试技术 持续交付 数据库
python集成测试
【4月更文挑战第16天】在Python集成测试中,确保模块间正确交互是关键。选择合适的测试框架如`unittest`或`pytest`,定义全面的测试用例,编写测试代码并设置类似生产环境的测试环境。执行测试后分析修复问题,将测试整合到持续集成流程,以尽早发现并解决问题。例如,使用`pytest`,我们可以创建测试用例验证不同模块间的功能是否按预期协同工作。
10 2
|
6天前
|
数据挖掘 API 数据安全/隐私保护
python请求模块requests如何添加代理ip
python请求模块requests如何添加代理ip
|
7天前
|
测试技术 Python
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
Python 有趣的模块之pynupt——通过pynput控制鼠标和键盘
|
7天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
40 1