HBase その1
・HBase と thrift の起動
#su
#cd hbase-0.94.16
#bin/start-hbase.sh
#bin/hbase-daemon.sh start thrift
(停止する時はbin/stop-hbase.sh bin/hbase-daemon.sh stop thrift)
・HBase を read write scan deleteする
<?php
# import setting
$GLOBALS['THRIFT_ROOT'] = '/var/www/thrift';
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' );
# mode = 1:get, 2: write, 3:scan, 4:delete
function hbase_test ($mode, $row, $val, $fam)
{
$server = 'localhost';
$port = 9090;
try {
$socket = new TSocket ($server, $port);
$socket->setRecvTimeout(5000);
$transport = new TBufferedTransport($socket);
$protocol = new TBinaryProtocol($transport);
$client = new HbaseClient($protocol);
$transport->open();
$table = 'tbl';
$mutations = array();
$family = $fam;
$write_value_num = 1;
switch ($mode) {
case 1:
echo "this is get mode\n";
$getrow = $row;
var_dump ($client->getRow ($table, $getrow));
break;
case 2:
echo "this is write mode\n";
# 複数のvalue を一括で更新
for ($i = 0; $i < $write_value_num; $i++) {
$mutations = new Mutation (
array (
'column' => "fam:col1",
'value' => "newval" )
);
}
$writerow = $row;
$client->mutateRow ($table, $writerow, $mutations);
echo "write done!";
break;
case 3:
echo "this is scan mode\n";
$scan = new TScan ();
$scan = $client->scannerOpenWithScan($table, $scan);
# SELECT * FROM TABLE LIMIT 2 に相当
var_dump ($client->scannerGetList ($scan, 2));
break;
case 4:
echo "this is delete mode\n";
$deleterow = $row;
$client->deleteAllRow ($table, $deleterow);
echo "delete is done";
break;
}
} catch (TException $e) {
error_log('TException');
error_log($e);
} catch (Exception $e) {
error_log('Exception');
error_log($e);
}
}
switch ($argv[1]) {
case 1:
# get
hbase_test (1, 'row1', null, null);
break;
case 2:
# write
hbase_test (2, 'newrow', 'newval', null);
break;
case 3:
# scan
hbase_test (3, null, null, null);
break;
case 4:
# put
hbase_test (4, 'newrow', null, null);
break;
case null:
echo "please write like -> php *.php 1\n";
}
?>
(1: read, 2: write, 3:scan, 4:delete)
・C言語で書かれたサーバから上プログラムを動かす
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#define BUF 256
int main (int argc, char *argv)
{
FILE *fp;
char buf[BUF];
char *cmd = "php thrift_hbase.php 1";
if ( ( fp = popen (cmd, "r")) == NULL) {
err(EXIT_FAILURE, "%s", cmdread);
}
while (fgets(buf, BUF, fp) != NULL) {
(void) fputs(buf, stdout);
}
int i;
for (i = 0; i < 100; i++)
printf("buf[%d] is %c\n", i, buf[i]);
(void) pclose(fp);
exit (EXIT_SUCCESS);
}
# ./a.out