当前位置: 首页 > news >正文

Hadoop 客户端 FileSystem加载过程

如何使用hadoop客户端

public class testCreate {public static void main(String[] args) throws IOException {System.setProperty("HADOOP_USER_NAME", "hdfs");String pathStr = "/home/hdp/shanshajia";Path path = new Path(pathStr);Configuration configuration = new HdfsConfiguration();FileSystem fs = path.getFileSystem(configuration);}
}

一般我们会这么使用,那么问题来了,FileSystem是个抽象类,我们到底用哪一个呢?或者说jvm怎么知道我们要加载哪些FileSystem的实现类呢?

Java SPI机制

SPI 全称 Service Provider Interface ,是 Java 提供的一套用来被第三方实现或者扩展的 API,它可以用来启用框架扩展和替换组件。

Java SPI 实际上是 基于接口的编程+策略模式+配置文件 组合实现的动态加载机制。

Java SPI 就是提供这样的一个机制:为某个接口寻找服务实现的机制。

将装配的控制权移到程序之外,在模块化设计中这个机制尤其重要。

所以 SPI 的核心思想就是解耦

Hadoop 客户端 SPI

当我们第一次视图创建一个FileSystem,会调用createFileSystem

  private static FileSystem createFileSystem(URI uri, Configuration conf) throws IOException {Tracer tracer = FsTracer.get(conf);TraceScope scope = null;if (tracer != null) {scope = tracer.newScope("FileSystem#createFileSystem");scope.addKVAnnotation("scheme", uri.getScheme());}try {Class<?> clazz = getFileSystemClass(uri.getScheme(), conf);if (clazz == null) {throw new IOException("No FileSystem for scheme: " + uri.getScheme());}FileSystem fs = (FileSystem)ReflectionUtils.newInstance(clazz, conf);fs.tracer = tracer;fs.initialize(uri, conf);return fs;} finally {if (scope != null) scope.close();}}
  public static Class<? extends FileSystem> getFileSystemClass(String scheme,Configuration conf) throws IOException {if (!FILE_SYSTEMS_LOADED) {loadFileSystems();}Class<? extends FileSystem> clazz = null;if (conf != null) {clazz = (Class<? extends FileSystem>) conf.getClass("fs." + scheme + ".impl", null);}if (clazz == null) {clazz = SERVICE_FILE_SYSTEMS.get(scheme);}if (clazz == null) {throw new IOException("No FileSystem for scheme: " + scheme);}return clazz;}

1、尝试使用配置的fs

<property><name>fs.hdfs.impl</name><value>DistributedFileSystem</value>
</property>

2、如果配置里没有,这个时候用加载的

hadoop-hdfs-project/hadoop-hdfs/src/main/resources/META-INF/services,可以看到,这个目录下有一个org.apache.hadoop.fs.FileSystem文件

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.org.apache.hadoop.hdfs.DistributedFileSystem
org.apache.hadoop.hdfs.web.HftpFileSystem
org.apache.hadoop.hdfs.web.HsftpFileSystem
org.apache.hadoop.hdfs.web.WebHdfsFileSystem
org.apache.hadoop.hdfs.web.SWebHdfsFileSystem

 通过 loadFileSystems这个方法会加载文件中的所有类,并且根据scheme生成一个map,key是scheme,value是fs

  private static void loadFileSystems() {synchronized (FileSystem.class) {if (!FILE_SYSTEMS_LOADED) {ServiceLoader<FileSystem> serviceLoader = ServiceLoader.load(FileSystem.class);Iterator<FileSystem> it = serviceLoader.iterator();while (it.hasNext()) {FileSystem fs = null;try {fs = it.next();try {SERVICE_FILE_SYSTEMS.put(fs.getScheme(), fs.getClass());} catch (Exception e) {LOG.warn("Cannot load: " + fs + " from " +ClassUtil.findContainingJar(fs.getClass()), e);}} catch (ServiceConfigurationError ee) {LOG.warn("Cannot load filesystem", ee);}}FILE_SYSTEMS_LOADED = true;}}}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 前端工具vscode 提交代码git操作
  • vue项目input框使用lodash的debounce防抖
  • 创建vue工程、Vue项目的目录结构、Vue项目-启动、API风格
  • 集成Vision Transformer 的概率模型改进了复杂的息肉分割
  • 几行代码使用Go语言对接第三方工具之集成gin、kite框架、通过原生不借助其他软件实现内网穿透工具
  • Make New 函数 ---golang
  • 第八篇【传奇开心果系列】Python微项目技术点案例示例:以微项目开发为案例,深度解读Dearpygui 编写图形化界面桌面程序的优势
  • FreeRTOS_事件组_学习笔记
  • Unidac连接Excel文件
  • Stream流的使用
  • Windows批处理命令和概念
  • php部分特性漏洞学习
  • 【设计模式】JAVA Design Patterns——Bridge(桥接模式)
  • 面试被问到不懂的东西,是直接说不懂还是坚持狡辩一下?
  • test_mqtt/python
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • Apache的80端口被占用以及访问时报错403
  • Consul Config 使用Git做版本控制的实现
  • django开发-定时任务的使用
  • ES6简单总结(搭配简单的讲解和小案例)
  • exif信息对照
  • iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码...
  • Javascript弹出层-初探
  • Java教程_软件开发基础
  • Laravel Telescope:优雅的应用调试工具
  • Vue--数据传输
  • 对话 CTO〡听神策数据 CTO 曹犟描绘数据分析行业的无限可能
  • 反思总结然后整装待发
  • 服务器之间,相同帐号,实现免密钥登录
  • 计算机常识 - 收藏集 - 掘金
  • 检测对象或数组
  • 理清楚Vue的结构
  • 前端之React实战:创建跨平台的项目架构
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 一个项目push到多个远程Git仓库
  • 一天一个设计模式之JS实现——适配器模式
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 运行时添加log4j2的appender
  • 正则表达式
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • ​Java并发新构件之Exchanger
  • (2024)docker-compose实战 (9)部署多项目环境(LAMP+react+vue+redis+mysql+nginx)
  • (42)STM32——LCD显示屏实验笔记
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (三)c52学习之旅-点亮LED灯
  • (转)ORM
  • (转)Sql Server 保留几位小数的两种做法
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .NET 4 并行(多核)“.NET研究”编程系列之二 从Task开始
  • .NET 使用配置文件
  • .net访问oracle数据库性能问题
  • .NET开源纪元:穿越封闭的迷雾,拥抱开放的星辰
  • @SuppressWarnings(unchecked)代码的作用
  • [ linux ] linux 命令英文全称及解释