diff --git a/hbase-shell/src/main/ruby/hbase/admin.rb b/hbase-shell/src/main/ruby/hbase/admin.rb index b6e5a22ed650..bf5ebbe925a2 100644 --- a/hbase-shell/src/main/ruby/hbase/admin.rb +++ b/hbase-shell/src/main/ruby/hbase/admin.rb @@ -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) diff --git a/hbase-shell/src/main/ruby/shell.rb b/hbase-shell/src/main/ruby/shell.rb index a8e58ed6560b..0e8b1ee75e24 100644 --- a/hbase-shell/src/main/ruby/shell.rb +++ b/hbase-shell/src/main/ruby/shell.rb @@ -339,6 +339,7 @@ def help_footer flush major_compact move + move_table_to_servers split merge_region unassign diff --git a/hbase-shell/src/main/ruby/shell/commands/move_table_to_servers.rb b/hbase-shell/src/main/ruby/shell/commands/move_table_to_servers.rb new file mode 100644 index 000000000000..e504d01e5e13 --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/move_table_to_servers.rb @@ -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 diff --git a/hbase-shell/src/test/ruby/hbase/admin_test.rb b/hbase-shell/src/test/ruby/hbase/admin_test.rb index 959159c2031b..4c2995d33202 100644 --- a/hbase-shell/src/test/ruby/hbase/admin_test.rb +++ b/hbase-shell/src/test/ruby/hbase/admin_test.rb @@ -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')