PHP和Golang使用Thrift1和Thrift2访问Hbase0.96.2(ubuntu12.04)

目录

前言–Thrift1和Thrift2的简要介绍

0.1 写在前面

  从Hbase0.94.11开始有两套thrift接口(可以叫Thrift1和Thrift2),根据官方文档,Thrift1很可能被抛弃,但网上的文章基本是介绍Thrift1的,本文则兼容介绍Thrift1和Thrift2,目前在网上是比较详细的介绍Thrift2文章了。
  Thrift2相比较Thrift1做了简化和合成,但不提供查询所有Table、创建Table、删除Table的功能了。日常在工作中应该也很少会在Hbase中使用Thrift来创建Table。
  从Thrift0.9.1开始支持golang语言,相关使用和demo可以参考这里《Golang、Php、Python、Java基于Thrift0.9.1实现跨语言调用》,不过由于写本文时Thrift0.9.1中对Golang访问Hbase支持的并不友好,在0.9.2中的Thrift2已经进行了改进可以很好的支持Golang访问Hbase。所以后面在使用Golang访问Hbase时,使用的是Thrift0.9.2。
  网上关于Thrift1的文章一搜索有很多,也就不在这里多描述,在后面的演示中Thrift2中90%的方法都会有,不过在实际使用中可能还需要根据你的系统要求进行二次封装 。
  

0.2 Thrift1和Thrift2的区别

  现在官网也没有太详细的介绍,下面的对比也是从IDL描述中找到的。这是一位网友整理的,后面的扩展阅读中链接了他的文章,更多的介绍可以去那里看。

 

Thrift

Thrift2

结构

struct TCell

struct ColumnDescriptor

struct TRegionInfo

struct Mutation

struct BatchMutation

struct TIncrement

struct TColumn

struct TRowResult

struct TScan

struct TTimeRange

struct TColumn

struct TColumnValue

struct TColumnIncrement

struct TResult

struct TGet

struct TPut

struct TDelete

struct TIncrement

struct TScan

struct TRowMutations

异常

exception IOError

exception IllegalArgument

exception AlreadyExists

exception TIOError

exception TIllegalArgument

其他

 

union TMutation

enum TDeleteType

enum TDurability

服务

名称为:Hbase

void enableTable()

void disableTable()

bool isTableEnabled()

void compact()

void majorCompact()

list<Text> getTableNames()

map<Text,ColumnDescriptor> getColumnDescriptors()

list<TRegionInfo> getTableRegions()

void createTable()

void deleteTable()

list<TCell> get()

list<TCell> getVer()

list<TCell> getVerTs()

list<TRowResult> getRow()

list<TRowResult> getRowWithColumns()

list<TRowResult> getRowTs()

list<TRowResult> getRowWithColumnsTs()

list<TRowResult> getRows()

list<TRowResult> getRowsWithColumns()

list<TRowResult> getRowsTs()

list<TRowResult> getRowsWithColumnsTs()

void mutateRow()

void mutateRowTs()

void mutateRows()

void mutateRowsTs()

i64 atomicIncrement()

void deleteAll()

void deleteAllTs()

void deleteAllRow()

void increment()

void incrementRows()

void deleteAllRowTs()

ScannerID scannerOpenWithScan()

ScannerID scannerOpen()

ScannerID scannerOpenWithStop()

ScannerID scannerOpenWithPrefix()

ScannerID scannerOpenTs()

ScannerID scannerOpenWithStopTs()

list<TRowResult> scannerGet()

list<TRowResult> scannerGetList()

void scannerClose()

list<TCell> getRowOrBefore()

TRegionInfo getRegionInfo()

名称为:THBaseService

bool exists(…)

TResult get(…)

list<TResult> getMultiple(…)

void put(…)

bool checkAndPut(…)

void putMultiple(…)

void deleteSingle(…)

list<TDelete> deleteMultiple(…)

bool checkAndDelete(…)

TResult increment(…)

i32 openScanner(…)

list<TResult> getScannerRows(…)

