方法二:使用hadoop namenode -importCheckpoint
1、删除name目录:
1[hadoop@node1 hdfs]$ rm-rf name
2、关闭集群,从secondarynamenode拷贝namesecondary目录到dfs.name.dir:

[hadoop@node2 hdfs]$ scp-r namesecondary node1:/app/user/hdfs/fsimage 100%1570.2KB/s00:00
fstime 100%80.0KB/s00:00
fsimage 100%24102.4KB/s00:00
VERSION 100%1010.1KB/s00:00
edits 100%40.0KB/s00:00
fstime 100%80.0KB/s00:00
fsimage 100%24102.4KB/s00:00
VERSION 100%1010.1KB/s00:00
edits 100%40.0KB/s00:00

3、在namenode节点上执行hadoop namenode -importCheckpoint

[hadoop@node1 hdfs]$ hadoop namenode-importCheckpoint13/11/1407:24:20 INFO namenode.NameNode: STARTUP_MSG: /************************************************************
STARTUP_MSG: Starting NameNode
STARTUP_MSG: host = node1/192.168.1.151
STARTUP_MSG: args = [-importCheckpoint]
STARTUP_MSG: version = 0.20.2
STARTUP_MSG: build = https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.20 -r 911707; compiled by 'chrisdo' on Fri Feb 19 08:07:34 UTC 2010
************************************************************/13/11/1407:24:20INFO metrics.RpcMetrics: Initializing RPC MetricswithhostName=NameNode, port=900013/11/1407:24:20INFO namenode.NameNode: Namenode up at: node1.com/192.168.1.151:900013/11/1407:24:20INFO jvm.JvmMetrics: Initializing JVM MetricswithprocessName=NameNode, sessionId=null13/11/1407:24:20 INFO metrics.NameNodeMetrics: Initializing NameNodeMeterics using context object:org.apache.hadoop.metrics.spi.NullContext13/11/1407:24:21INFO namenode.FSNamesystem: fsOwner=hadoop,hadoop13/11/1407:24:21INFO namenode.FSNamesystem: supergroup=supergroup13/11/1407:24:21INFO namenode.FSNamesystem: isPermissionEnabled=true13/11/1407:24:21 INFO metrics.FSNamesystemMetrics: Initializing FSNamesystemMetrics using context object:org.apache.hadoop.metrics.spi.NullContext13/11/1407:24:21 INFO namenode.FSNamesystem: Registered FSNamesystemStatusMBean13/11/1407:24:21INFO common.Storage: Storage directory/app/user/hdfs/nameisnot formatted.13/11/1407:24:21 INFO common.Storage: Formatting ...13/11/1407:24:21INFO common.Storage:Numberoffiles=2613/11/1407:24:21INFO common.Storage:Numberoffiles under construction=013/11/1407:24:21INFO common.Storage:Imagefileofsize2410loadedin0 seconds.13/11/1407:24:21INFO common.Storage: Editsfile/app/user/hdfs/namesecondary/current/editsofsize4edits #0loadedin0 seconds.13/11/1407:24:21INFO common.Storage:Imagefileofsize2410savedin0 seconds.13/11/1407:24:21INFO common.Storage:Imagefileofsize2410savedin0 seconds.13/11/1407:24:21INFO namenode.FSNamesystem:Numberoftransactions:0Total timefortransactions(ms): 0Numberoftransactions batchedinSyncs:0Numberofsyncs:0SyncTimes(ms):013/11/1407:24:21INFO namenode.FSNamesystem: Finished loading FSImagein252 msecs13/11/1407:24:21INFO hdfs.StateChange: STATE*Safe modeON.
The ratio ofreported blocks0.0000hasnotreached the threshold0.9990. Safe mode will be turnedoff automatically.13/11/1407:24:21INFO mortbay.log: Loggingtoorg.slf4j.impl.Log4jLoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog13/11/1407:24:21INFO http.HttpServer: Port returnedbywebServer.getConnectors()[0].getLocalPort() beforeopen()is-1. Opening the listeneron5007013/11/1407:24:21INFO http.HttpServer: listener.getLocalPort() returned50070webServer.getConnectors()[0].getLocalPort() returned5007013/11/1407:24:21INFO http.HttpServer: Jetty boundtoport5007013/11/1407:24:21INFO mortbay.log: jetty-6.1.1413/11/1407:24:21INFO mortbay.log: Started SelectChannelConnector@node1.com:5007013/11/1407:24:21INFO namenode.NameNode: Web-server up at: node1.com:5007013/11/1407:24:21 INFO ipc.Server: IPC Server Responder: starting13/11/1407:24:21INFO ipc.Server: IPC Server listeneron9000: starting13/11/1407:24:21INFO ipc.Server: IPC Server handler0on9000: starting13/11/1407:24:21INFO ipc.Server: IPC Server handler1on9000: starting13/11/1407:24:21INFO ipc.Server: IPC Server handler2on9000: starting13/11/1407:24:21INFO ipc.Server: IPC Server handler3on9000: starting13/11/1407:24:21INFO ipc.Server: IPC Server handler4on9000: starting13/11/1407:24:21INFO ipc.Server: IPC Server handler5on9000: starting13/11/1407:24:21INFO ipc.Server: IPC Server handler6on9000: starting13/11/1407:24:21INFO ipc.Server: IPC Server handler9on9000: starting13/11/1407:24:21INFO ipc.Server: IPC Server handler7on9000: starting13/11/1407:24:21INFO ipc.Server: IPC Server handler8on9000: starting13/11/1407:37:05 INFO namenode.NameNode: SHUTDOWN_MSG: /************************************************************
SHUTDOWN_MSG: Shutting down NameNode at node1/192.168.1.151
************************************************************/[hadoop@node1 current]$ start-all.sh
starting namenode, logging to/app/hadoop/bin/../logs/hadoop-hadoop-namenode-node1.out192.168.1.152: starting datanode, loggingto/app/hadoop/bin/../logs/hadoop-hadoop-datanode-node2.out192.168.1.153: starting datanode, loggingto/app/hadoop/bin/../logs/hadoop-hadoop-datanode-node3.out192.168.1.152: starting secondarynamenode, loggingto/app/hadoop/bin/../logs/hadoop-hadoop-secondarynamenode-node2.out
starting jobtracker, logging to/app/hadoop/bin/../logs/hadoop-hadoop-jobtracker-node1.out192.168.1.152: starting tasktracker, loggingto/app/hadoop/bin/../logs/hadoop-hadoop-tasktracker-node2.out192.168.1.153: starting tasktracker, loggingto/app/hadoop/bin/../logs/hadoop-hadoop-tasktracker-node3.out[hadoop@node1 current]$ jps1027 JobTracker1121 Jps879NameNode

