3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 # See {$THRIFT_HOME}/lib/php/README for additional help.
22 # Change this to match your thrift root.
23 $GLOBALS['THRIFT_ROOT'] = '/Users/irubin/Thrift/thrift-20080411p1/lib/php/src';
25 require_once( $GLOBALS['THRIFT_ROOT'].'/Thrift.php' );
27 # Something is wrong with this. Is this the PHP way of doing things?
28 # Old versions of thrift seemingly worked with just a couple includes.
29 require_once( $GLOBALS['THRIFT_ROOT'].'/type/TMessageType.php' );
30 require_once( $GLOBALS['THRIFT_ROOT'].'/type/TType.php' );
31 require_once( $GLOBALS['THRIFT_ROOT'].'/exception/TException.php' );
32 require_once( $GLOBALS['THRIFT_ROOT'].'/factory/TStringFuncFactory.php' );
33 require_once( $GLOBALS['THRIFT_ROOT'].'/stringfunc/TStringFunc.php' );
34 require_once( $GLOBALS['THRIFT_ROOT'].'/stringfunc/Core.php' );
35 require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php' );
36 require_once( $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php' );
37 require_once( $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php' );
39 # According to the thrift documentation, compiled PHP thrift libraries should
40 # reside under the THRIFT_ROOT/packages directory. Copy them there from gen-php/.
41 if (mkdir($GLOBALS['THRIFT_ROOT'].'/packages/Hbase', 0770, true)) {
42 $files = scandir('gen-php/Hbase');
43 foreach ($files as $file) {
44 if (preg_match("/.*php$/", $file)) {
45 copy("./gen-php/Hbase/$file", $GLOBALS['THRIFT_ROOT']."/packages/Hbase/$file");
50 require_once( $GLOBALS['THRIFT_ROOT'].'/packages/Hbase/Hbase.php' );
51 require_once( $GLOBALS['THRIFT_ROOT'].'/packages/Hbase/Types.php' );
53 use Thrift\Transport\TSocket
;
54 use Thrift\Transport\TBufferedTransport
;
55 use Thrift\Protocol\TBinaryProtocol
;
56 use Hbase\HbaseClient
;
57 use Hbase\ColumnDescriptor
;
61 function printRow( $rowresult ) {
62 echo( "row: {$rowresult->row}, cols: \n" );
63 $values = $rowresult->columns
;
65 foreach ( $values as $k=>$v ) {
66 echo( " {$k} => {$v->value}\n" );
70 $socket = new TSocket( 'localhost', 9090 );
71 $socket->setSendTimeout( 10000 ); // Ten seconds (too long for production, but this is just a demo ;)
72 $socket->setRecvTimeout( 20000 ); // Twenty seconds
73 $transport = new TBufferedTransport( $socket );
74 $protocol = new TBinaryProtocol( $transport );
75 $client = new HbaseClient( $protocol );
83 <title
>DemoClient
</title
>
90 # Scan all tables, look for the demo table and delete it.
92 echo( "scanning tables...\n" );
93 $tables = $client->getTableNames();
95 foreach ( $tables as $name ) {
96 echo( " found: {$name}\n" );
98 if ($client->isTableEnabled( $name )) {
99 echo( " disabling table: {$name}\n");
100 $client->disableTable( $name );
102 echo( " deleting table: {$name}\n" );
103 $client->deleteTable( $name );
108 # Create the demo table with two column families, entry: and unused:
111 new ColumnDescriptor( array(
115 new ColumnDescriptor( array(
120 echo( "creating table: {$t}\n" );
122 $client->createTable( $t, $columns );
123 } catch ( AlreadyExists
$ae ) {
124 echo( "WARN: {$ae->message}\n" );
127 echo( "column families in {$t}:\n" );
128 $descriptors = $client->getColumnDescriptors( $t );
129 asort( $descriptors );
130 foreach ( $descriptors as $col ) {
131 echo( " column: {$col->name}, maxVer: {$col->maxVersions}\n" );
133 $dummy_attributes = array();
135 # Test UTF-8 handling
137 $invalid = "foo-\xfc\xa1\xa1\xa1\xa1\xa1";
138 $valid = "foo-\xE7\x94\x9F\xE3\x83\x93\xE3\x83\xBC\xE3\x83\xAB";
140 # non-utf8 is fine for data
143 'column' => 'entry:foo',
147 $client->mutateRow( $t, "foo", $mutations, $dummy_attributes );
152 'column' => 'entry:',
156 $client->mutateRow( $t, "", $mutations, $dummy_attributes );
158 # this row name is valid utf8
161 'column' => 'entry:foo',
165 $client->mutateRow( $t, $valid, $mutations, $dummy_attributes );
167 # non-utf8 is not allowed in row names
171 'column' => 'entry:foo',
175 $client->mutateRow( $t, $invalid, $mutations, $dummy_attributes );
176 throw new Exception( "shouldn't get here!" );
177 } catch ( IOError
$e ) {
178 echo( "expected error: {$e->message}\n" );
181 # Run a scanner on the rows we just created
182 echo( "Starting scanner...\n" );
183 $scanner = $client->scannerOpen( $t, "", array( "entry:" ), $dummy_attributes );
185 while (true) printRow( $client->scannerGet( $scanner ) );
186 } catch ( NotFound
$nf ) {
187 $client->scannerClose( $scanner );
188 echo( "Scanner finished\n" );
192 # Run some operations on a bunch of rows.
194 for ($e=100; $e>=0; $e--) {
196 # format row keys as "00000" to "00100"
197 $row = str_pad( $e, 5, '0', STR_PAD_LEFT
);
201 'column' => 'unused:',
202 'value' => "DELETE_ME"
205 $client->mutateRow( $t, $row, $mutations, $dummy_attributes );
206 printRow( $client->getRow( $t, $row, $dummy_attributes ));
207 $client->deleteAllRow( $t, $row, $dummy_attributes );
211 'column' => 'entry:num',
215 'column' => 'entry:foo',
219 $client->mutateRow( $t, $row, $mutations, $dummy_attributes );
220 printRow( $client->getRow( $t, $row, $dummy_attributes ));
224 'column' => 'entry:foo',
228 'column' => 'entry:num',
232 $client->mutateRow( $t, $row, $mutations, $dummy_attributes );
233 printRow( $client->getRow( $t, $row, $dummy_attributes ) );
237 'column' => "entry:num",
241 'column' => "entry:sqr",
245 $client->mutateRow( $t, $row, $mutations, $dummy_attributes );
246 printRow( $client->getRow( $t, $row, $dummy_attributes ));
250 'column' => 'entry:num',
254 'column' => 'entry:sqr',
258 $client->mutateRowTs( $t, $row, $mutations, 1, $dummy_attributes ); # shouldn't override latest
259 printRow( $client->getRow( $t, $row, $dummy_attributes ) );
261 $versions = $client->getVer( $t, $row, "entry:num", 10, $dummy_attributes );
262 echo( "row: {$row}, values: \n" );
263 foreach ( $versions as $v ) echo( " {$v->value};\n" );
266 $client->get( $t, $row, "entry:foo", $dummy_attributes );
267 throw new Exception ( "shouldn't get here! " );
268 } catch ( NotFound
$nf ) {
275 foreach ( $client->getColumnDescriptors($t) as $col=>$desc ) {
276 echo("column with name: {$desc->name}\n");
277 $columns[] = $desc->name
.":";
280 echo( "Starting scanner...\n" );
281 $scanner = $client->scannerOpenWithStop( $t, "00020", "00040", $columns, $dummy_attributes );
283 while (true) printRow( $client->scannerGet( $scanner ) );
284 } catch ( NotFound
$nf ) {
285 $client->scannerClose( $scanner );
286 echo( "Scanner finished\n" );