void closeScanner(…)

void mutateRow(…)

list<TResult> getScannerResults(…)

1. Thrift0.9.2的安装

1.1 安装依赖插件

root@m1:/home/hadoop#sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
#下载thirft0.9.2版本
root@m1:/home/hadoop# git clone https://github.com/apache/thrift.git thrift-git0.9.2
Cloning into 'thrift-git0.9.2'...
remote: Counting objects: 37221, done.
remote: Compressing objects: 100% (195/195), done.
remote: Total 37221 (delta 312), reused 407 (delta 272)
Receiving objects: 100% (37221/37221), 9.61 MiB | 74 KiB/s, done.
Resolving deltas: 100% (25830/25830), done.
root@m1:/home/hadoop#cd thrift-git0.9.2
# 切换成相应所要用的分支,如果你编译通过,就不用像我现在这样切换了
root@m2:/home/hadoop/thrift-git0.9.2# git checkout --track origin/0.9.2
Branch 0.9.2 set up to track remote branch 0.9.2 from origin.
Switched to a new branch ‘0.9.2

1.2 Thrift0.9.2的编译

  #编译Thrift0.9.2版本,注意这个版本和0.9.1编译方法不一样,详情可以参考/home/hadoop/thrift-git0.9.2/compiler/cpp/README.md中的说明

