大数据问题排查系列 - HDFS FileSystem API 的正确打开方式,你 GET 了吗?

简介: 大数据问题排查系列 - HDFS FileSystem API 的正确打开方式,你 GET 了吗?

前言

大家好,我是明哥!

本片博文是“大数据问题排查系列”之一,我们首先会聊聊一个问题的现象原因和解决方法,然后给出 HDFS FileSystem API 常见的两种使用方式,最后来看下 HDFS 源码中是如何根据用户的配置文件创建对应的 FileSystem 对象实例的。

以下是正文。

从一个报错聊起

  • 问题现象:某 JAVA 作业需要读取 HDFS 文件系统中的文件,作业提交后报错如下:
java.io.IOException: No FileSystem for scheme: hdfs
    at org.apache.hadoop.fs.FileSystem.getFileSystemClass(FileSystem.java:2584)
    at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:2591)
  • 问题原因:类加载路径上缺少 hdfs 相关 Jar包 hadoop-hdfs-*.jar,导致org.apache.hadoop.fs.FileSystem 创建 FileSystem实列 时没有创建 org.apache.hadoop.hdfs.DistributedFileSystem,所以当配置文件中配置 fs.defaultFS 为 hdfs://nameservice1 时,会寻找 hdfs scheme 即org.apache.hadoop.hdfs.DistributedFileSystem,此时自然找不到,就会报上述错误。
  • 问题解决:只需要确保类的 Classpath 下有对应的 hdfs相关 jar报即可解决上述报错(注意在分布式环境中可能会涉及到不同 classloader下不同的加载机制),具体来讲:
  1. 可以在 pom中添加相关依赖:
<dependency>
        <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.7.4</version>
        </dependency>
  1. 在linux上提交时,可以通过类似以下命令确保类加载路径上包含相关 hdfs jar包:
java -cp ./test-1.0-SNAPSHOT-jar-with-dependencies.jar:`hadoop classpath` com.hundsun.HdfsTest core-site-test.xml hdfs-site-test.xml

HDFS FileSystem api 常见的两种方式

粗略来看,通过 HDFS FileSystem api 创建 FileSystem 实例时,主要有两种方式,两者在如何配置访问不同集群的 HDFS 上略有差异。以下是示例代码。

  1. 方式一:代码使用原生JAVA,目标 hdfs 集群的配置信息,通过导出目标集群中的配置文件core-site 和 hdfs-site.xml并放到特定路径下加载进来
