DistCp(分布式拷贝)

概述

DistCp(分布式拷贝)是用于大规模集群内部和集群之间拷贝的工具。 它使用Map/Reduce实现文件分发,错误处理和恢复,以及报告生成。 它把文件和目录的列表作为map任务的输入,每个任务会完成源列表中部分文件的拷贝。 由于使用了Map/Reduce方法,这个工具在语义和执行上都会有特殊的地方。 这篇文档会为常用DistCp操作提供指南并阐述它的工作模型。

 

【文档来源于Hadoop官方文档(中文版),该文档并非最新文档,最新文档为distcp2的英文文档。】

使用方法

基本使用方法

DistCp最常用在集群之间的拷贝:

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

这条命令会把nn1集群的/foo/bar目录下的所有文件或目录名展开并存储到一个临时文件中,这些文件内容的拷贝工作被分配给多个map任务, 然后每个TaskTracker分别执行从nn1nn2的拷贝操作。注意DistCp使用绝对路径进行操作。

命令行中可以指定多个源目录:

bash$ hadoop distcp hdfs://nn1:8020/foo/a \ 
                    hdfs://nn1:8020/foo/b \ 
                    hdfs://nn2:8020/bar/foo

或者使用-f选项,从文件里获得多个源:
bash$ hadoop distcp -f hdfs://nn1:8020/srclist \ 
                       hdfs://nn2:8020/bar/foo 

其中srclist 的内容是
    hdfs://nn1:8020/foo/a 
    hdfs://nn1:8020/foo/b

当从多个源拷贝时,如果两个源冲突,DistCp会停止拷贝并提示出错信息, 如果在目的位置发生冲突,会根据选项设置解决。 默认情况会跳过已经存在的目标文件(比如不用源文件做替换操作)。每次操作结束时 都会报告跳过的文件数目,但是如果某些拷贝操作失败了,但在之后的尝试成功了, 那么报告的信息可能不够精确(请参考附录)。

每个TaskTracker必须都能够与源端和目的端文件系统进行访问和交互。 对于HDFS来说,源和目的端要运行相同版本的协议或者使用向下兼容的协议。 (请参考不同版本间的拷贝 )。

拷贝完成后,建议生成源端和目的端文件的列表,并交叉检查,来确认拷贝真正成功。 因为DistCp使用Map/Reduce和文件系统API进行操作,所以这三者或它们之间有任何问题 都会影响拷贝操作。一些Distcp命令的成功执行可以通过再次执行带-update参数的该命令来完成, 但用户在如此操作之前应该对该命令的语法很熟悉。

值得注意的是,当另一个客户端同时在向源文件写入时,拷贝很有可能会失败。 尝试覆盖HDFS上正在被写入的文件的操作也会失败。 如果一个源文件在拷贝之前被移动或删除了,拷贝失败同时输出异常 FileNotFoundException

选项

 

标识

描述

备注

-p[rbugp]

Preserve
  r: replication number
  b: block size
  u: user
  g: group
  p: permission

修改次数不会被保留。并且当指定 -update 时,更新的状态不会 被同步,除非文件大小不同(比如文件被重新创建)。

-i

忽略失败

就像在 附录中提到的,这个选项会比默认情况提供关于拷贝的更精确的统计, 同时它还将保留失败拷贝操作的日志,这些日志信息可以用于调试。最后,如果一个map失败了,但并没完成所有分块任务的尝试,这不会导致整个作业的失败。

-log <logdir>

记录日志到 <logdir>

DistCp为每个文件的每次尝试拷贝操作都记录日志,并把日志作为map的输出。 如果一个map失败了,当重新执行时这个日志不会被保留。

-m <num_maps>

同时拷贝的最大数目

指定了拷贝数据时map的数目。请注意并不是map数越多吞吐量越大。

-overwrite

覆盖目标

