使用perl通过thrift连接hbase读取数据

 

Thrift是一种跨语言的服务部署框架,通过一种中间语言定义RPC接口,然后通过编译器生成不同语言的代码,能够支持常见的开发语言,包括c#,java,python,php,ruby等等。本文尝试使用perl语言,通过thrift连接Hbase。

 

下载thrift,可至任意apache镜像站下载:http://mirror.bit.edu.cn/apache/thrift/

 
  •  

# wget http://mirror.bit.edu.cn/apache/thrift/0.8.0/thrift-0.8.0.tar.gz

 

# tar xvfz thrift-0.8.0.tar.gz  

  # cd thrift-0.8.0
 

执行编译命令如下:

 
  •  

# ./configure --prefix=/usr/local/thrift/ --with-cshare=no --with-java=no --with-erlang=no --with-python=no --with-php=no --with-php_extension=no --with-ruby=no --with-haskell=no --with-go=no --with-perl=yes

 

# make

  # make install
 

在我的个人环境中,安装时出现一些错误,经过检查主要是缺少依赖包,安装好相关依赖包后,编译安装过程就能够顺利进行下去。

 

主要出现的错误如下:

 

1、没有编译perl的链接库

 

尽管我们在编译时强制指定编译perl的链接库,但实际编译时仍然没有编译perl的链接库,输出信息如下:

   
  •  

Building code generators ..... :

 

 

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

 

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

 

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

 

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

 

Building Python Library ...... : no

 

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

 

Building Haskell Library ..... : no

 

Building Perl Library ........ : yes

 

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

 

Building Erlang Library ...... : no

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

您说其它语言的没有也就算了,因为我们明确地禁用了,但perl为什么没有呢,经过检查configure的输出日志,发现与在perl有关的部分提示:

 
  •  

checking for perl... /usr/bin/perl

  checking for perl module Bit::Vector... no
 

一看就知道,这是缺少模块,执行下列命令安装Bit::Vector模块:

   
  • # perl -MCPAN -e ¨install Bit::Vector¨
 

 

2、提示缺少yacc命令

 

编译时抛出错误信息如下:

 
  •  

       /bin/sh ../../ylwrap `test -f ¨src/thrifty.yy¨ || echo ¨./¨`src/thrifty.yy y.tab.c thrifty.cc y.tab.h thrifty.h y.output thrifty.output -- yacc  -d

  ../../ylwrap: line 109: yacc: command not found
 

直接通过yum命令安装byacc,然后就有yacc命令了:

 
  • # yum install byacc
 

 

3、提示缺少flex命令

 
  •  

       /bin/sh ../../ylwrap `test -f ¨src/thriftl.ll¨ || echo ¨./¨`src/thriftl.ll .c thriftl.cc -- /bin/sh /data/software/thrift-0.8.0/missing --run flex  

  /data/software/thrift-0.8.0/missing: line 52: flex: command not found
 

通过yum命令安装flex相关包:

 
  • # yum install flex
 

 

4、提示缺少perl模块

 
  • Warning: prerequisite Class::Accessor 0 not found.
 

执行下列命令进行安装:

 
  • # perl -MCPAN -e ¨install Class::Accessor¨
 

 

出现上类错误,安装软件包后如果编译仍然出错,那么就删除MakeFile和config.log文件,而后重新执行./configure命令编译安装。

 

安装完成后,下面配置perl语言连接hbase。

 

首先要生成perl的映射,这段要在装有HBase的环境上操作:

 
  •  

# cp /data/software/thrift-0.8.0/lib/perl/lib/* ~/perl-src/ -r

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

而后将生成的gen-perl目录中的Hbase目录,复制到具有perl环境的服务器适当路径下:

 
  •  

# mkdir perl-src/packages -p

  # cp -a gen-perl/Hbase perl-src/packages  
 

Hbase文件中的*.pm就是我们要用到的连接模板。

 

下面创建一段perl脚本,通过thrift连接HBase,输出当前所有表名:

 
  • # vi t_thrift.pl
 

增加下列内容:

 
  •  

#!/bin/env perl

 

 

use strict;

 

use warnings;

 

 

use lib ¨./perl-src¨;

 

use lib ¨./perl-src/packages¨;

 

 

use Data::Dumper;

 

use Thrift;

 

use Thrift::BinaryProtocol;

 

use Thrift::Socket;

 

use Thrift::BufferedTransport;

 

use Hbase::Hbase;

 

 

my $host   = ¨192.168.30.203¨;

 

my $port   = ¨9090¨;

 

 

my $socket    = new Thrift::Socket($host,$port);

 

my $transport = new Thrift::BufferedTransport($socket,1024,1024);

 

my $protocol  = new Thrift::BinaryProtocol($transport);

 

my $client    = new Hbase::HbaseClient($protocol);  

 

 

eval {  $transport->open () };

 

 

my $tables = $client->getTableNames();

 

foreach my $table (sort @{$tables})

 

{

 

       print "  found {$table}\n";

  }
 

启动HBase thrift服务:

 
  • $ /usr/local/hbase-0.90.5/bin/hbase-daemon.sh start thrift
 

执行t_thrift.pl脚本:

 
  •  

# perl t_thrift.pl  

 

 found {rlog}

 

 found {t}

 

 found {t1}

 

 found {t2}

    found {t3}
 

检查一下输出的结果是您连接的HBase中创建的表对象吗:

 
  •  

$ hbase shell

 

HBase Shell; enter ¨help¨ for list of supported commands.

 

Type "exit" to leave the HBase Shell

 

Version 0.90.5, r1212209, Fri Dec  9 05:40:36 UTC 2011

 

 

hbase(main):001:0> list

 

TABLE                                                                                                                                                                    

 

rlog                                                                                                                                                                    

 

t                                                                                                                                                                        

 

t1                                                                                                                                                                      

 

t2                                                                                                                                                                      

 

t3                                                                                                                                                                      

  5 row(s) in 0.6890 seconds
 

如果没有报错,那么就可以在此基础之上进行扩展,实现通过perl连接HBase并执行各种操作了。

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7607759/viewspace-761360/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7607759/viewspace-761360/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值