首页  >  文章  >  数据库  >  Hadoop集群间的hbase数据迁移

Hadoop集群间的hbase数据迁移

WBOY
WBOY原创
2016-06-07 17:58:16865浏览

在日常的使用过程中,可能经常需要将一个集群中hbase的数据迁移到或者拷贝到另外一个集群中,这时候,可能会出很多问题 以下是我在处理的过程中的一些做法和处理方式。 前提,两个hbase的版本一直,否则可能出现不可预知的问题,造成数据迁移失败 当两个集群

在日常的使用过程中,可能经常需要将一个集群中hbase的数据迁移到或者拷贝到另外一个集群中,这时候,可能会出很多问题

以下是我在处理的过程中的一些做法和处理方式。

前提,两个hbase的版本一直,否则可能出现不可预知的问题,造成数据迁移失败

当两个集群不能通讯的时候,可以先将数据所在集群中hbase的数据文件拷贝到本地

具体做法如下:

在Hadoop目录下执行如下命令,拷贝到本地文件。

bin/Hadoop fs -copyToLocal /hbase/tab_keywordflow /home/test/xiaochenbak

然后你懂得,将文件拷贝到你需要的你需要迁移到的那个集群中,目录是你的表的目录,

如果这个集群中也有对应的表文件,那么删除掉,然后拷贝。

/bin/Hadoop fs -rmr /hbase/tab_keywordflow

/bin/Hadoop fs -copyFromLocal /home/other/xiaochenbak /hbase/tab_keywordflow

此时的/home/other/xiaochenbak为你要迁移到数据的集群。

重置该表在.META.表中的分区信息

bin/hbase org.jruby.Main /home/other/hbase/bin/add_table.rb /hbase/tab_keywordflow

/home/other/hbase/bin/add_table.rb为ruby脚本,可以执行,脚本内容如下:另存为add_table.rb即可


# Copyright 2009 The Apache Software Foundation 

# Licensed to the Apache Software Foundation (ASF) under one 
# or more contributor license agreements.  See the NOTICE file 
# distributed with this work for additional information 
# regarding copyright ownership.  The ASF licenses this file 
# to you under the Apache License, Version 2.0 (the 
# "License"); you may not use this file except in compliance 
# with the License.  You may obtain a copy of the License at 

#     http://www.apache.org/licenses/LICENSE-2.0  

# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License. 

# Script adds a table back to a running hbase. 
# Currently only works on if table data is in place. 

# To see usage for this script, run: 

#  ${HBASE_HOME}/bin/hbase org.jruby.Main addtable.rb 

include Java 
import org.apache.Hadoop.hbase.util.Bytes 
import org.apache.Hadoop.hbase.HConstants 
import org.apache.Hadoop.hbase.regionserver.HRegion 
import org.apache.Hadoop.hbase.HRegionInfo 
import org.apache.Hadoop.hbase.client.HTable 
import org.apache.Hadoop.hbase.client.Delete 
import org.apache.Hadoop.hbase.client.Put 
import org.apache.Hadoop.hbase.client.Scan 
import org.apache.Hadoop.hbase.HTableDescriptor 
import org.apache.Hadoop.hbase.HBaseConfiguration 
import org.apache.Hadoop.hbase.util.FSUtils 
import org.apache.Hadoop.hbase.util.Writables 
import org.apache.Hadoop.fs.Path 
import org.apache.Hadoop.fs.FileSystem 
import org.apache.commons.logging.LogFactory 
 
# Name of this script 
NAME = "add_table" 
 
# Print usage for this script 
def usage 
  puts 'Usage: %s.rb TABLE_DIR [alternate_tablename]' % NAME 
  exit! 
end 
 
# Get configuration to use. 
c = HBaseConfiguration.new() 
 
# Set Hadoop filesystem configuration using the hbase.rootdir. 
# Otherwise, we'll always use localhost though the hbase.rootdir 
# might be pointing at hdfs location. 
c.set("fs.default.name", c.get(HConstants::HBASE_DIR)) 
fs = FileSystem.get(c) 
 