如果一个map失败并且没有使用-i选项,不仅仅那些拷贝失败的文件,这个分块任务中的所有文件都会被重新拷贝。 就像下面提到的,它会改变生成目标路径的语义,所以 用户要小心使用这个选项。

-update

如果源和目标的大小不一样则进行覆盖

像之前提到的,这不是"同步"操作。 执行覆盖的唯一标准是源文件和目标文件大小是否相同;如果不同,则源文件替换目标文件。 像 下面提到的,它也改变生成目标路径的语义, 用户使用要小心。

-f <urilist_uri>

使用<urilist_uri> 作为源文件列表

这等价于把所有文件名列在命令行中。 urilist_uri 列表应该是完整合法的URI。


https://hadoop.apache.org/docs/r1.2.1/distcp2.html

 

更新和覆盖

这里给出一些 -update和 -overwrite的例子。 考虑一个从/foo/a  /foo/b  /bar/foo的拷贝,源路径包括:

    hdfs://nn1:8020/foo/a 
    hdfs://nn1:8020/foo/a/aa 
    hdfs://nn1:8020/foo/a/ab 
    hdfs://nn1:8020/foo/b 
    hdfs://nn1:8020/foo/b/ba 
    hdfs://nn1:8020/foo/b/ab

如果没设置-update或 -overwrite选项, 那么两个源都会映射到目标端的 /bar/foo/ab。 如果设置了这两个选项,每个源目录的内容都会和目标目录的 内容 做比较。DistCp碰到这类冲突的情况会终止操作并退出。

默认情况下,/bar/foo/a  /bar/foo/b 目录都会被创建,所以并不会有冲突。

现在考虑一个使用-update合法的操作:
distcp -update hdfs://nn1:8020/foo/a \ 
               hdfs://nn1:8020/foo/b \ 
               hdfs://nn2:8020/bar

其中源路径/大小:

    hdfs://nn1:8020/foo/a 
    hdfs://nn1:8020/foo/a/aa 32 
    hdfs://nn1:8020/foo/a/ab 32 
    hdfs://nn1:8020/foo/b 
    hdfs://nn1:8020/foo/b/ba 64 
    hdfs://nn1:8020/foo/b/bb 32

和目的路径/大小:

    hdfs://nn2:8020/bar 
    hdfs://nn2:8020/bar/aa 32 
    hdfs://nn2:8020/bar/ba 32 
    hdfs://nn2:8020/bar/bb 64

会产生:

    hdfs://nn2:8020/bar 
    hdfs://nn2:8020/bar/aa 32 
    hdfs://nn2:8020/bar/ab 32 
    hdfs://nn2:8020/bar/ba 64 
    hdfs://nn2:8020/bar/bb 32

只有nn2aa文件没有被覆盖。如果指定了 -overwrite选项,所有文件都会被覆盖。

 

补充说明:

1.SSL Configurations for HSFTP sources

To use an HSFTP source (i.e. using the hsftp protocol), a Map-Red SSL configuration file needs to be specified (via the -mapredSslConf option). This must specify 3 parameters:

 

ssl.client.truststore.location: The local-filesystem location of the trust-store file, containing the certificate for the namenode.

ssl.client.truststore.type: (Optional) The format of the trust-store file.

ssl.client.truststore.password: (Optional) Password for the trust-store file.

The following is an example of the contents of the contents of a Map-Red SSL Configuration file:

 

 

<configuration>

<property>

<name>ssl.client.truststore.location</name>

<value>/work/keystore.jks</value>

<description>Truststore to be used by clients like distcp. Must be specified. </description>

</property>

<property>

<name>ssl.client.truststore.password</name>

<value>changeme</value>

<description>Optional. Default value is "". </description>

</property>

 

<property>

<name>ssl.client.truststore.type</name>

<value>jks</value>

<description>Optional. Default value is "jks". </description>

</property>

</configuration>

 

 

The SSL configuration file must be in the class-path of the DistCp program.

 

2.在不同HDFS版本复制