root@m1:/home/hadoop/thrift-git0.9.2# mkdir -p compiler/cpp/build
root@m1:/home/hadoop/thrift-git0.9.2# cd compiler/cpp/build
root@m1:/home/hadoop/thrift-git0.9.2/compiler/cpp/build# cmake ..
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Thrift package version: 0.9.2
-- Thrift version: 0.9.2 (0.9.2)
-- Found FLEX: /usr/bin/flex (found version "2.5.35")
-- Found BISON: /usr/bin/bison (found version "2.5")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hadoop/thrift-git0.9.2/compiler/cpp/build
root@m1:/home/hadoop/thrift-git0.9.2/compiler/cpp/build# make
[  3%] [BISON][thrifty] Building parser with bison 2.5
[  6%] Generating thrifty.h
[  9%] [FLEX][thriftl] Building scanner with flex 2.5.35
Scanning dependencies of target libparse
[ 12%] Building CXX object CMakeFiles/libparse.dir/thrifty.cc.o
[ 15%] Building CXX object CMakeFiles/libparse.dir/thriftl.cc.o
Linking CXX static library liblibparse.a
[ 15%] Built target libparse
Scanning dependencies of target thrift
[ 18%] Building CXX object CMakeFiles/thrift.dir/src/main.cc.o
[ 21%] Building C object CMakeFiles/thrift.dir/src/md5.c.o
[ 25%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_generator.cc.o
[ 28%] Building CXX object CMakeFiles/thrift.dir/src/parse/t_typedef.cc.o
[ 31%] Building CXX object CMakeFiles/thrift.dir/src/parse/parse.cc.o
[ 34%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_c_glib_generator.cc.o
[ 37%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_cpp_generator.cc.o
[ 40%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_java_generator.cc.o
[ 43%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_as3_generator.cc.o
[ 46%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_csharp_generator.cc.o
[ 50%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_py_generator.cc.o
[ 53%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_rb_generator.cc.o
[ 56%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_perl_generator.cc.o
[ 59%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_php_generator.cc.o
[ 62%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_erl_generator.cc.o
[ 65%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_cocoa_generator.cc.o
[ 68%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_st_generator.cc.o
[ 71%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_ocaml_generator.cc.o
[ 75%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_hs_generator.cc.o
[ 78%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_xsd_generator.cc.o
[ 81%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_html_generator.cc.o
[ 84%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_js_generator.cc.o
[ 87%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_javame_generator.cc.o
[ 90%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_delphi_generator.cc.o
[ 93%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_go_generator.cc.o
[ 96%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_d_generator.cc.o
[100%] Building CXX object CMakeFiles/thrift.dir/src/generate/t_lua_generator.cc.o
Linking CXX executable thrift
[100%] Built target thrift
root@m1:/home/hadoop/thrift-git0.9.2/compiler/cpp/build#
root@m1:/home/hadoop/thrift-git0.9.2/compiler/cpp/build# ./thrift -version
Thrift version 0.9.2

1.3 Thrift0.9.2编译后,配置成可执行文件

  #将编译后的Thrift0.9.2放到可执行程序目录,并命名为thrift092

root@m1:/home/hadoop/thrift-git0.9.2/compiler/cpp/build# whereis thrift
thrift: /usr/local/bin/thrift
root@m1:/home/hadoop/thrift-git0.9.2/compiler/cpp/build# cp thrift /usr/local/bin/thrift092
root@m1:/home/hadoop/thrift-git0.9.2/compiler/cpp/build# thrift092 -version
Thrift version 0.9.2
root@m1:/home/hadoop/thrift-git0.9.2/compiler/cpp/build#

2. Hbase0.96.2的安装

2.1 Hbase的介绍

  hbase的介绍,参考这里:http://baike.baidu.com/view/1993870.htm

2.2 Hbase的安装

  请参考这篇文章:《ubuntu12.04+hadoop2.2.0+zookeeper3.4.5+hbase0.96.2+hive0.13.1伪分布式环境部署
  后面的测试,也是基于上面这篇文章的环境测试的。

2.3 Hbase基于源码的Thrift生成接口文件

  #下载Hbase的源码包(后面会使用里面的Thrift1和Thrift2)

root@m1:/home/hadoop# wget http://mirrors.hust.edu.cn/apache/hbase/hbase-0.96.2/hbase-0.96.2-src.tar.gz
root@m1:/home/hadoop# tar xzvf hbase-0.96.2-src.tar.gz

  #创建测试目录

root@m1:/home/hadoop# mkdir thrift_hbase
root@m1:/home/hadoop# cd thrift_hbase/

  #生成Php基于Hbase的Thrift1的接口文件,由于Thrift0.9.1版本对Golang访问Hbase支持不够后,后面Golang访问Hbase用Thrift0.9.2来测试

root@m1:/home/hadoop/thrift_hbase# thrift --gen php /home/hadoop/hbase-0.96.2/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
root@m1:/home/hadoop/thrift_hbase# ll
总用量 16
drwxr-xr-x  4 root   root   4096 Aug 19 21:19 ./
drwxr-xr-x 47 hadoop hadoop 4096 Aug 19 21:15 ../
drwxr-xr-x  3 root   root   4096 Aug 19 21:19 gen-php/
root@m1:/home/hadoop/thrift_hbase#

3. PHP和Golang使用Thrift1和Thrift2访问Hbase

3.1 PHP基于Thrift1访问Hbase

  #将Php用到的Thrift1包复制到thrift_hbase里面

root@m1:/home/hadoop/thrift_hbase# mkdir libphp                      
root@m1:/home/hadoop/thrift_hbase# cp -r /home/hadoop/thrift-git/lib/php/src/* libphp/
root@m1:/home/hadoop/thrift_hbase# cp -r /home/hadoop/thrift-git/lib/php/lib/Thrift libphp/
root@m1:/home/hadoop/thrift_hbase# cp -r gen-php/Hbase libphp/
root@m1:/home/hadoop/thrift_hbase# rm -rf gen-php/

  #编写Hbase.php文件

<?php 
/**
 * PHP基于Hbase0.96.2的Thrift1访问Hbase
 *
 * @author     迦壹
 * @copyright  Copyright (c) 2014 (http://idoall.org)
 * @license    GNU General Public License 2.0
 * @version    1
 */
 $startTime = getMillisecond();
$GLOBALS['THRIFT_ROOT'] = './libphp';   # 指定库目录,可以是绝对路径或是相对路径 
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/ClassLoader/ThriftClassLoader.php';

use Thrift\ClassLoader\ThriftClassLoader; 
use Thrift\Type\TMessageType;
use Thrift\Type\TType; 
use 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值