FileSystem、Path与Configure

1. FileSystem

/1/
我们通过断点调试可知,当host一致时,返回的是同一个对象。

FileSystem fs1=new Path("hdfs://cluster-host1:9000/a/b/c").getFileSystem(conf1);
FileSystem fs2=new Path("hdfs://cluster-host1:9000/x/y/z").getFileSystem(conf2);

返回的是同一个对象:fs1==fs2;

通过以下3个静态方法获得FileSystem文件系统对象:

public static FileSystem.get(Configuration conf) throws IOException
public static FileSystem.get(URI uri, Configuration conf) throws IOException
public statis FileSystem get(URI uri , Configuration conf ,String user) throws IOException

那么为什么我们还能通过Path对象的getFileSystem来获取呢?我们看Path类的该方法:

  /** Return the FileSystem that owns this Path. */
  public FileSystem getFileSystem(Configuration conf) throws IOException {
    return FileSystem.get(this.toUri(), conf);
  }

可以看到,最终还是调用了FileSystem的静态方法。

(1) 返回默认的文件系统,读取配置文件在core-site.xml中指定的,如果没有指定,则使用默认的本地文件系统。
(2) 通过URI来明确执行要使用的文件系统。
(3) 加入安全。

/2/
FAQ:
Q:

Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(URI.create(“/user/algo/wy/tmp”),conf);
这是打开hdfs系统上的文件的常用写法。但是我不明白:
conf感觉就是一个空的配置文件,一个空的配置文件相当于里面没有携带任何有用的信息,为什么FileSystem的get函数还一定要加入conf参数呢,保留这个conf参数的作用是什么?

A:

Configuration conf=new Configuration()
创建一个Configuration对象时,其构造方法会默认加载hadoop中的两个配置文件,分别是hdfs-site.xml以及core-site.xml,这两个文件中会有访问hdfs所需的参数值,主要是fs.default.name,指定了hdfs的地址,有了这个地址客户端就可以通过这个地址访问hdfs了。即可理解为configuration就是hadoop中的配置信息。

FileSystem fs=FileSystem.get(URI.create(“/user/xxx/tmp”),conf);
URI.create是解析参数的值(就是给的地址)
以下是FileSystem的方法:
get(Configuration conf) 根据conf获取具体的文件系统对象
get(URI uri, Configuration conf) 基于uri和conf创建文件系统对象
get(URI uri, Configuration conf, String user) 基于uri,conf和user获取文件系统
getLocal(Configuration conf) 获取本地文件系统
newInstance(Configuration conf) 返回唯一的文件系统对象,该方法总是返回新的对象
newInstance(URI uri, Configuration conf) 基于uri返回新的文件系统对象
newInstance(URI uri, Configuration conf, String user) 基于uri,conf和user获


2. Path

/1/
先来看一下Path的内部:

可以将路径视为一个uri,如: hdfs://host1:9000/a/x.txt

3. Configuration

/1/
那么配置文件是如何生效的呢?
(1)FileSystem get(Configuration conf) 将会使用默认的配置,我们看源码:

  public static FileSystem get(Configuration conf) throws IOException {
    return get(getDefaultUri(conf), conf);
  }

还是需要一个URI,这个URI的获取如下:

  public static URI getDefaultUri(Configuration conf) {
    return URI.create(fixName(conf.get(FS_DEFAULT_NAME_KEY, DEFAULT_FS)));
  }

其中:

 public static final String  FS_DEFAULT_NAME_KEY = "fs.defaultFS";
 public static final String DEFAULT_FS = 
                   CommonConfigurationKeys.FS_DEFAULT_NAME_DEFAULT;
 public static final String  FS_DEFAULT_NAME_DEFAULT = "file:///";

/2/
测试

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值