package com.mingge.hdfs.demo;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
public class HdfsTest {
    /**
     *
     1. If there are configuration files in the classpath with default names like core-site.xml, hdfs-site.xml,
     The Configuration class will automatically load them;
     2. If your configuration files are not following the default names like core-site.xml, hdfs-site.xml,
     you must load them explicitly;
     3. If the configuration files are already in the classpath, you can load them this way:
     conf.addResource(Thread.currentThread().getContextClassLoader().getResourceAsStream(coreConfPath));
     4. If the configuration files are not in the classpath, you can load them this way:
     conf.addResource(new Path(coreConfPath));
     */
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        // paths to configuration files
        String coreConfPath = null;
        String hdfsConfPath = null;
        coreConfPath = args[0];
        hdfsConfPath = args[1];
        // you can use absolute paths, like below on windows:
//        coreConfPath = "E:\\git\\test\\src\\main\\resources\\core-site-test.xml";
//        hdfsConfPath = "E:\\git\\test\\src\\main\\resources\\hdfs-site-test.xml";
        // If you use relative paths, the files must be in the cwd, which can be get by System.getProperty("user.dir");
//        coreConfPath = "core-site-test.xml";
//        hdfsConfPath = "hdfs-site-test.xml";
//        conf.addResource(new Path(coreConfPath));
//        conf.addResource(new Path(hdfsConfPath));
        // if the configuraton files are already in the classpath, you can also use below codes to load them:
        conf.addResource(Thread.currentThread().getContextClassLoader().getResourceAsStream(coreConfPath));
        conf.addResource(Thread.currentThread().getContextClassLoader().getResourceAsStream(hdfsConfPath));
//        System.getenv().forEach((key,value) -> System.out.println(key.toString() + value.toString()));
//        System.getProperties().forEach((key,value) -> System.out.println(key.toString() + value.toString()));
//        conf.iterator().forEachRemaining((confItem)-> System.out.println(confItem.toString()));
        System.out.println("user dir is: " + System.getProperty("user.dir"));
        try { FileSystem fileSystem = FileSystem.get(conf);
        for (FileStatus status: fileSystem.listStatus(new Path("/"))){
            System.out.println(status.getPath());}
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  1. 方式二:代码使用 spring 框架,目标HDFS 集群相关配置信息,配置在 properties 配置文件中:
package com.mingge.hdfs.demo;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hdfs.HdfsConfiguration;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
/**
 * Created by xxx on xxx.
 */
@Configuration
public class HdfsConfig {
    private final Logger log = Logger.getLogger(this.getClass());
    @Value("${fs.default.name}")
    String fs_default_name;
    @Value("${fs.namenode.isha:false}")
    boolean fs_namenode_isha;
    @Value("${fs.namenode.address:}")
    private String fs_namenode_address;
    @Value("${hadoop.security.authentication:}")
    String hadoop_auth;
    @Value("${key.user:}")
    String key_user;
    @Value("${key.path:}")
    String key_path;
    @Value("${java.security.krb5.conf:}")
    String kbr5_conf;
    @Value("${dfs.namenode.kerberos.principal:}")
    String dfs_namenode_kerberos_principal;
    @Bean(name = "configuration1")
    public org.apache.hadoop.hdfs.HdfsConfiguration configuration(){
        org.apache.hadoop.hdfs.HdfsConfiguration conf = new HdfsConfiguration();
        if(fs_namenode_isha){
            String clusterName = "clusterName";
            conf.set("fs.defaultFS", fs_default_name);
            conf.set("dfs.nameservices", clusterName);
            if(StringUtils.isBlank(fs_namenode_address)){
                throw new RuntimeException("fs.namenode.address 不能为空");
            }
            int i = 0;
            StringBuilder nodes = new StringBuilder();
            for(String address : fs_namenode_address.split(",")){
                i++;
                nodes.append(",nn").append(i);
                conf.set("dfs.namenode.rpc-address."+clusterName+".nn" + i, address);
            }
            conf.set("dfs.ha.namenodes."+clusterName, nodes.substring(1));
            //conf.setBoolean(name, value);
            conf.set("dfs.client.failover.proxy.provider."+clusterName,
                    "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");
        }else{
            conf.set("fs.default.name",fs_default_name);
        }
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        if (StringUtils.isNotBlank(kbr5_conf)) {
//            System.setProperty("HADOOP_USER_NAME", user); //设置当前window/linux下用户为HBase可访问用户
            System.setProperty("java.security.krb5.conf", kbr5_conf );
            /** 使用Hadoop安全登录 **/
            conf.set("hadoop.security.authentication", hadoop_auth);
            conf.set("dfs.namenode.kerberos.principal", dfs_namenode_kerberos_principal);
            try {
                UserGroupInformation.setConfiguration(conf);
                UserGroupInformation.loginUserFromKeytab(key_user, key_path);
                log.info("=========================kerberos登录成功==============================");
            } catch (IOException e1) {
                log.error("=====================登录错误==================:",e1);
                e1.printStackTrace();
            }
        }
        return conf;
    }
}

HDFS 源码中是如何根据用户的配置文件创建对应的 FileSystem 对象实例的?

通过以上方法一的代码,我们不难发现,只要在类的加载路径上有配置文件core-site.xml和hdfs-site.xml,两行简单的如下语句就可以创建好指向目标 hdfs 集群的 FileSystem对象实例:

Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(conf);

这背后的原理,其实涉及到源码中类 org.apache.hadoop.conf.Configuration 和 org.apache.hadoop.hdfs.HdfsConfiguration,尤其是其如下静态代码块,截图如下:

image.png

Configuration.java

image.png

HdfsConfiguration.java

相关知识点,总结如下:

  • If there are configuration files in the classpath with default names like core-site.xml, hdfs-site.xml, the Configuration class will automatically load them;
  • If your configuration files are not following the default names like core-site.xml, hdfs-site.xml, you must load them explicitly;
  • If the configuration files are already in the classpath, you can load them this way: conf.addResource(Thread.currentThread().getContextClassLoader().getResourceAsStream(coreConfPath));
  • If the configuration files are not in the classpath, you can load them this way: conf.addResource(new Path(coreConfPath));

大家可以在 IDE 工具中导入以上方法一的代码,DEBUG 调试下以了解其中的细节。以下是笔者排查上述问题时,debug过程中的相关截图:

image.png

debug FileSystem.java when no org.apache.hadoop:hadoop-hdfs is in the classpath


image.png

debug FileSystem.java when org.apache.hadoop:hadoop-hdfs is in the classpath


相关实践学习
基于MaxCompute的热门话题分析
Apsara Clouder大数据专项技能认证配套课程:基于MaxCompute的热门话题分析
相关文章
|
XML JSON API
淘宝商品详情(item get)API接口系列,示例说明参考
淘宝商品详情(item_get)API接口是淘宝开放平台(Taobao Open Platform)提供的一个重要接口,允许开发者通过HTTP请求获取淘宝商品的详细信息。以下是对该接口系列的示例说明参考
|
JSON 安全 API
淘宝商品详情API接口(item get pro接口概述)
淘宝商品详情API接口旨在帮助开发者获取淘宝商品的详细信息,包括商品标题、描述、价格、库存、销量、评价等。这些信息对于电商企业而言具有极高的价值,可用于商品信息展示、市场分析、价格比较等多种应用场景。
|
数据采集 监控 数据挖掘
常用电商商品数据API接口(item get)概述,数据分析以及上货
电商商品数据API接口(item get)是电商平台上用于提供商品详细信息的接口。这些接口允许开发者或系统以编程方式获取商品的详细信息,包括但不限于商品的标题、价格、库存、图片、销量、规格参数、用户评价等。这些信息对于电商业务来说至关重要,是商品数据分析、价格监控、上货策略制定等工作的基础。
|
消息中间件 NoSQL Kafka
大数据-52 Kafka 基础概念和基本架构 核心API介绍 应用场景等
大数据-52 Kafka 基础概念和基本架构 核心API介绍 应用场景等
310 5
Hadoop-09-HDFS集群 JavaClient 代码上手实战!详细附代码 安装依赖 上传下载文件 扫描列表 PUT GET 进度条显示(二)
Hadoop-09-HDFS集群 JavaClient 代码上手实战!详细附代码 安装依赖 上传下载文件 扫描列表 PUT GET 进度条显示(二)
206 3
|
分布式计算 Java Hadoop
Hadoop-09-HDFS集群 JavaClient 代码上手实战!详细附代码 安装依赖 上传下载文件 扫描列表 PUT GET 进度条显示(一)
Hadoop-09-HDFS集群 JavaClient 代码上手实战!详细附代码 安装依赖 上传下载文件 扫描列表 PUT GET 进度条显示(一)
289 2
|
分布式计算 Java 大数据
大数据-147 Apache Kudu 常用 Java API 增删改查
大数据-147 Apache Kudu 常用 Java API 增删改查
234 1
|
SQL 分布式计算 DataWorks
DataWorks产品使用合集之使用API调用ODPS SQL时,出现资源被定时任务抢占,该怎么办
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
325 32
|
JSON Go API
使用Go语言和Gin框架构建RESTful API:GET与POST请求示例
使用Go语言和Gin框架构建RESTful API:GET与POST请求示例
|
分布式计算 DataWorks API
DataWorks产品使用合集之使用REST API Reader往ODPS写数据时,如何获取入库时间
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。