HBASE-16379 [replication] Minor improvement to replication/copy_tables_desc.rb
diff --git a/bin/replication/copy_tables_desc.rb b/bin/replication/copy_tables_desc.rb
index bc70031..07b17a86 100644
--- a/bin/replication/copy_tables_desc.rb
+++ b/bin/replication/copy_tables_desc.rb
@@ -27,7 +27,6 @@
import org.apache.commons.logging.LogFactory
import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.HConstants
-import org.apache.hadoop.hbase.EmptyWatcher
import org.apache.hadoop.hbase.client.HBaseAdmin
import org.apache.hadoop.hbase.HTableDescriptor
import org.apache.hadoop.conf.Configuration
@@ -37,11 +36,32 @@
# Print usage for this script
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!
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
end
@@ -51,6 +71,8 @@
parts2 = ARGV[1].split(":")
+parts3 = ARGV[2].split(",") unless ARGV[2].nil?
+
c1 = HBaseConfiguration.create()
c1.set(HConstants::ZOOKEEPER_QUORUM, parts1[0])
c1.set("hbase.zookeeper.property.clientPort", parts1[1])
@@ -65,9 +87,12 @@
admin2 = HBaseAdmin.new(c2)
-for t in admin1.listTables()
- admin2.createTable(t)
+if parts3.nil?
+ admin1.listTableNames().each do |t|
+ copy(admin1, admin2, t.nameAsString())
+ end
+else
+ parts3.each do |t|
+ copy(admin1, admin2, t)
+ end
end
-
-
-puts "All descriptions were copied"