hadoop集群间的hdfs文件拷贝

1、背景

部门有个需求,在网络互通的情况下,把现有的hadoop集群(未做Kerberos认证,集群名为:bd-stg-hadoop)的一些hdfs文件拷贝到新的hadoop集群(做了Kerberos认证,集群名为zp-tt-hadoop)

如果是两个都没有做安全认证的集群互传文件,使用distcp可以很快实现。在当前情况下,情况可能要复杂一些。通过查阅资料,在cdh的官网上竟然有这么神奇的一个参数可以解决这样的需求。传送门:http://www.cloudera.com/documentation/enterprise/5-5-x/topics/cdh_admin_distcp_secure_insecure.html

2、实现

2.1 Copying Data between two Insecure cluster

两个都没有做安全认证的集群,通常方法如下:

$ hadoop distcp hdfs://nn1:8020/foo/bar hdfs://nn2:8020/bar/foo

也可以通过webhdfs的方式:

$ hadoop distcp webhdfs://nn1:8020/foo/bar  webhdfs://nn2:8020/bar/foo

对于不同Hadoop版本间的拷贝,用户应该使用HftpFileSystem。 这是一个只读文件系统,所以DistCp必须运行在目标端集群上。 源的格式是

hftp://<dfs.http.address>/<path> (默认情况dfs.http.address是 <namenode>:50070)。

distcp的一些参数如下:

-i:忽略失败(不建议开启)
-log:记录日志到 <logdir>
-m:同时拷贝的最大数目(指定了拷贝数据时map的数目。请注意并不是map数越多吞吐量越大。)
-overwrite:覆盖目标(如果一个map失败并且没有使用-i选项,不仅仅那些拷贝失败的文件,这个分块任务中的所有文件都会被重新拷贝。 就像下面提到的,它会改变生成目标路径的语义,所以 用户要小心使用这个选项。)
-update:如果源和目标的大小不一样则进行覆盖
-f:使用<urilist_uri> 作为源文件列表

2.2 Copying Data between a Secure and an Insecure

在secure-cluster上的core-site.xml配置文件中添加:

<property> 
  <name>ipc.client.fallback-to-simple-auth-allowed</name>
  <value>true</value> 
</property>

然后在secure-cluster执行如下命令:

distcp webhdfs://insecureCluster webhdfs://secureCluster 
distcp webhdfs://secureCluster webhdfs://insecureCluster 

在我实际操作过程中,两个集群hdfs都有做ha,使用上面的命令会报错:

16/09/27 14:47:52 ERROR tools.DistCp: Exception encountered 
java.lang.IllegalArgumentException: java.net.UnknownHostException: bd-stg-hadoop
	at org.apache.hadoop.security.SecurityUtil.buildTokenService(SecurityUtil.java:374)
	at org.apache.hadoop.security.SecurityUtil.buildTokenService(SecurityUtil.java:392)
	at org.apache.hadoop.hdfs.web.WebHdfsFileSystem.initialize(WebHdfsFileSystem.java:167)
	at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:2643)
	at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:93)
	at org.apache.hadoop.fs.FileSystem$Cache.getInternal(FileSystem.java:2680)
	at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:2662)
	at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:379)
	at org.apache.hadoop.fs.Path.getFileSystem(Path.java:296)
	at org.apache.hadoop.tools.GlobbedCopyListing.doBuildListing(GlobbedCopyListing.java:76)
	at org.apache.hadoop.tools.CopyListing.buildListing(CopyListing.java:86)
	at org.apache.hadoop.tools.DistCp.createInputFileListing(DistCp.java:365)
	at org.apache.hadoop.tools.DistCp.execute(DistCp.java:171)
	at org.apache.hadoop.tools.DistCp.run(DistCp.java:122)
	at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70)
	at org.apache.hadoop.tools.DistCp.main(DistCp.java:429)
Caused by: java.net.UnknownHostException: bd-stg-hadoop

解决如下:

把bd-stg-hadoop集群名字改成了active-namenode的host再进行操作,如下:

$ hadoop distcp webhdfs://bd-stg-namenode-138/tmp/hivebackup/app/app_stg_session webhdfs://zp-tt-hadoop:8020/tmp/hivebackup/app/app_stg_session

成功执行后, 可以发现原理就是启动一个只有map的MapReduce作业来实现两个集群间的数据复制。

2.3 Copying Data between two Secure cluster

这种情况相对有些复杂了,需要Kerberos做跨域的配置。本文暂不研究讨论。

----------------------------我也是有底线的-----------------------------
原文地址:https://www.cnblogs.com/bugsbunny/p/6811519.html