Alluxio源码分析读数据:打开文件选项OpenFileOptions

        OpenFileOptions是为读数据打开一个文件方法的选项,提供了打开文件的多种选择。在OpenFileOptions内部,封装了两个重要的成员变量,如下:

  // 定位策略
  private FileWriteLocationPolicy mLocationPolicy;
  
  // 读取类型
  private ReadType mReadType;

        其中mLocationPolicy为FileWriteLocationPolicy类型的定位策略,而mReadType为ReadType类型的读取类型。关于定位策略是什么可以参阅《Alluxio之定位策略》一文,而关于读取类型是什么可以参照《Alluxio之IO选项:读写类型》一文,这里均不再解释。

        而OpenFileOptions只有一个私有无参构造函数,基于默认配置信息构造一个OpenFileOptions实例,代码如下:

  /**
   * 私有无参构造函数,基于默认配置信息构造一个OpenFileOptions实例
   * Creates a new instance with defaults based on the configuration.
   */
  private OpenFileOptions() {
    
	// 获取读取类型ReadType:取参数alluxio.user.file.readtype.default配置的值
	mReadType =
        ClientContext.getConf().getEnum(Constants.USER_FILE_READ_TYPE_DEFAULT, ReadType.class);
    try {
    	
      // 获取定位策略:取参数alluxio.user.file.write.location.policy.class配置的值
      mLocationPolicy =
          CommonUtils.createNewClassInstance(ClientContext.getConf()
                  .<FileWriteLocationPolicy>getClass(Constants.USER_FILE_WRITE_LOCATION_POLICY),
              new Class[]{}, new Object[]{});
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }
        其中,读取类型ReadType取参数alluxio.user.file.readtype.default配置的值,定位策略取参数alluxio.user.file.write.location.policy.class配置的值。同时,OpenFileOptions对外提供了一个静态方法defaults(),外部调用者可以基于OpenFileOptions.defaults()形式获取一个默认配置的OpenFileOptions实例,代码如下:

  /**
   * 静态方法,获取一个默认配置的OpenFileOptions实例
   * @return the default {@link InStreamOptions}
   */
  public static OpenFileOptions defaults() {
	  
	// 调用OpenFileOptions的无参构造函数构造一个实例
    return new OpenFileOptions();
  }
        OpenFileOptions还有一个比较重要的方法toInStreamOptions(),它是基于当前OpenFileOptions实例成员变量获取一个默认实现的输入流选项InStreamOptions的实例,代码如下:

  /**
   * 基于当前OpenFileOptions实例成员变量获取一个默认实现的输入流选项InStreamOptions的实例
   * @return the {@link OutStreamOptions} representation of this object
   */
  public InStreamOptions toInStreamOptions() {
    return InStreamOptions.defaults().setReadType(mReadType).setLocationPolicy(mLocationPolicy);
  }
        InStreamOptions是在真正读取数据时使用的一个输入流选项,其内部也是封装了FileWriteLocationPolicy和ReadType两个成员变量,且默认构造时二者的取值与OpenFileOptions一致,这里不再赘述。

        剩余的,便是一些比较简单的get和set方法,不再详细介绍,具体代码如下:

  /**
   * @return the location policy to use when storing data to Alluxio
   */
  public FileWriteLocationPolicy getLocationPolicy() {
    return mLocationPolicy;
  }

  /**
   * 获取Alluxio的存储策略
   * @return the Alluxio storage type
   */
  public AlluxioStorageType getAlluxioStorageType() {
    return mReadType.getAlluxioStorageType();
  }

  /**
   * @param policy the location policy to use when storing data to Alluxio
   * @return the updated options object
   */
  public OpenFileOptions setLocationPolicy(FileWriteLocationPolicy policy) {
    mLocationPolicy = policy;
    return this;
  }

  /**
   * @param readType the {@link ReadType} for this operation. Setting this will
   *        override the {@link AlluxioStorageType}.
   * @return the updated options object
   */
  public OpenFileOptions setReadType(ReadType readType) {
    mReadType = readType;
    return this;
  }

  /**
   * @return the name : value pairs for all the fields
   */
  @Override
  public String toString() {
    return Objects.toStringHelper(this).add("locationPolicy", mLocationPolicy)
        .add("readType", mReadType).toString();
  }
        getAlluxioStorageType()方法是根据读取类型mReadType获取Alluxio的存储策略。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值