
# 快速开发HDFS应用
HDFS（Hadoop Distributed File System）是一个适合运行在通用硬件之上，具备高度容错特性，支持高吞吐量数据访问的分布式文件系统，非常适合大规模数据集应用。
HDFS适用于如下场景：
- 处理海量数据（TB或PB级别以上）
- 需要很高的吞吐量
- 需要高可靠性
- 需要很好的可扩展能力
MRS对外提供了基于HDFS组件的应用开发样例工程，本实践用于指导您创建MRS集群后，获取并导入样例工程并在本地进行编译调测，用于实现MRS集群中的HDFS文件目录创建、文件写入、读取、删除等操作。
#### 创建MRS Hadoop集群
1. 购买一个包含有Hadoop组件的MRS集群，详情请参见[购买自定义集群](https://support.huaweicloud.com/usermanual-mrs/mrs_01_0513.html)。
   ![](https://support.huaweicloud.com/devg-lts-mrs/public_sys-resources/note_3.0-zh-cn.png)
   本文以购买的MRS 3.2.0-LTS.1版本的集群为例，组件包含Hadoop组件，集群开启Kerberos认证。
   
2. 单击"立即购买"，等待MRS集群创建成功。
 
 #### 准备应用开发配置文件
1. 集群创建成功后，登录FusionInsight Manager创建用于样例工程安全认证的集群用户。 
   选择"系统 \> 权限 \> 用户 \> 添加用户"，在新增用户界面创建一个机机用户，例如**developuser**。
   "用户组"需加入"supergroup"用户组。
   
   
2. 选择"系统 \> 权限 \> 用户"，在用户名为"developuser"的操作列选择"更多 \> 下载认证凭据"下载认证凭据文件，保存后解压得到该用户的"user.keytab"文件与"krb5.conf"文件。
3. 选择"集群 \> 概览 \> 更多 \> 下载客户端"，"选择客户端类型"设置为"仅配置文件"，单击"确定"，等待客户端文件包生成后根据浏览器提示下载客户端到本地并解压。 
   例如，客户端配置文件压缩包为"FusionInsight_Cluster_1_Services_Client.tar"，解压后得到"FusionInsight_Cluster_1_Services_ClientConfig_ConfigFiles.tar"，继续解压该文件。
   1. 进入客户端配置文件解压路径"FusionInsight_Cluster_1_Services_ClientConfig_ConfigFiles\\HDFS\\config"，获取[表1]中相关配置文件。
       表1配置文件 
      | 配置文件          | 作用                 |
      |:---|:---|
      | core-site.xml | 配置Hadoop Core详细参数。 |
      | hdfs-site.xml | 配置HDFS详细参数。        |
         
      
   
   2. 复制解压目录下的"hosts"文件中的内容到本地hosts文件中，确保本地机器能与解压目录下"hosts"文件中所列出的各主机在网络上互通。
      ![](https://support.huaweicloud.com/devg-lts-mrs/public_sys-resources/note_3.0-zh-cn.png)
      - 在本实践中，需要确保本地环境与MRS集群所在网络平面互通，通常可以通过绑定EIP的方式访问MRS集群，具体操作请参考[配置Windows通过EIP访问集群HDFS](https://support.huaweicloud.com/devg3-mrs/mrs_07_090038.html)。
      
      - 如果本地开发环境与MRS集群内节点网络不通，也可以将样例工程编译后，将jar包上传至集群内运行，具体操作可参考[调测HDFS应用](https://support.huaweicloud.com/devg-lts-mrs/mrs_07_090024.html)。
      
      - Windows本地hosts文件存放路径举例："C:\\WINDOWS\\system32\\drivers\\etc\\hosts"。
        
   
   
   
   
 
#### 获取样例工程
1. 通过开源镜像站获取样例工程。 
   下载样例工程的Maven工程源码和配置文件，并在本地配置好相关开发工具，可参考[通过开源镜像站获取样例工程](https://support.huaweicloud.com/devg3-mrs/mrs_07_010002.html)。
   根据集群版本选择对应的分支，下载并获取MRS相关样例工程。
   例如本章节场景对应示例为"hdfs-example-security"样例，获取地址：<https://github.com/huaweicloud/huaweicloud-mrs-example/tree/mrs-3.2.0.1/src/hdfs-example-security>。
   
   
2. 本地使用IDEA工具导入样例工程，等待Maven工程下载相关依赖包，具体操作可参考[配置并导入样例工程](https://support.huaweicloud.com/devg3-mrs/mrs_07_410007.html)。
   
   图1HDFS样例工程示例   
   ![](https://support.huaweicloud.com/devg-lts-mrs/zh-cn_image_0000001597091173.png)
   本地配置好Maven及SDK相关参数后，样例工程会自动加载相关依赖包。
   
   
3. 将[准备应用开发配置文件]中获取的集群配置文件及用户认证文件放置在样例工程的"conf"目录下。
4. 在HDFS样例工程代码中，不同的样例工程，使用的认证代码不同，包括基本安全认证和带ZooKeeper认证。 
   本示例中，不需要访问HBase或ZooKeeper，所以使用基本的安全认证代码即可。
   在"com.huawei.bigdata.hdfs.examples"包的"HdfsExample"类中修改"PRINCIPAL_NAME"为实际用户名，例如"developuser"。
   ```
   private static final String PATH_TO_HDFS_SITE_XML = System.getProperty("user.dir") + File.separator + "conf"
           + File.separator + "hdfs-site.xml";
   private static final String PATH_TO_CORE_SITE_XML =  System.getProperty("user.dir") + File.separator + "conf"
           + File.separator + "core-site.xml";
   private static final String PRINCIPAL_NAME = "developuser";
   private static final String PATH_TO_KEYTAB = System.getProperty("user.dir") + File.separator + "conf"
           + File.separator + "user.keytab";
   private static final String PATH_TO_KRB5_CONF = System.getProperty("user.dir") + File.separator + "conf"
           + File.separator + "krb5.conf";
   ...
   ```
   本样例工程中，基于业务场景的开发思路如下，各功能代码片段详情说明可参考[开发HDFS应用](https://support.huaweicloud.com/devg-lts-mrs/mrs_07_090009.html)。
   以"/user/hdfs-examples/test.txt"文件的读写删除等操作为例，说明HDFS文件的基本操作流程：
   1. 通过集群安全认证。
   
   2. 创建FileSystem对象：fSystem。
   
   3. 调用fSystem的mkdir接口创建目录。
   
   4. 调用fSystem的create接口创建FSDataOutputStream对象：out，使用out的write方法写入数据。
   
   5. 调用fSystem的append接口创建FSDataOutputStream对象：out，使用out的write方法追加写入数据。
   
   6. 调用fSystem的open接口创建FSDataInputStream对象：in，使用in的read方法读取文件。
   
   7. 调用fSystem中的delete接口删除文件。
   
   8. 调用fSystem中的delete接口删除文件夹。
   
   
   
   
 
#### 编译并运行程序
1. 单击IDEA右边Maven窗口的"Reimport All Maven Projects"，进行Maven项目依赖加载。 
   图2加载样例工程   
   ![](https://support.huaweicloud.com/devg-lts-mrs/zh-cn_image_0000001597151565.png)
   
   
2. 编译运行程序。 
   1. 选择"Maven \> *样例工程名称*\> Lifecycle \> clean"，双击"clean"运行Maven的clean命令。
   
   2. 选择"Maven \> *样例工程名称* \> Lifecycle \> compile"，双击"compile"运行Maven的compile命令。
   
   
   编译完成，打印"Build Success"，生成target目录。
   ```
   [INFO] ------------------------------------------------------------------------
   [INFO] BUILD SUCCESS
   [INFO] ------------------------------------------------------------------------
   [INFO] Total time:  21.276 s
   [INFO] Finished at: 2023-05-05T14:36:39+08:00
   [INFO] ------------------------------------------------------------------------
   ```
   
   
3. 运行程序。 
   右键单击"HdfsExample.java"文件，选择"Run 'HdfsExample.main() '"。
   图3运行程序   
   ![](https://support.huaweicloud.com/devg-lts-mrs/zh-cn_image_0000001597097073.png)
   
   
4. 样例运行成功后，运行信息如下，表示相关文件操作成功执行。 
   ```
   ...
   2217 [main] INFO  org.apache.hadoop.security.UserGroupInformation  - Login successful for user developuser using keytab file user.keytab. Keytab auto renewal enabled : false
   2217 [main] INFO  com.huawei.hadoop.security.LoginUtil  - Login success!!!!!!!!!!!!!!
   3529 [main] WARN  org.apache.hadoop.hdfs.shortcircuit.DomainSocketFactory  - The short-circuit local reads feature cannot be used because UNIX Domain sockets are not available on Windows.
   4632 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to create path /user/hdfs-examples
   5392 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to write.
   8200 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to append.
   9384 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - result is : hi, I am bigdata. It is successful if you can see me.I append this content.
   9384 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to read.
   9636 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete the file /user/hdfs-examples\test.txt
   9860 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete path /user/hdfs-examples
   10010 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to create path /user/hdfs-examples/hdfs_example_0
   10069 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to create path /user/hdfs-examples/hdfs_example_1
   10553 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to write.
   10607 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to write.
   13356 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to append.
   13469 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to append.
   13784 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - result is : hi, I am bigdata. It is successful if you can see me.I append this content.
   13784 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to read.
   13834 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - result is : hi, I am bigdata. It is successful if you can see me.I append this content.
   13834 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to read.
   13837 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete the file /user/hdfs-examples/hdfs_example_0\test.txt
   13889 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete the file /user/hdfs-examples/hdfs_example_1\test.txt
   14003 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete path /user/hdfs-examples/hdfs_example_0
   14118 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete path /user/hdfs-examples/hdfs_example_1
   ...
   ```
   
   
 
