HBASE-26792 Implement ScanInfo#toString (#4153)
[hbase.git] / hbase-examples / src / main / php / DemoClient.php
blob03491500faa810ddf44bf9050d01f885e6035245
1 <?php
2 /**
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;
58 use Hbase\Mutation;
61 function printRow( $rowresult ) {
62 echo( "row: {$rowresult->row}, cols: \n" );
63 $values = $rowresult->columns;
64 asort( $values );
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 );
77 $transport->open();
79 $t = 'demo_table';
81 ?><html>
82 <head>
83 <title>DemoClient</title>
84 </head>
85 <body>
86 <pre>
87 <?php
90 # Scan all tables, look for the demo table and delete it.
92 echo( "scanning tables...\n" );
93 $tables = $client->getTableNames();
94 sort( $tables );
95 foreach ( $tables as $name ) {
96 echo( " found: {$name}\n" );
97 if ( $name == $t ) {
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:
110 $columns = array(
111 new ColumnDescriptor( array(
112 'name' => 'entry:',
113 'maxVersions' => 10
114 ) ),
115 new ColumnDescriptor( array(
116 'name' => 'unused:'
120 echo( "creating table: {$t}\n" );
121 try {
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
141 $mutations = array(
142 new Mutation( array(
143 'column' => 'entry:foo',
144 'value' => $invalid
145 ) ),
147 $client->mutateRow( $t, "foo", $mutations, $dummy_attributes );
149 # try empty strings
150 $mutations = array(
151 new Mutation( array(
152 'column' => 'entry:',
153 'value' => ""
154 ) ),
156 $client->mutateRow( $t, "", $mutations, $dummy_attributes );
158 # this row name is valid utf8
159 $mutations = array(
160 new Mutation( array(
161 'column' => 'entry:foo',
162 'value' => $valid
163 ) ),
165 $client->mutateRow( $t, $valid, $mutations, $dummy_attributes );
167 # non-utf8 is not allowed in row names
168 try {
169 $mutations = array(
170 new Mutation( array(
171 'column' => 'entry:foo',
172 'value' => $invalid
173 ) ),
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 );
184 try {
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 );
199 $mutations = array(
200 new Mutation( array(
201 'column' => 'unused:',
202 'value' => "DELETE_ME"
203 ) ),
205 $client->mutateRow( $t, $row, $mutations, $dummy_attributes );
206 printRow( $client->getRow( $t, $row, $dummy_attributes ));
207 $client->deleteAllRow( $t, $row, $dummy_attributes );
209 $mutations = array(
210 new Mutation( array(
211 'column' => 'entry:num',
212 'value' => "0"
213 ) ),
214 new Mutation( array(
215 'column' => 'entry:foo',
216 'value' => "FOO"
217 ) ),
219 $client->mutateRow( $t, $row, $mutations, $dummy_attributes );
220 printRow( $client->getRow( $t, $row, $dummy_attributes ));
222 $mutations = array(
223 new Mutation( array(
224 'column' => 'entry:foo',
225 'isDelete' => 1
226 ) ),
227 new Mutation( array(
228 'column' => 'entry:num',
229 'value' => '-1'
230 ) ),
232 $client->mutateRow( $t, $row, $mutations, $dummy_attributes );
233 printRow( $client->getRow( $t, $row, $dummy_attributes ) );
235 $mutations = array(
236 new Mutation( array(
237 'column' => "entry:num",
238 'value' => $e
239 ) ),
240 new Mutation( array(
241 'column' => "entry:sqr",
242 'value' => $e * $e
243 ) ),
245 $client->mutateRow( $t, $row, $mutations, $dummy_attributes );
246 printRow( $client->getRow( $t, $row, $dummy_attributes ));
248 $mutations = array(
249 new Mutation( array(
250 'column' => 'entry:num',
251 'value' => '-999'
252 ) ),
253 new Mutation( array(
254 'column' => 'entry:sqr',
255 'isDelete' => 1
256 ) ),
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" );
265 try {
266 $client->get( $t, $row, "entry:foo", $dummy_attributes );
267 throw new Exception ( "shouldn't get here! " );
268 } catch ( NotFound $nf ) {
269 # blank
274 $columns = array();
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 );
282 try {
283 while (true) printRow( $client->scannerGet( $scanner ) );
284 } catch ( NotFound $nf ) {
285 $client->scannerClose( $scanner );
286 echo( "Scanner finished\n" );
289 $transport->close();
292 </pre>
293 </body>
294 </html>