HBASE-16379 [replication] Minor improvement to replication/copy_tables_desc.rb

This commit is contained in:
Esteban Gutierrez 2016-08-08 16:10:29 -07:00
parent 45bb6180a3
commit b3888eadf8
1 changed files with 33 additions and 6 deletions

View File

@ -27,7 +27,6 @@ include Java
import org.apache.commons.logging.LogFactory import org.apache.commons.logging.LogFactory
import org.apache.hadoop.hbase.HBaseConfiguration import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.HConstants import org.apache.hadoop.hbase.HConstants
import org.apache.hadoop.hbase.EmptyWatcher
import org.apache.hadoop.hbase.client.HBaseAdmin import org.apache.hadoop.hbase.client.HBaseAdmin
import org.apache.hadoop.hbase.HTableDescriptor import org.apache.hadoop.hbase.HTableDescriptor
import org.apache.hadoop.conf.Configuration import org.apache.hadoop.conf.Configuration
@ -38,11 +37,32 @@ NAME = "copy_tables_desc"
# Print usage for this script # Print usage for this script
def usage def usage
puts 'Usage: %s.rb master_zookeeper.quorum.peers:clientport:znode_parent slave_zookeeper.quorum.peers:clientport:znode_parent' % NAME puts 'Usage: %s.rb master_zookeeper.quorum.peers:clientport:znode_parent slave_zookeeper.quorum.peers:clientport:znode_parent [table1,table2,table3,...]' % NAME
exit! exit!
end end
if ARGV.size != 2 def copy (src, dst, table)
# verify if table exists in source cluster
begin
t = src.getTableDescriptor(table.to_java_bytes)
rescue org.apache.hadoop.hbase.TableNotFoundException
puts "Source table \"%s\" doesn't exist, skipping." % table
return
end
# verify if table *doesn't* exists in the target cluster
begin
dst.createTable(t)
rescue org.apache.hadoop.hbase.TableExistsException
puts "Destination table \"%s\" exists in remote cluster, skipping." % table
return
end
puts "Schema for table \"%s\" was succesfully copied to remote cluster." % table
end
if ARGV.size < 2 || ARGV.size > 3
usage usage
end end
@ -52,6 +72,8 @@ parts1 = ARGV[0].split(":")
parts2 = ARGV[1].split(":") parts2 = ARGV[1].split(":")
parts3 = ARGV[2].split(",") unless ARGV[2].nil?
c1 = HBaseConfiguration.create() c1 = HBaseConfiguration.create()
c1.set(HConstants::ZOOKEEPER_QUORUM, parts1[0]) c1.set(HConstants::ZOOKEEPER_QUORUM, parts1[0])
c1.set("hbase.zookeeper.property.clientPort", parts1[1]) c1.set("hbase.zookeeper.property.clientPort", parts1[1])
@ -68,11 +90,16 @@ c2.set(HConstants::ZOOKEEPER_ZNODE_PARENT, parts2[2])
connection2 = ConnectionFactory.createConnection(c2) connection2 = ConnectionFactory.createConnection(c2)
admin2 = connection2.getAdmin() admin2 = connection2.getAdmin()
for t in admin1.listTables() if parts3.nil?
admin2.createTable(t) admin1.listTableNames().each do |t|
copy(admin1, admin2, t.nameAsString())
end
else
parts3.each do |t|
copy(admin1, admin2, t)
end
end end
puts "All descriptions were copied"
admin1.close() admin1.close()
admin2.close() admin2.close()
connection1.close() connection1.close()