对于在两个不同版本的Hadoop之间进行复制,通常使用HftpFileSystem。这是一个只读文件系统,因此DistCp必须在目标集群上运行(更具体地说,在可以写入目标集群的TaskTracker上)。每个源都指定为

hftp// <dfs.http.address> / <path> 

(默认dfs.http.address是<namenode>50070)。

 

附录

Map数目

DistCp会尝试着均分需要拷贝的内容,这样每个map拷贝差不多相等大小的内容。 但因为文件是最小的拷贝粒度,所以配置增加同时拷贝(如map)的数目不一定会增加实际同时拷贝的数目以及总吞吐量。

如果没使用-m选项,DistCp会尝试在调度工作时指定map的数目 为 min (total_bytes / bytes.per.map, 20 * num_task_trackers), 其中bytes.per.map默认是256MB建议对于长时间运行或定期运行的作业,根据源和目标集群大小、拷贝数量大小以及带宽调整map的数目。

不同HDFS版本间的拷贝

对于不同Hadoop版本间的拷贝,用户应该使用HftpFileSystem。 这是一个只读文件系统,所以DistCp必须运行在目标端集群上(更确切的说是在能够写入目标集群的TaskTracker上)。 源的格式是hftp://<dfs.http.address>/<path> (默认情况dfs.http.address是 <namenode>:50070)。

Map/Reduce和副效应

像前面提到的,map拷贝输入文件失败时,会带来一些副效应。

1. 除非使用了-i,任务产生的日志会被新的尝试替换掉。

2. 除非使用了-overwrite,文件被之前的map成功拷贝后当又一次执行拷贝时会被标记为 "被忽略"

3. 如果map失败了mapred.map.max.attempts次,剩下的map任务会被终止(除非使用了-i)

4. 如果mapred.speculative.execution被设置为 final和true,则拷贝的结果是未定义的。

 

Frequently Asked Questions

1.Why does -update not create the parent source-directory under a pre-existing target directory?

The behaviour of -update and -overwrite is described in detail in the Usage section of this document. In short, if either option is used with a pre-existing destination directory, the contents of each source directory is copied over, rather than the source-directory itself. This behaviour is consistent with the legacy DistCp implementation as well.

 

2.How does the new DistCp differ in semantics from the Legacy DistCp?

Files that are skipped during copy used to also have their file-attributes (permissions, owner/group info, etc.) unchanged, when copied with Legacy DistCp. These are now updated, even if the file-copy is skipped.

Empty root directories among the source-path inputs were not created at the target, in Legacy DistCp. These are now created.

 

3.Why does the new DistCp use more maps than legacy DistCp?

Legacy DistCp works by figuring out what files need to be actually copied to target before the copy-job is launched, and then launching as many maps as required for copy. So if a majority of the files need to be skipped (because they already exist, for example), fewer maps will be needed. As a consequence, the time spent in setup (i.e. before the M/R job) is higher.

 

The new DistCp calculates only the contents of the source-paths. It doesn't try to filter out what files can be skipped. That decision is put- off till the M/R job runs. This is much faster (vis-a-vis execution-time), but the number of maps launched will be as specified in the -m option, or 20 (default) if unspecified.

 

4.Why does DistCp not run faster when more maps are specified?

At present, the smallest unit of work for DistCp is a file. i.e., a file is processed by only one map. Increasing the number of maps to a value exceeding the number of files would yield no performance benefit. The number of maps lauched would equal the number of files.

 

5.Why does DistCp run out of memory?

If the number of individual files/directories being copied from the source path(s) is extremely large (e.g. 1,000,000 paths), DistCp might run out of memory while determining the list of paths for copy. This is not unique to the new DistCp implementation.

To get around this, consider changing the -Xmx JVM heap-size parameters, as follows:

 

bash$ export HADOOP_CLIENT_OPTS="-Xms64m -Xmx1024m"

bash$ hadoop distcp2 /source /target

 

 

 

Guess you like

Origin blog.csdn.net/qq_30901367/article/details/64128450