更新时间:2025-12-16 GMT+08:00
分享

在本地Windows环境中调测HDFS程序

操作场景

在代码完成开发后,您可以在Windows开发环境中运行应用。本地和集群业务平面网络互通时,您可以直接在本地进行调测。

HDFS应用程序运行完成后,可直接通过运行结果查看应用程序运行情况,也可以通过HDFS日志获取应用运行情况。

在本地Windows环境中调测HDFS程序

  • 编译并运行SpringBoot对接HDFS样例:
    1. 登录HDFS时会使用到如表1所示的配置文件。这些文件均已导入到“hdfs-examples”工程的“src/main/resource”目录。
      表1 配置文件

      文件名称

      作用

      core-site.xml

      配置HDFS详细参数。

      hdfs-site.xml

      配置HDFS详细参数。

      user.keytab

      对于Kerberos安全认证提供HDFS用户信息。

      krb5.conf

      Kerberos server配置信息。

      application.properties

      Spring boot的配置信息,目前需要配置下面信息:

      • “hdfs-examples”工程的“src/main/resource”目录的绝对地址。
      • 登录hdfs所需要的用户名
    2. 修改application.properties中的参数:
      • spring.hdfs.config.dir:访问HDFS的配置文件目录地址,其中配置文件包括:hdfs-site.xml、core-site.xml、krb5.conf、user.keytab。
      • hdfs.user:访问HDFS的业务用户名称。
    3. 在开发环境中(例如IntelliJ IDEA中),选中SpringDataHDFSApplication.java,右键工程。选中“Run 'SpringDataHDFSApplication.main()'”运行工程。
    4. 在浏览器中依次访问如下对应链接。
      • http://127.0.0.1:8080/save?fileName=test.txt : 向HDFS的目录“/tmp/examples”中保存test.txt数据。
      • http://127.0.0.1:8080/read?fileName=test.txt :读取HDFS的目录“/tmp/examples”中保存test.txt数据。
      • http://127.0.0.1:8080/remove?fileName=test.txt:删除HDFS的目录“/tmp/examples”中test.txt文件。
  • 编译并运行SpringBoot外其他样例程序:
    1. 在开发环境中(例如IntelliJ IDEA中),分别选中以下两个工程运行程序:
      • 选中HdfsExample.java,右键工程,选择Run 'HdfsExample.main()'运行应用工程。
      • 选中ColocationExample.java,右键工程,选择Run 'ColocationExample.main()'运行应用工程。
      • 在HDFS任务运行过程中禁止重启HDFS服务,否则可能会导致任务失败。
      • 在运行Colocation工程时,HDFS的配置项fs.defaultFS不能配置为viewfs://ClusterX。

设置系统的环境变量说明

Hadoop在安全认证的时候,需要获取客户端所在主机的域名(Default Realm,从环境变量USERDNSDOMAIN中获取)。如果该主机没有域名,则运行样例程序会有如下报错:

此时需要用户设置系统的环境变量USERDNSDOMAIN以规避该问题,具体如下:

  1. 单击“计算机”右键,选择“属性”,然后选择“高级系统设置 > 高级 > 环境变量”。
  2. 设置系统环境变量,在系统变量下单击“新建”,弹出新建系统变量框,变量名中输入“USERDNSDOMAIN”,变量值设为非空字符串,图中以“test”为例。

    连续单击“确定”,完成系统环境变量的设置。

  3. 关闭样例工程,重新打开,运行。

