2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
23 # Change the path here to point to your thrift directory.
24 use lib
'/Users/sergey/Downloads/thrift/lib/perl/lib';
28 use Thrift
::BufferedTransport
;
29 use Thrift
::BinaryProtocol
;
35 my $rowresult = shift;
37 return if (!$rowresult || @
{$rowresult} < 1);
38 # rowresult is presummed to be a Hbase::TRowResult object
40 printf ("row: {%s}, cols: \n", $rowresult->[0]->{row
});
41 my $values = $rowresult->[0]->{columns
};
42 foreach my $key ( sort ( keys %{$values} ) )
44 printf ("{%s} => {%s}\n", $key, $values->{$key}->{value
});
48 my $host = $ARGV[0] || "localhost";
49 my $port = $ARGV[1] || 9090;
51 my $socket = Thrift
::Socket
->new ($host, $port);
52 $socket->setSendTimeout (10000); # Ten seconds (value is in millisec)
53 $socket->setRecvTimeout (20000); # Twenty seconds (value is in millisec)
55 my $transport = Thrift
::BufferedTransport
->new ($socket);
56 my $protocol = Thrift
::BinaryProtocol
->new ($transport);
57 my $client = Hbase
::HbaseClient
->new ($protocol);
64 print "Unable to connect: $@->{message}\n";
68 my $demo_table = "demo_table";
70 print "scanning tables...\n";
73 # Search for all the tables in the HBase DB, return value is an arrayref
75 my $tables = $client->getTableNames();
76 foreach my $table (sort @
{$tables})
78 print " found {$table}\n";
79 # This client will re-create the $demo_table, so we need to drop it first
80 if ($table eq $demo_table)
82 # Before we can drop a table, it has to be disabled first
83 if ($client->isTableEnabled ($table))
85 print " disabling table: {$table}\n";
86 $client->disableTable ($table);
88 # We assume the table has been disabled at this point
89 print " deleting table: {$table}\n";
90 $client->deleteTable ($table);
95 # Create the demo table with two column families, entry: and unused:
98 Hbase
::ColumnDescriptor
->new ( { name
=> "entry:", maxVersions
=> 10 } ),
99 Hbase
::ColumnDescriptor
->new ( { name
=> "unused:" } ),
102 print "creating table: {$demo_table}\n";
104 # This can throw Hbase::IllegalArgument (HASH)
105 $client->createTable ( $demo_table, $columns );
109 die "ERROR: Unable to create table {$demo_table}: $@->{message}\n";
112 print "column families in {$demo_table}:\n";
113 my $descriptors = $client->getColumnDescriptors ($demo_table);
114 foreach my $col (sort keys %{$descriptors})
116 printf (" column: {%s}, maxVer: {%s}\n", $descriptors->{$col}->{name
}, $descriptors->{$col}->{maxVersions
} );
119 my %dummy_attributes = ();
122 # Test UTF-8 handling
124 my $invalid = "foo-\xfc\xa1\xa1\xa1\xa1\xa1";
125 my $valid = "foo-\xE7\x94\x9F\xE3\x83\x93\xE3\x83\xBC\xE3\x83\xAB";
127 # non-utf8 is fine for data
129 my $mutations = [ Hbase
::Mutation
->new ( { column
=> "entry:$key", value
=> $invalid } ) ];
130 $client->mutateRow ( $demo_table, $key, $mutations, %dummy_attributes );
134 $mutations = [ Hbase
::Mutation
->new ( { column
=> "entry:$key", value
=> "" } ) ];
135 $client->mutateRow ( $demo_table, $key, $mutations, %dummy_attributes );
137 # this row name is valid utf8
139 # This is another way to use the Mutation class
140 my $mutation = Hbase
::Mutation
->new ();
141 $mutation->{column
} = "entry:$key";
142 $mutation->{value
} = $valid;
143 $mutations = [ $mutation ];
144 $client->mutateRow ( $demo_table, $key, $mutations, %dummy_attributes );
146 # non-utf8 is not allowed in row names
148 $mutations = [ Hbase
::Mutation
->new ( { column
=> "entry:$key", value
=> $invalid } ) ];
149 # this can throw a TApplicationException (HASH) error
150 $client->mutateRow ($demo_table, $key, $mutations, %dummy_attributes);
151 die ("shouldn't get here!");
155 print "expected error: $@->{message}\n";
159 # Run a scanner on the rows we just created
161 print "Starting scanner...\n";
163 # scannerOpen expects ( table, key, <column descriptors> )
164 # if key is empty, it searches for all entries in the table
165 # if column descriptors is empty, it searches for all column descriptors within the table
166 my $scanner = $client->scannerOpen ( $demo_table, $key, [ "entry:" ], %dummy_attributes );
169 # scannerGet returns an empty arrayref (instead of an undef) to indicate no results
170 my $result = $client->scannerGet ( $scanner );
171 while ( $result && @
{$result} > 0 )
173 printRow
( $result );
174 $result = $client->scannerGet ( $scanner );
177 $client->scannerClose ( $scanner );
178 print "Scanner finished\n";
182 $client->scannerClose ( $scanner );
183 print "Scanner finished\n";
187 # Run some operations on a bunch of rows
189 for (my $e = 100; $e > 0; $e--)
191 # format row keys as "00000" to "00100";
192 my $row = sprintf ("%05d", $e);
194 $mutations = [ Hbase
::Mutation
->new ( { column
=> "unused:", value
=> "DELETE_ME" } ) ];
195 $client->mutateRow ( $demo_table, $row, $mutations, %dummy_attributes );
196 printRow
( $client->getRow ( $demo_table, $row ) );
197 $client->deleteAllRow ( $demo_table, $row );
200 Hbase
::Mutation
->new ( { column
=> "entry:num", value
=> "0" } ),
201 Hbase
::Mutation
->new ( { column
=> "entry:foo", value
=> "FOO" } ),
203 $client->mutateRow ( $demo_table, $row, $mutations, %dummy_attributes );
204 printRow
( $client->getRow ( $demo_table, $row, %dummy_attributes ) );
207 Hbase
::Mutation
->new ( { column
=> "entry:foo", isDelete
=> 1 } ),
208 Hbase
::Mutation
->new ( { column
=> "entry:num", value
=> -1 } ),
210 $client->mutateRow ( $demo_table, $row, $mutations, %dummy_attributes );
211 printRow
( $client->getRow ( $demo_table, $row, %dummy_attributes ) );
214 Hbase
::Mutation
->new ( { column
=> "entry:num", value
=> $e } ),
215 Hbase
::Mutation
->new ( { column
=> "entry:sqr", value
=> $e * $e } ),
217 $client->mutateRow ( $demo_table, $row, $mutations, %dummy_attributes );
218 printRow
( $client->getRow ( $demo_table, $row, %dummy_attributes ) );
221 Hbase
::Mutation
->new ( { column
=> "entry:num", value
=> -999 } ),
222 Hbase
::Mutation
->new ( { column
=> "entry:sqr", isDelete
=> 1 } ),
225 # mutateRowTs => modify the row entry at the specified timestamp (ts)
226 $client->mutateRowTs ( $demo_table, $row, $mutations, 1, %dummy_attributes ); # shouldn't override latest
227 printRow
( $client->getRow ( $demo_table, $row, %dummy_attributes ) );
229 my $versions = $client->getVer ( $demo_table, $row, "entry:num", 10, %dummy_attributes );
230 printf ( "row: {%s}, values: \n", $row );
231 foreach my $v ( @
{$versions} )
233 printf ( " {%s} @ {%s}\n", $v->{value
}, $v->{timestamp
} );
238 my $result = $client->get ( $demo_table, $row, "entry:foo", %dummy_attributes );
240 # Unfortunately, the API returns an empty arrayref instead of undef
241 # to signify a "not found", which makes it slightly inconvenient.
242 die "shouldn't get here!" if ($result && @
{$result} > 0);
244 if (!$result || ($result && @
{$result} < 1))
246 print "expected: {$row} not found in {$demo_table}\n";
251 print "expected error: $@\n";
255 my $column_descriptor = $client->getColumnDescriptors ( $demo_table );
257 foreach my $col ( keys %{$column_descriptor} )
259 my $colname = $column_descriptor->{$col}->{name
};
260 print "column with name: {$colname}\n";
261 push ( @
{$columns}, $colname);
264 print "Starting scanner...\n";
265 $scanner = $client->scannerOpenWithStop ( $demo_table, "00020", "00040", $columns, %dummy_attributes );
268 # scannerGet returns an empty arrayref (instead of an undef) to indicate no results
269 my $result = $client->scannerGet ( $scanner );
270 while ( $result && @
$result > 0 )
272 printRow
( $result );
273 $result = $client->scannerGet ( $scanner );
276 $client->scannerClose ( $scanner );
277 print "Scanner finished\n";
281 $client->scannerClose ( $scanner );
282 print "Scanner finished\n";
285 $transport->close ();