# Get a logger and a metautils instance. 
LOG = LogFactory.getLog(NAME) 
 
# Check arguments 
if ARGV.size < 1 || ARGV.size > 2 
  usage 
end 
 
# Get cmdline args. 
srcdir = fs.makeQualified(Path.new(java.lang.String.new(ARGV[0]))) 
 
if not fs.exists(srcdir) 
  raise IOError.new("src dir " srcdir.toString() " doesn't exist!") 
end 
 
# 获取表名称
表名 = nil
如果 ARGV.size > 1
  表名 = ARGV[1] 
  raise IOError.new("尚不支持")
埃尔西夫
  # 如果没有提供,则使用目录名
  表名 = srcdir.getName()
结束
HTableDescriptor.isLegalTableName(tableName.to_java_bytes) 
 
# 计算 hbase.rootdir 下的位置
# 将目录移动到位;小心不要覆盖。 
rootdir = FSUtils.getRootDir(c)
tableDir = fs.makeQualified(Path.new(rootdir, tableName)) 
 
# 如果目录当前存在,请将其移到一边。 
if srcdir.equals(tableDir)
  LOG.info("源目录位于 hbase.rootdir 下:" srcdir.toString()); 
elsif fs.exists(tableDir)
  移动表名=表名“.” java.lang.System.currentTimeMillis().to_s
  movingTableDir = Path.new(rootdir, java.lang.String.new(movedTableName)) 
  LOG.warn("将 " tableDir.toString() " 移至一边为 " movingTableDir.toString()); 
  raise IOError.new(“移动失败” tableDir.toString()) 除非 fs.rename(tableDir, movingTableDir)
  LOG.info("将" srcdir.toString() "移动到" tableDir.toString()); 
  raise IOError.new("移动失败" srcdir.toString()) 除非 fs.rename(srcdir, tableDir)
结束
 
# 明确提及 .META 中的表。 
# 扫描.META。并删除所有以表名
开头的行 LOG.info(“从 .META 中删除提及“ tableName ”。”)
metaTable = HTable.new(c, HConstants::META_TABLE_NAME) 
tableNameMetaPrefix = tableName HConstants::META_ROW_DELIMITER.chr 
scan = Scan.new((tableNameMetaPrefix HConstants::META_ROW_DELIMITER.chr).to_java_bytes) 
扫描仪 = metaTable.getScanner(scan)
# 使用 java.lang.String 进行比较。  Ruby String 有点奇怪。 
tableNameStr = java.lang.String.new(tableName)
while (结果=scanner.next())
  rowid = Bytes.toString(result.getRow()) 
  rowidStr = java.lang.String.new(rowid)
  如果不是 rowidStr.startsWith(tableNameMetaPrefix)
    # 太过分了,打破
    休息
  结束
  LOG.info("从目录中删除行:" rowid); 
  d = Delete.new(result.getRow()) 
  metaTable.delete(d)
结束
Scanner.close()
 
# 现在,遍历表格并为每个区域添加一个条目
LOG.info("Walking " srcdir.toString() " 将区域添加到目录表中")
statuses = fs.listStatus(srcdir)
对于状态中的状态
  下一步,除非 status.isDir()
  接下来如果 status.getPath().getName() == "compaction.dir" 
  Regioninfofile = Path.new(status.getPath(), HRegion::REGIONINFO_FILE)
  除非 fs.exists(regioninfofile)
    LOG.warn("缺少 .regioninfo: "regioninfofile.toString()) 
    下一个
  结束
  is = fs.open(regioninfofile) 
  hri = HRegionInfo.new()
  hri.readFields(is)
  is.close()
  # TODO:需要使用传递的表名称重做表描述符,然后重新计算区域编码名称。 
  p = Put.new(hri.getRegionName()) 
  p.add(HConstants::CATALOG_FAMILY, HConstants::REGIONINFO_QUALIFIER, Writables.getBytes(hri)) 
  metaTable.put(p)
  LOG.info("已添加到目录:" hri.toString()) 
结束
好了,以上就是我的做法,如何集群键可以通信,那就更好办了,相信你懂,scp
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn