在Linux环境中调测HDFS应用
操作场景
HDFS应用程序支持在Linux环境中运行。在程序代码完成开发后,可以上传Jar包至准备好的Linux环境中运行。
HDFS应用程序运行完成后,可直接通过运行结果查看应用程序运行情况,也可以通过HDFS日志获取应用运行情况。
前提条件
- 已安装客户端时:
- 已安装HDFS客户端。
- 当客户端所在主机不是集群中的节点时,需要在客户端所在节点的hosts文件中设置主机名和IP地址映射。主机名和IP地址请保持一一对应。
- 未安装客户端时:
- Linux环境已安装JDK,版本号需要和IDEA导出Jar包使用的JDK版本一致。
- 当Linux环境所在主机不是集群中的节点时,需要在Linux环境所在节点的hosts文件中设置主机名和IP地址映射。主机名和IP地址请保持一一对应。
已安装客户端时编译并运行程序
- 进入样例工程本地根目录,在Windows命令提示符窗口中执行下面命令进行打包。
mvn -s "{maven_setting_path}" clean package
- “{maven_setting_path}”为本地Maven的“settings.xml”文件路径,例如“C:\Users\Developer\settings.xml”。
- 打包成功之后,在工程根目录的“target”子目录下获取打好的jar包,例如“HDFSTest-XXX.jar”,jar包名称以实际打包结果为准。
- 将导出的Jar包上传至集群客户端运行环境的任意目录下,例如“/opt/client”,然后在该目录下创建“conf”目录,将需要的配置文件复制至“conf”目录,具体操作请参考准备运行环境。
对于Spring Boot对接HDFS样例,需要将所有的配置文件放置到“hdfs-examples”工程的“src/main/resource”目录。并参考SpringBoot对接HDFS修改application.properties文件。
- 配置环境变量:
cd /opt/client
source bigdata_env
- 执行如下命令,运行Jar包。
hadoop jar HDFSTest-XXX.jar com.huawei.bigdata.hdfs.examples.HdfsExample
hadoop jar HDFSTest-XXX.jar com.huawei.bigdata.hdfs.examples.ColocationExample
如果运行Spring Boot对接HDFS样例,需执行以下命令:
java -jar spring-boot-hdfs-*.jar
在运行com.huawei.bigdata.hdfs.examples.ColocationExample时,HDFS的配置项“fs.defaultFS”不能配置为“viewfs://ClusterX”。
- 对于Spring Boot对接HDFS样例,在浏览器上面访问如下链接:
- http://{应用所在IP}:8080/save?fileName=test.txt : 向HDFS的目录“/tmp/examples”中保存test.txt数据。
- http://{应用所在IP}:8080/read?fileName=test.txt :读取HDFS的目录“/tmp/examples”中test.txt数据。
- http://{应用所在IP}:8080/remove?fileName=test.txt:删除HDFS的目录“/tmp/examples”中test.txt文件。
未安装客户端时编译并运行程序
- 进入工程本地根目录,在Windows命令提示符窗口中执行下面命令进行打包。
mvn -s "{maven_setting_path}" clean package
- 上述打包命令中的{maven_setting_path}为本地Maven的“settings.xml”文件路径。
- 打包成功之后,在工程根目录的target子目录下获取打好的jar包。
- 将导出的Jar包上传至Linux运行环境的任意目录下,例如“/opt/client”。
- 将工程中的“lib”文件夹和“conf”文件夹上传至和Jar包相同的Linux运行环境目录下(其中“lib”目录汇总包含了工程中依赖的所有的Jar包,“conf”目录包含运行jar包所需的集群相关配置文件,请参考准备运行环境)。
对于Spring Boot对接HDFS样例,需要将所有的配置文件放置到“hdfs-examples”工程的“src/main/resource”目录。并参考SpringBoot对接HDFS修改application.properties文件。
- 执行如下命令运行Jar包。
java -cp HDFSTest-XXX.jar:conf/:lib/* com.huawei.bigdata.hdfs.examples.HdfsExample
java -cp HDFSTest-XXX.jar:conf/:lib/* com.huawei.bigdata.hdfs.examples.ColocationExample
如果运行Spring Boot对接HDFS样例,需执行以下命令:
java -jar spring-boot-hdfs-*.jar
在运行“com.huawei.bigdata.hdfs.examples.ColocationExample”时,HDFS的配置项“fs.defaultFS”不能配置为“viewfs://ClusterX”。
- 对于Spring Boot对接HDFS样例,在浏览器上面访问如下链接:
- http://{应用所在IP}:8080/save?fileName=test.txt : 向HDFS的目录“/tmp/examples”中保存test.txt数据。
- http://{应用所在IP}:8080/read?fileName=test.txt :读取HDFS的目录“/tmp/examples”中test.txt数据。
- http://{应用所在IP}:8080/remove?fileName=test.txt:删除HDFS的目录“/tmp/examples”中test.txt文件。
查看调测结果
- 查看运行结果获取应用运行情况
- HdfsExample Linux样例程序运行结果如下所示。
0 [main] INFO org.apache.hadoop.security.UserGroupInformation - Login successful for user hdfsDevelop using keytab file user.keytab 1 [main] INFO com.huawei.hadoop.security.LoginUtil - Login success!!!!!!!!!!!!!! 568 [main] WARN org.apache.hadoop.util.NativeCodeLoader - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 582 [main] WARN org.apache.hadoop.hdfs.shortcircuit.DomainSocketFactory - The short-circuit local reads feature cannot be used because libhadoop cannot be loaded. 793 [main] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to create path /user/hdfs-examples 969 [main] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to write. 1068 [main] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to append. 1191 [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. 1191 [main] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to read. 1202 [main] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to delete the file /user/hdfs-examples/test.txt 1210 [main] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to delete path /user/hdfs-examples 1223 [hdfs_example_0] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to create path /user/hdfs-examples/hdfs_example_0 1224 [hdfs_example_1] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to create path /user/hdfs-examples/hdfs_example_1 1261 [hdfs_example_0] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to write. 1264 [hdfs_example_1] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to write. 2807 [hdfs_example_0] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to append. 2810 [hdfs_example_1] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to append. 2861 [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. 2861 [hdfs_example_0] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to read. 2866 [hdfs_example_0] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to delete the file /user/hdfs-examples/hdfs_example_0/test.txt 2874 [hdfs_example_0] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to delete path /user/hdfs-examples/hdfs_example_0 2874 [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. 2874 [hdfs_example_1] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to read. 2879 [hdfs_example_1] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to delete the file /user/hdfs-examples/hdfs_example_1/test.txt 2885 [hdfs_example_1] INFO com.huawei.bigdata.hdfs.examples.HdfsExample - success to delete path /user/hdfs-examples/hdfs_example_1
- ColocationExample Linux样例程序运行结果如下所示。
0 [main] INFO com.huawei.hadoop.security.LoginUtil - JaasConfiguration loginContextName=Client principal=hdfsDevelop useTicketCache=false keytabFile=/opt/hdfsDemo/conf/user.keytab 817 [main] INFO org.apache.hadoop.security.UserGroupInformation - Login successful for user hdfsDevelop using keytab file user.keytab 817 [main] INFO com.huawei.hadoop.security.LoginUtil - Login success!!!!!!!!!!!!!! 1380 [main] WARN org.apache.hadoop.util.NativeCodeLoader - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable ... 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. ...
- Spring对接HDFS的样例运行结果。
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)访问链接结果如下:



- HdfsExample Linux样例程序运行结果如下所示。
- 查看HDFS日志获取应用运行情况
您可以查看HDFS的namenode日志了解应用运行情况,并根据日志信息调整应用程序。