HBASE-569 DemoClient.php

git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@656511 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2008-05-15 05:26:47 +00:00
parent 100a1616d4
commit 7ddd733236
4 changed files with 238 additions and 4 deletions

View File

@ -27,7 +27,7 @@ Hbase Change Log
alls-well to the master
HBASE-614 Retiring regions is not used; exploit or remove
HBASE-538 Improve exceptions that come out on client-side
HBASE-569 DemoClient.php (Jim R. Wilson via Stack)
Release 0.1.2 - 05/13/2008

View File

@ -66,6 +66,24 @@
reported back to the master.
</description>
</property>
<property>
<name>hbase.regionserver.dns.interface</name>
<value>default</value>
<description>Name of the network interface which a regionserver
should use to determine it's "real" IP address. This lookup
prevents strings like "localhost" and "127.0.0.1" from being
reported back to the master.
</description>
</property>
<property>
<name>hbase.regionserver.dns.interface</name>
<value>default</value>
<description>Name of the network interface which a regionserver
should use to determine it's "real" IP address. This lookup
prevents strings like "localhost" and "127.0.0.1" from being
reported back to the master.
</description>
</property>
<property>
<name>hbase.regionserver.info.port</name>
<value>60030</value>

View File

@ -0,0 +1,217 @@
<?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.
*/
# Change this to match your thrift root
$GLOBALS['THRIFT_ROOT'] = dirname(__FILE__).'/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.
require_once( $GLOBALS['THRIFT_ROOT'].'/packages/Hbase/Hbase.php' );
function printRow( $row, $values ) {
echo( "row: {$row}, cols: \n" );
asort( $values );
foreach ( $values as $k=>$v ) {
echo( " {$k} => {$v}\n" );
}
}
function printEntry( $entry ) {
printRow( $entry->row, $entry->columns );
}
$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();
$t = 'demo_table';
?><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" );
if ( $name == $t ) {
echo( " deleting table: {$name}\n" );
$client->deleteTable( $name );
}
}
#
# Create the demo table with two column families, entry: and unused:
#
$columns = array(
new ColumnDescriptor( array(
'name' => 'entry:',
'maxVersions' => 10
) ),
new ColumnDescriptor( array(
'name' => 'unused:'
) )
);
echo( "creating table: {$t}\n" );
try {
$client->createTable( $t, $columns );
} catch ( AlreadyExists $ae ) {
echo( "WARN: {$ae->message}\n" );
}
echo( "column families in {$t}:\n" );
$descriptors = $client->getColumnDescriptors( $t );
asort( $descriptors );
foreach ( $descriptors as $col ) {
echo( " column: {$col->name}, maxVer: {$col->maxVersions}\n" );
}
#
# Test UTF-8 handling
#
$invalid = "foo-\xfc\xa1\xa1\xa1\xa1\xa1";
$valid = "foo-\xE7\x94\x9F\xE3\x83\x93\xE3\x83\xBC\xE3\x83\xAB";
# non-utf8 is fine for data
$client->put( $t, "foo", "entry:foo", $invalid );
# try empty strings
$client->put( $t, "", "entry:", "" );
# this row name is valid utf8
$client->put( $t, $valid, "entry:foo", $valid );
# non-utf8 is not allowed in row names
try {
$client->put( $t, $invalid, "entry:foo", $invalid );
throw new Exception( "shouldn't get here!" );
} catch ( IOError $e ) {
echo( "expected error: {$e->message}\n" );
}
# Run a scanner on the rows we just created
echo( "Starting scanner...\n" );
$scanner = $client->scannerOpen( $t, "", array( "entry:" ) );
try {
while (true) printEntry( $client->scannerGet( $scanner ) );
} catch ( NotFound $nf ) {
$client->scannerClose( $scanner );
echo( "Scanner finished\n" );
}
#
# Run some operations on a bunch of rows.
#
for ($e=100; $e>=0; $e--) {
# format row keys as "00000" to "00100"
$row = str_pad( $e, 5, '0', STR_PAD_LEFT );
$client->put( $t, $row, "unused:", "DELETE_ME" );
printRow( $row, $client->getRow( $t, $row ) );
$client->deleteAllRow( $t, $row );
$client->put( $t, $row, "entry:num", "0" );
$client->put( $t, $row, "entry:foo", "FOO");
printRow( $row, $client->getRow( $t, $row ) );
$mutations = array(
new Mutation( array(
'column' => 'entry:foo',
'isDelete' => 1
) ),
new Mutation( array(
'column' => 'entry:num',
'value' => '-1'
) ),
);
$client->mutateRow( $t, $row, $mutations );
printRow( $row, $client->getRow( $t, $row ) );
$client->put( $t, $row, "entry:num", $e );
$client->put( $t, $row, "entry:sqr", $e * $e );
printRow( $row, $client->getRow( $t, $row ) );
$mutations = array(
new Mutation( array(
'column' => 'entry:num',
'isDelete' => '-999'
) ),
new Mutation( array(
'column' => 'entry:sqr',
'isDelete' => 1
) ),
);
$client->mutateRowTs( $t, $row, $mutations, 1 ); # shouldn't override latest
printRow( $row, $client->getRow( $t, $row ) );
$versions = $client->getVer( $t, $row, "entry:num", 10 );
echo( "row: {$row}, values: \n" );
foreach ( $versions as $v ) echo( " {$v};\n" );
try {
$client->get( $t, $row, "entry:foo");
throw new Exception ( "shouldn't get here! " );
} catch ( NotFound $nf ) {
# blank
}
}
$columns = array();
foreach ( $client->getColumnDescriptors($t) as $col=>$desc ) $columns[] = $col;
echo( "Starting scanner...\n" );
$scanner = $client->scannerOpenWithStop( $t, "00020", "00040", $columns );
try {
while (true) printEntry( $client->scannerGet( $scanner ) );
} catch ( NotFound $nf ) {
$client->scannerClose( $scanner );
echo( "Scanner finished\n" );
}
$transport->close();
?>
</pre>
</body>
</html>

View File

@ -2,14 +2,13 @@ Hbase Thrift Client Examples
============================
Included in this directory are sample clients of the HBase ThriftServer. They
all perform the same actions but are implemented in C++, Java, and Ruby
all perform the same actions but are implemented in C++, Java, Ruby, and PHP
respectively.
To run/compile this clients, you will first need to install the thrift package
(from http://developers.facebook.com/thrift/) and then run thrift to generate
the language files:
thrift -cpp -java -rb \
thrift -cpp -java -rb -php \
../../../src/java/org/apache/hadoop/hbase/thrift/Hbase.thrift