查看调测结果

  • 查看运行结果获取应用运行情况
    • HdfsExample Windows样例程序运行结果如下所示。
      ...
      1308 [main] INFO  org.apache.hadoop.security.UserGroupInformation  - Login successful for user hdfsDevelop using keytab file 
      1308 [main] INFO  com.huawei.hadoop.security.LoginUtil  - Login success!!!!!!!!!!!!!!
      2040 [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.
      3006 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to create path /user/hdfs-examples
      3131 [main] WARN  org.apache.hadoop.util.NativeCodeLoader  - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
      3598 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to write.
      4408 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to append.
      5015 [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.
      5015 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to read.
      5077 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete the file /user/hdfs-examples\test.txt
      5186 [main] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete path /user/hdfs-examples
      5311 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to create path /user/hdfs-examples/hdfs_example_0
      5311 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to create path /user/hdfs-examples/hdfs_example_1
      5669 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to write.
      5669 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to write.
      7258 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to append.
      7741 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to append.
      7896 [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.
      7896 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to read.
      7959 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete the file /user/hdfs-examples/hdfs_example_1\test.txt
      8068 [hdfs_example_1] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete path /user/hdfs-examples/hdfs_example_1
      8364 [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.
      8364 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to read.
      8426 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete the file /user/hdfs-examples/hdfs_example_0\test.txt
      8535 [hdfs_example_0] INFO  com.huawei.bigdata.hdfs.examples.HdfsExample  - success to delete path /user/hdfs-examples/hdfs_example_0
      ...

      在Windows环境运行样例代码时可能会出现下面的异常,但是不影响业务:

      java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.

    • ColocationExample Windows样例程序运行结果如下所示。
      ...
      945  [main] INFO  org.apache.hadoop.security.UserGroupInformation  - Login successful for user hdfsDeveloper using keytab file user.keytab
      945  [main] INFO  com.huawei.hadoop.security.LoginUtil  - Login success!!!!!!!!!!!!!!
      945  [main] INFO  com.huawei.hadoop.security.LoginUtil  - JaasConfiguration loginContextName=Client principal=hdfsDeveloper useTicketCache=false keytabFile=XXX\sample_project\src\hdfs-example-security\conf\user.keytab
      946  [main] INFO  com.huawei.hadoop.security.KerberosUtil  - Get default realm successfully, the realm is : HADOOP.COM
      ...
      Create Group has finished.
      Put file is running...
      Put file has finished.
      Delete file is running...
      Delete file has finished.
      Delete Group is running...
      Delete Group has finished.
      4946 [main-EventThread] INFO  org.apache.zookeeper.ClientCnxn  - EventThread shut down for connection: 0x440751cb41a4d415
      4946 [main] INFO  org.apache.zookeeper.ZooKeeper  - Connection: 0x440751cb41a4d415 closed
      ...
    • SpringBoot对接HDFS样例运行结果。

      Spring Boot启动日志:

      SLF4J: Class path contains multiple SLF4J bindings.
      SLF4J: Found binding in [jar:file:/opt/example/spring-boot-hdfs-1.0-SNAPSHOT.jar!/BOOT-INF/lib/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
      SLF4J: Found binding in [jar:file:/opt/example/spring-boot-hdfs-1.0-SNAPSHOT.jar!/BOOT-INF/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
      SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
      SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
        .   ____          _            __ _ _
       /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
      ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
       \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
        '  |____| .__|_| |_|_| |_\__, | / / / /
       =========|_|==============|___/=/_/_/_/
       :: Spring Boot ::                (v2.4.2)
      2025-01-21 10:43:58.049  INFO 3628949 --- [           main] c.h.s.hdfs.SpringDataHDFSApplication     : Starting SpringDataHDFSApplication v1.0-SNAPSHOT using Java 1.8.0_422 on hdcore-common-001 with PID 3628949 (/opt/example/spring-boot-hdfs-1.0-SNAPSHOT.jar started by root in /opt/example)
      2025-01-21 10:43:58.054  INFO 3628949 --- [           main] c.h.s.hdfs.SpringDataHDFSApplication     : No active profile set, falling back to default profiles: default
      2025-01-21 10:43:58.928  WARN 3628949 --- [kground-preinit] o.s.h.c.j.Jackson2ObjectMapperBuilder    : For Jackson Kotlin classes support please add "com.fasterxml.jackson.module:jackson-module-kotlin" to the classpath
      2025-01-21 10:43:59.695  INFO 3628949 --- [           main] org.eclipse.jetty.util.log               : Logging initialized @3808ms to org.eclipse.jetty.util.log.Slf4jLog
      2025-01-21 10:43:59.845  INFO 3628949 --- [           main] o.s.b.w.e.j.JettyServletWebServerFactory : Server initialized with port: 8080
      2025-01-21 10:43:59.848  INFO 3628949 --- [           main] org.eclipse.jetty.server.Server          : jetty-9.4.35.v20201120; built: 2020-11-20T21:17:03.964Z; git: bdc54f03a5e0a7e280fab27f55c3c75ee8da89fb; jvm 1.8.0_422-BiSheng_JDK8_Enterprise_202.1.0.420.B002-b05
      2025-01-21 10:43:59.886  INFO 3628949 --- [           main] o.e.j.s.h.ContextHandler.application     : Initializing Spring embedded WebApplicationContext
      2025-01-21 10:43:59.886  INFO 3628949 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1180 ms
      2025-01-21 10:44:00.018  INFO 3628949 --- [           main] org.eclipse.jetty.server.session         : DefaultSessionIdManager workerName=node0
      2025-01-21 10:44:00.018  INFO 3628949 --- [           main] org.eclipse.jetty.server.session         : No SessionScavenger set, using defaults
      2025-01-21 10:44:00.021  INFO 3628949 --- [           main] org.eclipse.jetty.server.session         : node0 Scavenging every 660000ms
      2025-01-21 10:44:00.035  INFO 3628949 --- [           main] o.e.jetty.server.handler.ContextHandler  : Started o.s.b.w.e.j.JettyEmbeddedWebAppContext@7ce3cb8e{application,/,[file:///tmp/jetty-docbase.8080.5072591134057219935/],AVAILABLE}
      2025-01-21 10:44:00.036  INFO 3628949 --- [           main] org.eclipse.jetty.server.Server          : Started @4150ms
      2025-01-21 10:44:00.643  INFO 3628949 --- [           main] o.a.h.security.UserGroupInformation      : Login successful for user admintest using keytab file user.keytab. Keytab auto renewal enabled : false
      2025-01-21 10:44:00.643  INFO 3628949 --- [           main] com.huawei.spring.hdfs.LoginUtil         : Login success!!!!!!!!!!!!!!
      2025-01-21 10:44:01.464  WARN 3628949 --- [           main] org.apache.hadoop.util.NativeCodeLoader  : Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
      2025-01-21 10:44:01.471  WARN 3628949 --- [           main] o.a.h.h.s.DomainSocketFactory            : The short-circuit local reads feature cannot be used because libhadoop cannot be loaded.
      2025-01-21 10:44:01.711  INFO 3628949 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
      2025-01-21 10:44:01.975  INFO 3628949 --- [           main] o.e.j.s.h.ContextHandler.application     : Initializing Spring DispatcherServlet 'dispatcherServlet'
      2025-01-21 10:44:01.975  INFO 3628949 --- [           main] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
      2025-01-21 10:44:01.976  INFO 3628949 --- [           main] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
      2025-01-21 10:44:02.004  INFO 3628949 --- [           main] o.e.jetty.server.AbstractConnector       : Started ServerConnector@2133814f{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
      2025-01-21 10:44:02.005  INFO 3628949 --- [           main] o.s.b.web.embedded.jetty.JettyWebServer  : Jetty started on port(s) 8080 (http/1.1) with context path '/'
      2025-01-21 10:44:02.023  INFO 3628949 --- [           main] c.h.s.hdfs.SpringDataHDFSApplication     : Started SpringDataHDFSApplication in 4.513 seconds (JVM running for 6.138)

      访问链接结果如下:

  • 查看HDFS日志获取应用运行情况

    您可以查看HDFS的NameNode日志了解应用运行情况,并根据日志信息调整应用程序。

相关文档