diff --git a/hbase-shell/src/main/ruby/hbase/admin.rb b/hbase-shell/src/main/ruby/hbase/admin.rb index 4b4ea24c7be..7765ef03c44 100644 --- a/hbase-shell/src/main/ruby/hbase/admin.rb +++ b/hbase-shell/src/main/ruby/hbase/admin.rb @@ -99,6 +99,15 @@ module Hbase end end + def locate_region(table_name, row_key) + locator = @connection.getRegionLocator(TableName.valueOf(table_name)) + begin + return locator.getRegionLocation(Bytes.toBytesBinary(row_key)) + ensure + locator.close() + end + end + #---------------------------------------------------------------------------------------------- # Requests a cluster balance # Returns true if balancer ran @@ -436,10 +445,13 @@ module Hbase def truncate_preserve(table_name, conf = @conf) h_table = @connection.getTable(TableName.valueOf(table_name)) locator = @connection.getRegionLocator(TableName.valueOf(table_name)) - splits = locator.getAllRegionLocations(). - map{|i| Bytes.toString(i.getRegionInfo().getStartKey)}. - delete_if{|k| k == ""}.to_java :String - locator.close() + begin + splits = locator.getAllRegionLocations(). + map{|i| Bytes.toString(i.getRegionInfo().getStartKey)}. + delete_if{|k| k == ""}.to_java :String + ensure + locator.close() + end table_description = @admin.getTableDescriptor(TableName.valueOf(table_name)) yield 'Disabling table...' if block_given? @@ -674,7 +686,7 @@ module Hbase end elsif format == "replication" #check whether replication is enabled or not - if (!@admin.getConfiguration().getBoolean(org.apache.hadoop.hbase.HConstants::REPLICATION_ENABLE_KEY, + if (!@admin.getConfiguration().getBoolean(org.apache.hadoop.hbase.HConstants::REPLICATION_ENABLE_KEY, org.apache.hadoop.hbase.HConstants::REPLICATION_ENABLE_DEFAULT)) puts("Please enable replication first.") else @@ -686,7 +698,7 @@ module Hbase rSourceString = " SOURCE:" rLoadSink = sl.getReplicationLoadSink() rSinkString << " AgeOfLastAppliedOp=" + rLoadSink.getAgeOfLastAppliedOp().to_s - rSinkString << ", TimeStampsOfLastAppliedOp=" + + rSinkString << ", TimeStampsOfLastAppliedOp=" + (java.util.Date.new(rLoadSink.getTimeStampsOfLastAppliedOp())).toString() rLoadSourceList = sl.getReplicationLoadSourceList() index = 0 @@ -695,7 +707,7 @@ module Hbase rSourceString << " PeerID=" + rLoadSource.getPeerID() rSourceString << ", AgeOfLastShippedOp=" + rLoadSource.getAgeOfLastShippedOp().to_s rSourceString << ", SizeOfLogQueue=" + rLoadSource.getSizeOfLogQueue().to_s - rSourceString << ", TimeStampsOfLastShippedOp=" + + rSourceString << ", TimeStampsOfLastShippedOp=" + (java.util.Date.new(rLoadSource.getTimeStampOfLastShippedOp())).toString() rSourceString << ", Replication Lag=" + rLoadSource.getReplicationLag().to_s index = index + 1 diff --git a/hbase-shell/src/main/ruby/shell.rb b/hbase-shell/src/main/ruby/shell.rb index 7bcc60e5b6c..067111818f5 100644 --- a/hbase-shell/src/main/ruby/shell.rb +++ b/hbase-shell/src/main/ruby/shell.rb @@ -267,6 +267,7 @@ Shell.load_command_group( alter_status alter_async get_table + locate_region ], :aliases => { 'describe' => ['desc'] diff --git a/hbase-shell/src/main/ruby/shell/commands/locate_region.rb b/hbase-shell/src/main/ruby/shell/commands/locate_region.rb new file mode 100644 index 00000000000..b1e8c7bf485 --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/locate_region.rb @@ -0,0 +1,44 @@ +# +# Copyright 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. +# + +module Shell + module Commands + class LocateRegion < Command + def help + return <<-EOF +Locate the region given a table name and a row-key + + hbase> locate_region 'tableName', 'key0' +EOF + end + + def command(table, row_key) + now = Time.now + + region_location = admin.locate_region(table, row_key) + hri = region_location.getRegionInfo() + + formatter.header([ "HOST", "REGION" ]) + formatter.row([region_location.getHostnamePort(), hri.toString()]) + formatter.footer(now, 1) + end + end + end +end