通过前两天的学习,hbase分布式已经在电脑上跑起来了,下一步想通过PHP网页来访问hbase的数据。php的搭建也比较简单,直接通过apt-get install apache2和apt-get install php5就可以搭建好,如果可以通过http://localhost/可以看见网页画面,说明php搭建成功。通过网上的学习,实现的方式是要搭建thrift来访问hbase,以下是搭建的步骤
第一步,通过thrift.apache.org下载0.8.0版本,0.9.0版本和0.94.4的例子搭配有点小问题。首先是编译thrift,我的笔记本在configuration的时候老是报错configure: error: "Error: libcrypto required.",至今没有解决,网上说解决办法是apt-get install libssl-dev, 我一次安装后可以编译过,好像后面又安装了php5-cli,就再也编译不过了,重新安装libssl-dev也不行,最后还算用的公司的电脑编译的thrift,会生成thrift一个文件。
第二步,利用生成的thrift,
thrift --gen php [hbase-root]/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift,会产生hbase.php和hbase_types.php两个文件,这两个文件就是php操作hbase的类,在DemoClient.php使用。 将thrift-0.8.0/lib/php/src拷贝到/var/www/thrift/下面,当然也可以不拷,这个可以在DemoClient.php文件里面可以设定的,再把产生hbase.php和hbase_types.php两个文件拷贝到src文件夹下面的/packages/Hbase/下面,这个也是在DemoClient.php里面设置。 第三步,将hbase-0.94.4/src/examples/thrift/DemoClient.php拷贝到/var/www/下面,修改 $GLOBALS['THRIFT_ROOT'] = '/var/www/thrift/src'; require_once( $GLOBALS['THRIFT_ROOT'].'/packages/Hbase/Hbase.php' ); 改成自己相应的目录就OK了。
第四步 通过hbase启动thrift服务,bin/hbase thrift start
第五步,hbase-0.94.4自带的这个DemoClient.php跑起来有点小问题,需要将一些地方做相关的屏蔽,我最后自己写了一个简单的php,实现的功能就是显示hbase里面所有的表,然后再指定一个table将里面的row全部显示出来,下面是php源码
<?php
/**
* Copyright 2008 The Apache Software Foundation
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
# Instructions:
# 1. Run Thrift to generate the php module HBase
# thrift -php ../../../src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
# 2. Modify the import string below to point to {$THRIFT_HOME}/lib/php/src.
# 3. Execute {php DemoClient.php}. Note that you must use php5 or higher.
# 4. See {$THRIFT_HOME}/lib/php/README for additional help.
# Change this to match your thrift root
$GLOBALS['THRIFT_ROOT'] = '/var/www/thrift/src';
require_once( $GLOBALS['THRIFT_ROOT'].'/Thrift.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php' );
require_once( $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php' );
# According to the thrift documentation, compiled PHP thrift libraries should
# reside under the THRIFT_ROOT/packages directory. If these compiled libraries
# are not present in this directory, move them there from gen-php/.
require_once( $GLOBALS['THRIFT_ROOT'].'/packages/Hbase/Hbase.php' );
function printRow( $rowresult ) {
echo( "row: {$rowresult->row}, cols: \n" );
$values = $rowresult->columns;
asort( $values );
foreach ( $values as $k=>$v ) {
echo( " {$k} => {$v->value}\n" );
}
}
$socket = new TSocket( 'localhost', 9090 );
$socket->setSendTimeout( 10000 ); // Ten seconds (too long for production, but this is just a demo ;)
$socket->setRecvTimeout( 20000 ); // Twenty seconds
$transport = new TBufferedTransport( $socket );
$protocol = new TBinaryProtocol( $transport );
$client = new HbaseClient( $protocol );
$transport->open();
?>
<html>
<head>
<title>DemoClient</title>
</head>
<body>
<pre>
<?php
#
# Scan all tables, look for the demo table and delete it.
#
echo( "scanning tables...\n" );
$tables = $client->getTableNames();
sort( $tables );
foreach ( $tables as $name ) {
echo( " found: {$name}\n" );
}
$tablename = 'yht';
$des = $client->getColumnDescriptors($tablename);
//sort($des);
foreach($des as $col){
echo("column:{$col->name}");
}
echo("\n");
$tartrow = "";
$family = array('age','name','addr','father');
$scanner = $client->scannerOpen($tablename,$tartrow,$family);
while(true)
{
$get_arr = $client->scannerGet($scanner);
if($get_arr == null)
break;
foreach($get_arr as $rowresult)
{
echo("row={$rowresult->row}<br>");
$columns = $rowresult->columns;
foreach($columns as $family_column=>$Tcell)
{
echo("family:column = {$family_column}");
echo("value={$Tcell->value};");
echo("time={$Tcell->timestamp}");
echo("\n");
}
}
}
$transport->close();
?>
</pre>
</body>
</html>
本文详细介绍了如何在PHP环境中搭建Thrift服务,以便通过PHP访问HBase数据。包括Thrift的编译、生成PHP模块、配置环境以及运行示例代码等步骤。
1086

被折叠的 条评论
为什么被折叠?



