3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
20 # Script to recreate all tables from one cluster to another
21 # To see usage for this script, run:
23 # ${HBASE_HOME}/bin/hbase org.jruby.Main copy_tables_desc.rb
27 java_import org.apache.hadoop.conf.Configuration
28 java_import org.apache.hadoop.hbase.HBaseConfiguration
29 java_import org.apache.hadoop.hbase.HConstants
30 java_import org.apache.hadoop.hbase.HTableDescriptor
31 java_import org.apache.hadoop.hbase.TableName
32 java_import org.apache.hadoop.hbase.client.ConnectionFactory
33 java_import org.apache.hadoop.hbase.client.HBaseAdmin
34 java_import org.slf4j.LoggerFactory
37 NAME = 'copy_tables_desc'.freeze
39 # Print usage for this script
41 puts format('Usage: %s.rb master_zookeeper.quorum.peers:clientport:znode_parent slave_zookeeper.quorum.peers:clientport:znode_parent [table1,table2,table3,...]', NAME)
45 def copy(src, dst, table)
46 # verify if table exists in source cluster
48 t = src.getTableDescriptor(TableName.valueOf(table))
49 rescue org.apache.hadoop.hbase.TableNotFoundException
50 puts format("Source table \"%s\" doesn't exist, skipping.", table)
54 # verify if table *doesn't* exists in the target cluster
57 rescue org.apache.hadoop.hbase.TableExistsException
58 puts format('Destination table "%s" exists in remote cluster, skipping.', table)
62 puts format('Schema for table "%s" was succesfully copied to remote cluster.', table)
65 usage if ARGV.size < 2 || ARGV.size > 3
67 LOG = LoggerFactory.getLogger(NAME)
69 parts1 = ARGV[0].split(':')
71 parts2 = ARGV[1].split(':')
73 parts3 = ARGV[2].split(',') unless ARGV[2].nil?
75 c1 = HBaseConfiguration.create
76 c1.set(HConstants::ZOOKEEPER_QUORUM, parts1[0])
77 c1.set('hbase.zookeeper.property.clientPort', parts1[1])
78 c1.set(HConstants::ZOOKEEPER_ZNODE_PARENT, parts1[2])
80 connection1 = ConnectionFactory.createConnection(c1)
81 admin1 = connection1.getAdmin
83 c2 = HBaseConfiguration.create
84 c2.set(HConstants::ZOOKEEPER_QUORUM, parts2[0])
85 c2.set('hbase.zookeeper.property.clientPort', parts2[1])
86 c2.set(HConstants::ZOOKEEPER_ZNODE_PARENT, parts2[2])
88 connection2 = ConnectionFactory.createConnection(c2)
89 admin2 = connection2.getAdmin
92 admin1.listTableNames.each do |t|
93 copy(admin1, admin2, t.nameAsString)
97 copy(admin1, admin2, t)