HDFS的编码API入口
根据hadoop2.10.1版本的API DOC来看,对HDFS的操作一共有两种API入口:
- 通过org.apache.hadoop.fs.FileContext的静态方法创建。
- 通过org.apache.hadoop.fs.FileSystem的静态方法创建。
其中FileContext是通过org.apache.hadoop.fs.AbstractFileSystem抽象类创建org.apache.hadoop.fs.HDFS类作为DFSClient;
FileSystem作为抽象类,创建的是org.apache.hadoop.hdfs.DistributedFileSystem类作为DFSClient。
无论使用哪种方式进行RPC请求,Maven的依赖都是一样的,如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wondersgroup.test</groupId>
<artifactId>log4j2test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.10.1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
问题描述
标题中出现的问题,是在使用FileSystem创建RPC服务的时候出现的。
原因分析
从源码分析,看一下FileSystem创建RPC服务的过程
public static FileSystem get(URI uri, Configuration conf) throws IOException {
String scheme = uri.getScheme();
String authority = uri.getAuthority();
if (scheme == null && authority == null) { // use default FS
return get(conf);
}
if (scheme != null && authority == null) { // no authority
URI defaultUri = getDefaultUri(conf);
if (scheme.equals(defaultUri.getScheme()) // if scheme matches default
&& defaultUri.getAuthority() != null) { // & default has authority
return get(defaultUri, conf); // return default
}
}
String disableCacheName = String.format("fs.%s.impl.disable.cache", scheme);
if (conf.getBoolean(disableCacheName, false)) {
LOGGER.debug("Bypassing cache to create filesystem {}", uri);
return createFileSystem(uri, conf); //!!!CACHE.get()方法最终的逻辑也是这个方法,这个方法最终创建RPC
}
return CACHE.get(uri, conf);
}
private static FileSystem createFileSystem(URI uri, Configuration conf)
throws IOException {
Tracer tracer = FsTracer.get(conf);
try(TraceScope scope = tracer.newScope("FileSystem#createFileSystem")) {
scope.addKVAnnotation("scheme", uri.getScheme());
Class<?> clazz = getFileSystemClass(uri.getScheme(), conf); //!!!这个方法最终目的是获取到org.apache.hadoop.hdfs.DistributedFileSystem这个类
FileSystem fs = (FileSystem)ReflectionUtils.newInstance(clazz, conf);
fs.initialize(uri, conf);
return fs;
}
}
public static Class<? extends FileSystem> getFileSystemClass(String scheme,
Configuration conf) throws IOException {
if (!FILE_SYSTEMS_LOADED) {
loadFileSystems(); //!!!这个方法会执行,主要的工作是通过java SPI机制,通过ServiceLoader加载classpath*:META-INF/services/org.apache.hadoop.fs.FileSystem对应的具体实现
}
LOGGER.debug("Looking for FS supporting {}", scheme);
Class<? extends FileSystem> clazz = null;
if (conf != null) {
String property = "fs." + scheme + ".impl";
LOGGER.debug("looking for configuration option {}", property);
clazz = (Class<? extends FileSystem>) conf.getClass(
property, null);
} else {
LOGGER.debug("No configuration: skipping check for fs.{}.impl", scheme);
}
if (clazz == null) {
LOGGER.debug("Looking in service filesystems for implementation class");
clazz = SERVICE_FILE_SYSTEMS.get(scheme); //!!!SERVICE_FILE_SYSTEMS是在上面的if判断中,通过SPI注入进去的
} else {
LOGGER.debug("Filesystem {} defined in configuration option", scheme);
}
if (clazz == null) {
//!!!!!!!!BUG出现在这里!!!!
throw new UnsupportedFileSystemException("No FileSystem for scheme "
+ "\"" + scheme + "\"");
}
LOGGER.debug("FS for {} is {}", scheme, clazz);
return clazz;
}
标题中的问题,就是出现在上面的代码末尾处。
首先看property=fs.hdfs.impl和Configuration,这个参数优先级最高,但Configuration的默认配置项并不会包含fs.hdfs.impl这个变量的内容,Configuration默认情况下,只会加载classpath*:core-default.xml,core-site.xml,mapred-default.xml,mapred-site.xml,yarn-default.xml,yarn-site.xml中的配置,而fs.hdfs.impl中并不包含在上述文件中,因此除非开发人员单独定义这个变量,否则不会在这一步获取到org.apache.hadoop.hdfs.DistributedFileSystem。
最后通过SPI获取,而这也正是问题产生的原因,首先看一下能够正常运行的情况,此时META-INF/services/org.apache.hadoop.fs.FileSystem文件中的内容如下:
No FileSystem for scheme:hdfs
对于Maven中使用了hadoop-hdfs-client和hadoop-common的情况,就有可能出现这个问题,造成这个问题的原因是META-INF/services/org.apache.hadoop.fs.FileSystem文件发生了冲突
可以发现,这个文件居然在hadoop-common中也存在,而且其内容如下:
org.apache.hadoop.fs.LocalFileSystem --!!!在hadoop-hdfs-client中的类是org.apache.hadoop.hdfs.DistributedFileSystem
org.apache.hadoop.fs.viewfs.ViewFileSystem
org.apache.hadoop.fs.ftp.FTPFileSystem
org.apache.hadoop.fs.HarFileSystem
org.apache.hadoop.fs.http.HttpFileSystem
org.apache.hadoop.fs.http.HttpsFileSystem
也就是说,在使用maven-assembly-plugin打jar包的时候,这个文件最终使用的是hadoop-common中的文件,从而无法支持HDFS格式的URI。
错误纠正
上面的理解有错误,Java SPI会扫描classpath下所有的META-INF/services/org.apache.hadoop.fs.FileSystem文件,因此理论上是可以实例化到DistributeFileSystem的(不会因为hadoop-common存在相同的文件就导致错误),上面的问题应该是打包后缺失了hdfs-client的SPI文件导致,总之出现这个问题时,可以通过下面的解决方案处理。
解决方案
在Configuration中添加fs.hdfs.impl即可解决。
题外话
文章开头提到过HDFS API有两个入口,使用FileContext入口时,不会出现这个问题,因此AbstractFileSystem不会通过SPI加载,而是直接通过读取Configuration的默认配置加载,且加载使用的property.key也不一样,有兴趣的可以看一下源码。