4 # Licensed to the Apache Software Foundation (ASF) under one
5 # or more contributor license agreements. See the NOTICE file
6 # distributed with this work for additional information
7 # regarding copyright ownership. The ASF licenses this file
8 # to you under the Apache License, Version 2.0 (the
9 # "License"); you may not use this file except in compliance
10 # with the License. You may obtain a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
21 # Modify the import string below to point to {$THRIFT_HOME}/lib/rb/lib.
23 # You will need to modify this import string:
24 # TODO: fix rb/php/py examples to actually work, similar to HBASE-3630.
25 $:.push('~/Thrift/thrift-20080411p1/lib/rb/lib')
32 def printRow(rowresult)
33 print "row: #{rowresult.row}, cols: "
34 rowresult.columns.sort.each do |k,v|
35 print "#{k} => #{v.value}; "
50 transport = Thrift::BufferedTransport.new(Thrift::Socket.new(host, port))
51 protocol = Thrift::BinaryProtocol.new(transport)
52 client = Apache::Hadoop::Hbase::Thrift::Hbase::Client.new(protocol)
59 # Scan all tables, look for the demo table and delete it.
61 puts "scanning tables..."
62 client.getTableNames().sort.each do |name|
63 puts " found: #{name}"
65 if (client.isTableEnabled(name))
66 puts " disabling table: #{name}"
67 client.disableTable(name)
69 puts " deleting table: #{name}"
70 client.deleteTable(name)
75 # Create the demo table with two column families, entry: and unused:
78 col = Apache::Hadoop::Hbase::Thrift::ColumnDescriptor.new
82 col = Apache::Hadoop::Hbase::Thrift::ColumnDescriptor.new
86 puts "creating table: #{t}"
88 client.createTable(t, columns)
89 rescue Apache::Hadoop::Hbase::Thrift::AlreadyExists => ae
90 puts "WARN: #{ae.message}"
93 puts "column families in #{t}: "
94 client.getColumnDescriptors(t).sort.each do |key, col|
95 puts " column: #{col.name}, maxVer: #{col.maxVersions}"
101 # Test UTF-8 handling
103 invalid = "foo-\xfc\xa1\xa1\xa1\xa1\xa1"
104 valid = "foo-\xE7\x94\x9F\xE3\x83\x93\xE3\x83\xBC\xE3\x83\xAB";
106 # non-utf8 is fine for data
108 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
109 m.column = "entry:foo"
112 client.mutateRow(t, "foo", mutations, dummy_attributes)
116 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
120 client.mutateRow(t, "", mutations, dummy_attributes)
122 # this row name is valid utf8
124 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
125 m.column = "entry:foo"
128 client.mutateRow(t, valid, mutations, dummy_attributes)
130 # non-utf8 is not allowed in row names
133 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
134 m.column = "entry:foo"
137 client.mutateRow(t, invalid, mutations, dummy_attributes)
138 raise "shouldn't get here!"
139 rescue Apache::Hadoop::Hbase::Thrift::IOError => e
140 puts "expected error: #{e.message}"
143 # Run a scanner on the rows we just created
144 puts "Starting scanner..."
145 scanner = client.scannerOpen(t, "", ["entry:"], dummy_attributes)
148 printRow(client.scannerGet(scanner))
150 rescue Apache::Hadoop::Hbase::Thrift::NotFound => nf
151 client.scannerClose(scanner)
152 puts "Scanner finished"
156 # Run some operations on a bunch of rows.
158 (0..100).to_a.reverse.each do |e|
159 # format row keys as "00000" to "00100"
160 row = format("%0.5d", e)
163 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
165 m.value = "DELETE_ME"
167 client.mutateRow(t, row, mutations, dummy_attributes)
168 printRow(client.getRow(t, row, dummy_attributes))
169 client.deleteAllRow(t, row, dummy_attributes)
172 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
173 m.column = "entry:num"
176 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
177 m.column = "entry:foo"
180 client.mutateRow(t, row, mutations, dummy_attributes)
181 printRow(client.getRow(t, row, dummy_attributes))
184 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
185 m.column = "entry:foo"
188 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
189 m.column = "entry:num"
192 client.mutateRow(t, row, mutations, dummy_attributes)
193 printRow(client.getRow(t, row, dummy_attributes));
196 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
197 m.column = "entry:num"
200 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
201 m.column = "entry:sqr"
204 client.mutateRow(t, row, mutations, dummy_attributes, dummy_attributes)
205 printRow(client.getRow(t, row, dummy_attributes))
208 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
209 m.column = "entry:num"
212 m = Apache::Hadoop::Hbase::Thrift::Mutation.new
213 m.column = "entry:sqr"
216 client.mutateRowTs(t, row, mutations, 1, dummy_attributes) # shouldn't override latest
217 printRow(client.getRow(t, row, dummy_attributes, dummy_attributes));
219 versions = client.getVer(t, row, "entry:num", 10, dummy_attributes)
220 print "row: #{row}, values: "
227 client.get(t, row, "entry:foo", dummy_attributes)
228 raise "shouldn't get here!"
229 rescue Apache::Hadoop::Hbase::Thrift::NotFound => nf
237 client.getColumnDescriptors(t).each do |col, desc|
238 puts "column with name: #{desc.name}"
239 columns << desc.name + ":"
242 puts "Starting scanner..."
243 scanner = client.scannerOpenWithStop(t, "00020", "00040", columns, dummy_attributes)
246 printRow(client.scannerGet(scanner, dummy_attributes))
248 rescue Apache::Hadoop::Hbase::Thrift::NotFound => nf
249 client.scannerClose(scanner)
250 puts "Scanner finished"