Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions hbase-shell/src/main/ruby/hbase/admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,48 @@ def move(encoded_region_name, server = nil)
@admin.move(encoded_region_name.to_java_bytes, server ? server.to_java_bytes : nil)
end

#----------------------------------------------------------------------------------------------
# Move regions of a table
def move_table_to_servers(table_name, host_or_servers = nil)
if table_name == nil
$stdout.puts "table_name can NOT be NULL"
return
end

table_regions = @admin.getRegions(TableName.valueOf(table_name))
if table_regions == nil || table_regions.size == 0
$stdout.puts "no regions found for table #{table_name}"
return
end
region_count = table_regions.size

if host_or_servers.nil? || host_or_servers.empty?
region_servers = getRegionServers
server_name_strs = []
for server in region_servers
server_name_strs << server.getServerName()
end
host_or_servers = server_name_strs
end

# If a string is passed, convert it to an array
_host_or_servers = host_or_servers.is_a?(Array) ?
host_or_servers :
java.util.Arrays.asList(host_or_servers)
server_names = getServerNames(_host_or_servers, true)

index = 0
while index < region_count
encoded_region_name = table_regions[index].getEncodedName
server_name = server_names[index % server_names.size].getServerName
index += 1
$stdout.puts "start moving #{index} / #{region_count} region #{encoded_region_name} of table : #{table_name} to server : #{server_name}"
# move one region to the server
@admin.move(Bytes.toBytes(encoded_region_name), Bytes.toBytes(server_name))
end
$stdout.puts "finish moving all #{region_count} regions"
end

#----------------------------------------------------------------------------------------------
# Merge two regions
def merge_region(region_a_name, region_b_name, force)
Expand Down
1 change: 1 addition & 0 deletions hbase-shell/src/main/ruby/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def help_footer
flush
major_compact
move
move_table_to_servers
split
merge_region
unassign
Expand Down
45 changes: 45 additions & 0 deletions hbase-shell/src/main/ruby/shell/commands/move_table_to_servers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
#
# 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 MoveTableToServers < Command
def help
return <<-EOF
Move all regions of the table to a list of regionservers. All regions will be moved to the
given regionservers evenly if the 'SERVER_NAME' or list is specified, or to all regionservers
evenly if 'SERVER_NAME' or list is absent

NOTE:
A server name is its host, port plus startcode. For example:
host187.example.com,60020,1289493121758
Examples:

hbase> move_table_to_servers 'TABLENAME', ['SERVER_NAME1','SERVER_NAME2']
hbase> move_table_to_servers 'TABLENAME', 'SERVER_NAME'
hbase> move_table_to_servers 'TABLENAME'
EOF
end

def command(table_name, server_names = nil)
admin.move_table_to_servers(table_name, server_names)
end
end
end
end
10 changes: 10 additions & 0 deletions hbase-shell/src/test/ruby/hbase/admin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ def teardown

#-------------------------------------------------------------------------------

define_test 'move table to regionserver' do
command(:move_table_to_servers, @test_name)
server_name = admin.getServerNames([], true)[0].getServerName()
command(:move_table_to_servers, @test_name, server_name)
command(:move_table_to_servers, @test_name, [server_name])
puts "move_table_to_servers success!"
end

#-------------------------------------------------------------------------------

define_test "create should fail with non-string table names" do
assert_raise(ArgumentError) do
command(:create, 123, 'xxx')
Expand Down