~ubuntu-branches/ubuntu/precise/hbase/precise

« back to all changes in this revision

Viewing changes to bin/add_table.rb

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Koch
  • Date: 2010-05-06 14:20:42 UTC
  • Revision ID: james.westby@ubuntu.com-20100506142042-r3hlvgxdcpb8tynl
Tags: upstream-0.20.4+dfsg1
ImportĀ upstreamĀ versionĀ 0.20.4+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright 2009 The Apache Software Foundation
 
3
 
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
 
11
 
12
#     http://www.apache.org/licenses/LICENSE-2.0
 
13
 
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.
 
19
#
 
20
# Script adds a table back to a running hbase.
 
21
# Currently only works on if table data is in place.
 
22
 
23
# To see usage for this script, run: 
 
24
#
 
25
#  ${HBASE_HOME}/bin/hbase org.jruby.Main addtable.rb
 
26
#
 
27
include Java
 
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
 
42
 
 
43
# Name of this script
 
44
NAME = "add_table"
 
45
 
 
46
# Print usage for this script
 
47
def usage
 
48
  puts 'Usage: %s.rb TABLE_DIR [alternate_tablename]' % NAME
 
49
  exit!
 
50
end
 
51
 
 
52
# Get configuration to use.
 
53
c = HBaseConfiguration.new()
 
54
 
 
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)
 
60
 
 
61
# Get a logger and a metautils instance.
 
62
LOG = LogFactory.getLog(NAME)
 
63
 
 
64
# Check arguments
 
65
if ARGV.size < 1 || ARGV.size > 2
 
66
  usage
 
67
end
 
68
 
 
69
# Get cmdline args.
 
70
srcdir = fs.makeQualified(Path.new(java.lang.String.new(ARGV[0])))
 
71
 
 
72
if not fs.exists(srcdir)
 
73
  raise IOError.new("src dir " + srcdir.toString() + " doesn't exist!")
 
74
end
 
75
 
 
76
# Get table name
 
77
tableName = nil
 
78
if ARGV.size > 1
 
79
  tableName = ARGV[1]
 
80
  raise IOError.new("Not supported yet")
 
81
elsif
 
82
  # If none provided use dirname
 
83
  tableName = srcdir.getName()
 
84
end
 
85
HTableDescriptor.isLegalTableName(tableName.to_java_bytes)
 
86
 
 
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))
 
91
 
 
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)
 
102
end
 
103
 
 
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
 
118
    break
 
119
  end
 
120
  LOG.info("Deleting row from catalog: " + rowid);
 
121
  d = Delete.new(result.getRow())
 
122
  metaTable.delete(d)
 
123
end
 
124
scanner.close()
 
125
 
 
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())
 
135
    next
 
136
  end
 
137
  is = fs.open(regioninfofile) 
 
138
  hri = HRegionInfo.new()
 
139
  hri.readFields(is)
 
140
  is.close() 
 
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))
 
144
  metaTable.put(p)
 
145
  LOG.info("Added to catalog: " + hri.toString())
 
146
end