前提条件
- 已开通文件存储HDFS版服务并创建文件系统实例和挂载点。具体操作,请参见文件存储HDFS版快速入门。
- 建议您使用的Hadoop版本不低于2.7.2,本文使用的Hadoop版本为Apache Hadoop 2.8.5。
- 已为Hadoop集群所有节点安装JDK,且JDK版本不低于1.8。更多信息,下载JDK。
- 已开通对象存储OSS服务并创建存储空间(Bucket)。具体操作,请参见对象存储OSS快速入门。
背景信息
阿里云文件存储HDFS版是面向阿里云ECS实例及容器服务等计算资源的文件存储服务。文件存储HDFS版允许您就像在Hadoop分布式文件系统中一样管理和访问数据,并对热数据提供高性能的数据访问能力。对象存储OSS是海量、安全、低成本、高可靠的云存储服务,并提供标准型、归档型等多种存储类型供选择。您可以在文件存储HDFS版和对象存储OSS之间迁移数据,从而实现热、温、冷数据的合理分层,在实现对热数据高性能访问的同时,有效控制存储成本。
步骤一:Hadoop集群挂载文件存储HDFS版实例
在Hadoop集群中配置文件存储HDFS版实例。具体操作,请参见挂载文件系统。
步骤二:Hadoop集群部署JindoSDK
- 下载最新的JindoSDK安装包(下载页面)。本文以jindosdk-4.5.0为例。
- 执行以下命令,解压安装包。
tar -zxvf ./jindosdk-4.5.0.tar.gz
- 执行以下命令,将安装包内的jindo-core-4.5.0.jar和jindo-sdk-4.5.0.jar文件复制到Hadoop的CLASSPATH路径下。
cp-v ./jindosdk-4.5.0/lib/jindo-*-4.5.0.jar ${HADOOP_HOME}/share/hadoop/hdfs/lib/
- 配置JindoSDK OSS实现类及Access Key。
a. 执行以下命令,打开core-site.xml配置文件。
vim${HADOOP_HOME}/etc/hadoop/core-site.xml
b. 将JindoSDK OSS实现类配置到core-site.xml 中。
<configuration><property><name>fs.AbstractFileSystem.oss.impl</name><value>com.aliyun.jindodata.oss.OSS</value></property><property><name>fs.oss.impl</name><value>com.aliyun.jindodata.oss.JindoOssFileSystem</value></property></configuration>
c. 将OSS Bucket对应的Access Key ID、Access Key Secret、Endpoint配置在core-site.xml中。
<configuration><property><name>fs.oss.accessKeyId</name><!-- 请填写您用于访问OSS Bucket的Access Key ID --><value>xxx</value></property><property><name>fs.oss.accessKeySecret</name><!-- 请填写上述Access Key ID对应的Access Key Secret --><value>xxx</value></property><property><name>fs.oss.endpoint</name><!-- ECS环境推荐使用内网OSS Endpoint,格式为oss-cn-xxx-internal.aliyuncs.com --><value>oss-cn-xxx.aliyuncs.com</value></property></configuration>
- 将上述jindo-core-4.5.0.jar、jindo-sdk-4.5.0.jar和core-site.xml文件同步到Hadoop集群所有节点。
- 使用Hadoop Shell访问OSS。
${HADOOP_HOME}/bin/hadoop fs -ls oss://<bucket>/<path>
请将<bucket>替换为OSS的存储空间名称,<path>替换为该存储空间下的文件路径。
步骤三:数据迁移
完成步骤一和步骤二后,使用Hadoop MapReduce任务(DistCp)即可实现数据迁移。
实践一:将文件存储HDFS版上的数据迁移至对象存储OSS
- 执行以下命令,在文件存储HDFS版实例上生成100GB测试数据。
${HADOOP_HOME}/bin/hadoop jar \ ${HADOOP_HOME}/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.8.5.jar \ randomtextwriter \ -D mapreduce.randomtextwriter.totalbytes=107374182400 \ -D mapreduce.randomtextwriter.bytespermap=10737418240 \ dfs://f-xxxxxxx.cn-beijing.dfs.aliyuncs.com:10290/dfs2oss/data/data_100g/
其中,f-xxxxxxx.cn-beijing.dfs.aliyuncs.com为文件存储HDFS版挂载点域名,/dfs2oss/data/data_100g为保存测试数据的目录。请根据实际情况进行替换。
- 执行以下命令,查看生成的测试数据。
${HADOOP_HOME}/bin/hadoop fs -du-s-h dfs://f-xxxxxxx.cn-beijing.dfs.aliyuncs.com:10290/dfs2oss/data/data_100g
${HADOOP_HOME}/bin/hadoop fs -du-s dfs://f-xxxxxxx.cn-beijing.dfs.aliyuncs.com:10290/dfs2oss/data/data_100g
- 执行以下命令,启动Hadoop MapReduce任务(DistCp)将测试数据迁移至对象存储OSS。
详细的DistCp工具使用说明请参见Hadoop DistCp工具官方说明文档。
${HADOOP_HOME}/bin/hadoop distcp \ dfs://f-xxxxxxx.cn-beijing.dfs.aliyuncs.com:10290/dfs2oss oss://<bucket>/<path>
- 任务执行完成后,如果回显包含如下类似信息,说明迁移成功。
22/08/10 16:20:54 INFO mapreduce.Job: Job job_1660115355800_0003 completed successfully 22/08/10 16:20:54 INFO mapreduce.Job: Counters: 38 File System Counters DFS: Number of bytes read=110223438504 DFS: Number of bytes written=0 DFS: Number of read operations=114 DFS: Number of large read operations=0 DFS: Number of writeoperations=26 FILE: Number of bytes read=0 FILE: Number of bytes written=2119991 FILE: Number of read operations=0 FILE: Number of large read operations=0 FILE: Number of writeoperations=0 OSS: Number of bytes read=0 OSS: Number of bytes written=0 OSS: Number of read operations=0 OSS: Number of large read operations=0 OSS: Number of writeoperations=0 Job Counters Launched map tasks=13 Other local map tasks=13 Total time spent by all maps in occupied slots (ms)=10264552 Total time spent by all reduces in occupied slots (ms)=0 Total time spent by all map tasks (ms)=10264552 Total vcore-milliseconds taken by all map tasks=10264552 Total megabyte-milliseconds taken by all map tasks=10510901248 Map-Reduce Framework Map input records=14 Map output records=0 Input split bytes=1755 Spilled Records=0 Failed Shuffles=0 Merged Map outputs=0 GC time elapsed (ms)=18918 CPU time spent (ms)=798190 Physical memory (bytes) snapshot=4427018240 Virtual memory (bytes) snapshot=50057256960 Total committed heap usage (bytes)=2214068224 File Input Format Counters Bytes Read=6528 File Output Format Counters Bytes Written=0 DistCp Counters Bytes Copied=110223430221 Bytes Expected=110223430221 Files Copied=14
- 执行以下命令,查看迁移到对象存储OSS上的数据大小是否与原文件一致。
${HADOOP_HOME}/bin/hadoop fs -du-s oss://<bucket>/<path>
${HADOOP_HOME}/bin/hadoop fs -du-s-h oss://<bucket>/<path>
实践二:将对象存储OSS上的数据迁移至文件存储HDFS版
- 执行以下命令,在对象存储OSS上生成100GB测试数据。
${HADOOP_HOME}/bin/hadoop jar \ ${HADOOP_HOME}/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.8.5.jar \ randomtextwriter \ -D mapreduce.randomtextwriter.totalbytes=107374182400 \ -D mapreduce.randomtextwriter.bytespermap=10737418240 \ oss://<bucket>/<path>
- 执行以下命令,查看生成的测试数据。
${HADOOP_HOME}/bin/hadoop fs -du-s-h oss://<bucket>/<path>
${HADOOP_HOME}/bin/hadoop fs -du-s oss://<bucket>/<path>
- 执行以下命令,启动Hadoop MapReduce任务(DistCp)将测试数据迁移至文件存储HDFS版。
${HADOOP_HOME}/bin/hadoop distcp \ oss://<bucket>/<path> \ dfs://f-xxxxxxx.cn-beijing.dfs.aliyuncs.com:10290/oss2dfs
- 任务执行完成后,如果回显包含如下类似信息,说明迁移成功。
22/08/10 17:08:17 INFO mapreduce.Job: Job job_1660115355800_0005 completed successfully 22/08/10 17:08:17 INFO mapreduce.Job: Counters: 38 File System Counters DFS: Number of bytes read=6495 DFS: Number of bytes written=110223424482 DFS: Number of read operations=139 DFS: Number of large read operations=0 DFS: Number of writeoperations=47 FILE: Number of bytes read=0 FILE: Number of bytes written=1957682 FILE: Number of read operations=0 FILE: Number of large read operations=0 FILE: Number of writeoperations=0 OSS: Number of bytes read=0 OSS: Number of bytes written=0 OSS: Number of read operations=0 OSS: Number of large read operations=0 OSS: Number of writeoperations=0 Job Counters Launched map tasks=12 Other local map tasks=12 Total time spent by all maps in occupied slots (ms)=10088860 Total time spent by all reduces in occupied slots (ms)=0 Total time spent by all map tasks (ms)=10088860 Total vcore-milliseconds taken by all map tasks=10088860 Total megabyte-milliseconds taken by all map tasks=10330992640 Map-Reduce Framework Map input records=12 Map output records=0 Input split bytes=1620 Spilled Records=0 Failed Shuffles=0 Merged Map outputs=0 GC time elapsed (ms)=21141 CPU time spent (ms)=780270 Physical memory (bytes) snapshot=4037222400 Virtual memory (bytes) snapshot=45695025152 Total committed heap usage (bytes)=2259156992 File Input Format Counters Bytes Read=4875 File Output Format Counters Bytes Written=0 DistCp Counters Bytes Copied=110223424482 Bytes Expected=110223424482 Files Copied=12
- 执行以下命令,查看迁移到文件存储HDFS版上的数据大小是否与原文件一致。
${HADOOP_HOME}/bin/hadoop fs -du-s dfs://f-xxxxx.cn-beijing.dfs.aliyuncs.com:10290/oss2dfs
${HADOOP_HOME}/bin/hadoop fs -du-s-h dfs://f-xxxxx.cn-beijing.dfs.aliyuncs.com:10290/oss2dfs
常见问题
- 对于正在写入的文件,进行迁移时会遗漏掉最新写入的数据吗?
Hadoop兼容文件系统提供单写者多读者并发语义,针对同一个文件,同一时刻可以有一个写者写入和多个读者读出。以文件存储HDFS版到对象存储OSS的数据迁移为例,数据迁移任务打开文件存储HDFS版的文件F,根据当前系统状态决定文件F的长度L,将L字节迁移到对象存储OSS。如果在数据迁移过程中,有并发的写者写入,文件F的长度将超过L,但是数据迁移任务无法感知到最新写入的数据。因此,建议您在做数据迁移时,避免向迁移的文件中写入数据。
联系我们
了解更多关于文件存储HDFS版的产品信息,欢迎访问https://www.aliyun.com/product/alidfs
如果您对文件存储HDFS版有任何问题,欢迎钉钉扫描以下二维码加入文件存储HDFS版技术交流群。