2
# Copyright 2009 The Apache Software Foundation
4
# Licensed to the Apache Software Foundation (ASF) under one
5
# or more contributor license agreements. See the NOTICE file
6
# distributed with this work for additional information
7
# regarding copyright ownership. The ASF licenses this file
8
# to you under the Apache License, Version 2.0 (the
9
# "License"); you may not use this file except in compliance
10
# with the License. You may obtain a copy of the License at
12
# http://www.apache.org/licenses/LICENSE-2.0
14
# Unless required by applicable law or agreed to in writing, software
15
# distributed under the License is distributed on an "AS IS" BASIS,
16
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
# See the License for the specific language governing permissions and
18
# limitations under the License.
20
# Script adds a table back to a running hbase.
21
# Currently only works on if table data is in place.
23
# To see usage for this script, run:
25
# ${HBASE_HOME}/bin/hbase org.jruby.Main addtable.rb
28
import org.apache.hadoop.hbase.util.Bytes
29
import org.apache.hadoop.hbase.HConstants
30
import org.apache.hadoop.hbase.HRegionInfo
31
import org.apache.hadoop.hbase.client.HTable
32
import org.apache.hadoop.hbase.client.Delete
33
import org.apache.hadoop.hbase.client.Put
34
import org.apache.hadoop.hbase.client.Scan
35
import org.apache.hadoop.hbase.HTableDescriptor
36
import org.apache.hadoop.hbase.HBaseConfiguration
37
import org.apache.hadoop.hbase.util.FSUtils
38
import org.apache.hadoop.hbase.util.Writables
39
import org.apache.hadoop.fs.Path
40
import org.apache.hadoop.fs.FileSystem
41
import org.apache.commons.logging.LogFactory
46
# Print usage for this script
48
puts 'Usage: %s.rb TABLE_DIR [alternate_tablename]' % NAME
52
# Get configuration to use.
53
c = HBaseConfiguration.new()
55
# Set hadoop filesystem configuration using the hbase.rootdir.
56
# Otherwise, we'll always use localhost though the hbase.rootdir
57
# might be pointing at hdfs location.
58
c.set("fs.default.name", c.get(HConstants::HBASE_DIR))
59
fs = FileSystem.get(c)
61
# Get a logger and a metautils instance.
62
LOG = LogFactory.getLog(NAME)
65
if ARGV.size < 1 || ARGV.size > 2
70
srcdir = fs.makeQualified(Path.new(java.lang.String.new(ARGV[0])))
72
if not fs.exists(srcdir)
73
raise IOError.new("src dir " + srcdir.toString() + " doesn't exist!")
80
raise IOError.new("Not supported yet")
82
# If none provided use dirname
83
tableName = srcdir.getName()
85
HTableDescriptor.isLegalTableName(tableName.to_java_bytes)
87
# Figure locations under hbase.rootdir
88
# Move directories into place; be careful not to overwrite.
89
rootdir = FSUtils.getRootDir(c)
90
tableDir = fs.makeQualified(Path.new(rootdir, tableName))
92
# If a directory currently in place, move it aside.
93
if srcdir.equals(tableDir)
94
LOG.info("Source directory is in place under hbase.rootdir: " + srcdir.toString());
95
elsif fs.exists(tableDir)
96
movedTableName = tableName + "." + java.lang.System.currentTimeMillis().to_s
97
movedTableDir = Path.new(rootdir, java.lang.String.new(movedTableName))
98
LOG.warn("Moving " + tableDir.toString() + " aside as " + movedTableDir.toString());
99
raise IOError.new("Failed move of " + tableDir.toString()) unless fs.rename(tableDir, movedTableDir)
100
LOG.info("Moving " + srcdir.toString() + " to " + tableDir.toString());
101
raise IOError.new("Failed move of " + srcdir.toString()) unless fs.rename(srcdir, tableDir)
104
# Clean mentions of table from .META.
105
# Scan the .META. and remove all lines that begin with tablename
106
LOG.info("Deleting mention of " + tableName + " from .META.")
107
metaTable = HTable.new(c, HConstants::META_TABLE_NAME)
108
tableNameMetaPrefix = tableName + HConstants::META_ROW_DELIMITER.chr
109
scan = Scan.new((tableNameMetaPrefix + HConstants::META_ROW_DELIMITER.chr).to_java_bytes)
110
scanner = metaTable.getScanner(scan)
111
# Use java.lang.String doing compares. Ruby String is a bit odd.
112
tableNameStr = java.lang.String.new(tableName)
113
while (result = scanner.next())
114
rowid = Bytes.toString(result.getRow())
115
rowidStr = java.lang.String.new(rowid)
116
if not rowidStr.startsWith(tableNameMetaPrefix)
117
# Gone too far, break
120
LOG.info("Deleting row from catalog: " + rowid);
121
d = Delete.new(result.getRow())
126
# Now, walk the table and per region, add an entry
127
LOG.info("Walking " + srcdir.toString() + " adding regions to catalog table")
128
statuses = fs.listStatus(srcdir)
129
for status in statuses
130
next unless status.isDir()
131
next if status.getPath().getName() == "compaction.dir"
132
regioninfofile = Path.new(status.getPath(), ".regioninfo")
133
unless fs.exists(regioninfofile)
134
LOG.warn("Missing .regioninfo: " + regioninfofile.toString())
137
is = fs.open(regioninfofile)
138
hri = HRegionInfo.new()
141
# TODO: Need to redo table descriptor with passed table name and then recalculate the region encoded names.
142
p = Put.new(hri.getRegionName())
143
p.add(HConstants::CATALOG_FAMILY, HConstants::REGIONINFO_QUALIFIER, Writables.getBytes(hri))
145
LOG.info("Added to catalog: " + hri.toString())