通过thrift0.8.0访问hbase0.94.4

通过前两天的学习,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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python中使用Thrift连接HBase,您需要按照以下步骤进行设置: 1. 安装所需的依赖项: 您需要安装`thrift`和`happybase`这两个Python库。可以使用以下命令进行安装: ```bash pip install thrift happybase ``` 2. 生成HBaseThrift代码: 使用Thrift工具生成HBaseThrift代码。您可以使用以下命令: ```bash thrift -r --gen py hbase.thrift ``` 这将生成Python的Thrift代码文件。 3. 创建HBase连接: 在Python脚本中,您需要首先创建一个HBase连接。示例代码如下: ```python import happybase connection = happybase.Connection(host='localhost', port=9090) ``` 4. 执行HBase操作: 在创建了HBase连接之后,您可以使用`connection`对象执行各种HBase操作,例如创建表、插入数据、获取数据等。以下是一些示例代码: - 创建表: ```python connection.create_table( 'mytable', { 'cf': dict(max_versions=10), } ) ``` - 插入数据: ```python table = connection.table('mytable') table.put( b'row1', { b'cf:col1': b'value1', b'cf:col2': b'value2', } ) ``` - 获取数据: ```python table = connection.table('mytable') row = table.row(b'row1') print(row) ``` - 删除数据: ```python table = connection.table('mytable') table.delete(b'row1') ``` 这只是一些示例代码,您可以根据需要使用其他HappyBase方法来执行更多操作。 5. 关闭连接: 当您完成HBase操作后,记得关闭连接以释放资源: ```python connection.close() ``` 请注意,为了成功执行这些操作,您需要确保HBase正在运行并且在指定的主机和端口上进行监听。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值