From 2b4703f12247019d0984e1e9a68485b76895ae3c Mon Sep 17 00:00:00 2001 From: libisthanks Date: Thu, 23 Nov 2017 15:24:22 +0800 Subject: [PATCH] HBASE-19336:Improve rsgroup to allow assign all tables within a specified namespace from one group to another --- hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb | 48 +++++++++++++++++++--- .../shell/commands/move_servers_tables_rsgroup.rb | 2 +- .../ruby/shell/commands/move_tables_rsgroup.rb | 2 +- .../src/test/ruby/shell/rsgroup_shell_test.rb | 13 +++++- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb b/hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb index befed01..aa5c94d 100644 --- a/hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb +++ b/hbase-shell/src/main/ruby/hbase/rsgroup_admin.rb @@ -26,6 +26,7 @@ module Hbase include HBaseConstants def initialize(connection) + @connection = connection @admin = org.apache.hadoop.hbase.rsgroup.RSGroupAdminClient.new(connection) end @@ -76,11 +77,20 @@ module Hbase end #-------------------------------------------------------------------------- - # move server to a group + # move tables to a group def move_tables(dest, *args) tables = java.util.HashSet.new args[0].each do |s| - tables.add(org.apache.hadoop.hbase.TableName.valueOf(s)) + if(isNamespace?(s)) + namespace_name = s[1...s.length] + raise(ArgumentError, "Can't find a namespace: #{namespace_name}") unless namespace_exists?(namespace_name) + tablelist = @connection.getAdmin.listTableNamesByNamespace(namespace_name).map { |t| t.getNameAsString() } + tablelist.each do |table| + tables.add(org.apache.hadoop.hbase.TableName.valueOf(table)) + end + else + tables.add(org.apache.hadoop.hbase.TableName.valueOf(s)) + end end @admin.moveTables(tables, dest) end @@ -89,7 +99,7 @@ module Hbase # get group of server def get_rsgroup_of_server(server) res = @admin.getRSGroupOfServer( - org.apache.hadoop.hbase.net.Address.fromString(server) + org.apache.hadoop.hbase.net.Address.fromString(server) ) raise(ArgumentError, 'Server has no group: ' + server) if res.nil? res @@ -99,7 +109,7 @@ module Hbase # get group of table def get_rsgroup_of_table(table) res = @admin.getRSGroupInfoOfTable( - org.apache.hadoop.hbase.TableName.valueOf(table) + org.apache.hadoop.hbase.TableName.valueOf(table) ) raise(ArgumentError, 'Table has no group: ' + table) if res.nil? res @@ -114,9 +124,35 @@ module Hbase servers.add(org.apache.hadoop.hbase.net.Address.fromString(s)) end args[1].each do |t| - tables.add(org.apache.hadoop.hbase.TableName.valueOf(t)) + if(isNamespace?(t)) + namespace_name = t[1...t.length] + raise(ArgumentError, "Can't find a namespace: #{namespace_name}") unless namespace_exists?(namespace_name) + tablelist = @connection.getAdmin.listTableNamesByNamespace(namespace_name).map { |table| table.getNameAsString() } + tablelist.each do |table| + tables.add(org.apache.hadoop.hbase.TableName.valueOf(table)) + end + else + tables.add(org.apache.hadoop.hbase.TableName.valueOf(t)) + end end @admin.moveServersAndTables(servers, tables, dest) end + + # check given table_name is namespace + def isNamespace?(table_name) + table_name.start_with?('@') + end + + # Does Namespace exist + def namespace_exists?(namespace_name) + namespaceDesc = @connection.getAdmin.getNamespaceDescriptor(namespace_name) + if(namespaceDesc == nil) + return false + else + return true + end + end + + end -end +end \ No newline at end of file diff --git a/hbase-shell/src/main/ruby/shell/commands/move_servers_tables_rsgroup.rb b/hbase-shell/src/main/ruby/shell/commands/move_servers_tables_rsgroup.rb index 1789a35..2eaabe4 100644 --- a/hbase-shell/src/main/ruby/shell/commands/move_servers_tables_rsgroup.rb +++ b/hbase-shell/src/main/ruby/shell/commands/move_servers_tables_rsgroup.rb @@ -24,7 +24,7 @@ Reassign RegionServers and Tables from one group to another. Example: - hbase> move_servers_tables_rsgroup 'dest',['server1:port','server2:port'],['table1','table2'] + hbase> move_servers_tables_rsgroup 'dest',['server1:port','server2:port'],['table1','table2','@ns'] EOF end diff --git a/hbase-shell/src/main/ruby/shell/commands/move_tables_rsgroup.rb b/hbase-shell/src/main/ruby/shell/commands/move_tables_rsgroup.rb index ff644d1..e444ded 100644 --- a/hbase-shell/src/main/ruby/shell/commands/move_tables_rsgroup.rb +++ b/hbase-shell/src/main/ruby/shell/commands/move_tables_rsgroup.rb @@ -24,7 +24,7 @@ Reassign tables from one RegionServer group to another. Example: - hbase> move_tables_rsgroup 'dest',['table1','table2'] + hbase> move_tables_rsgroup 'dest',['table1','table2','@ns'] EOF end diff --git a/hbase-shell/src/test/ruby/shell/rsgroup_shell_test.rb b/hbase-shell/src/test/ruby/shell/rsgroup_shell_test.rb index 8c33459..f4b57b9 100644 --- a/hbase-shell/src/test/ruby/shell/rsgroup_shell_test.rb +++ b/hbase-shell/src/test/ruby/shell/rsgroup_shell_test.rb @@ -33,6 +33,11 @@ module Hbase define_test 'Test Basic RSGroup Commands' do group_name = 'test_group' table_name = 'test_table' + namespace_name = 'test_namespace' + ns_table_name = 'test_namespace:test_table' + + @shell.command('create_namespace', namespace_name) + @shell.command('create', ns_table_name, 'f') @shell.command('create', table_name, 'f') @@ -62,12 +67,18 @@ module Hbase [table_name]) assert_equal(1, @rsgroup_admin.getRSGroupInfo(group_name).getTables.count) + @shell.command('move_tables_rsgroup', + group_name, + ['@test_namespace']) + assert_equal(2, @rsgroup_admin.getRSGroupInfo(group_name).getTables.count) + group = @hbase.rsgroup_admin.get_rsgroup(group_name) assert_not_nil(group) assert_equal(1, group.getServers.count) - assert_equal(1, group.getTables.count) + assert_equal(2, group.getTables.count) assert_equal(hostPortStr, group.getServers.iterator.next.toString) assert_equal(table_name, group.getTables.iterator.next.toString) + assert_equal(ns_table_name, group.getTables.iterator.next.toString) assert_equal(2, @hbase.rsgroup_admin.list_rs_groups.count) -- 1.9.3 (Apple Git-50)