4、验证数据的完整性:

1[hadoop@node3 conf]$ hive 2 3Logging initialized using configurationinjar:file:/app/hive/lib/hive-common-0.11.0.jar!/hive-log4j.properties 4Hive historyfile=/tmp/hadoop/hive_job_log_hadoop_8383@node3_201311111443_2018635710.txt 5hive>select*from table4; 6OK 7NULLNULLNULL 8115 924510345114561256713615147561583616NULLNULLNULL17Time taken:3.081seconds, Fetched:10row(s)

总结:
注意:恢复的namenode中secondarynamenode的最近一次check到故障发生这段时间的内容将丢失,所以fs.checkpoint.period参数值在实际设定中要尽可能的权衡。并且也时常备份secondarynamenode节点中的内容,因为scondarynamenode也是单点的,以防发生故障。
补充说明:如果是用新的节点来恢复namenode,则要注意
1、新节点的Linux环境,目录结构,环境变量等等配置需要跟原来的namenode一模一样,包括conf目录下的所有文件配置。
2、新namenode的主机名要与原namenode保持一致,如果是重新命名主机名的话,则需要批量替换datanode和secondarynamenode的hosts文件,并且重新配置以下文件的部分core-site.xml文件中的fs.default.name
hdfs-site.xml文件中的dfs.http.address(secondarynamenode节点上)
mapred-site.xml文件中的mapred.job.tracker(如果jobtracker与namenode在同一个机器上,一般都是同一台机器上)。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2730/viewspace-3156263/ ,如需转载,请注明出处,否则将追究法律责任。