Hadoop源码分析之DFSClient对象的创建

本文详细介绍了Hadoop客户端DFSClient的创建过程,重点在DFSClient如何通过DistributedFileSystem的initialize()方法初始化,并利用Java动态代理建立与NameNode的连接。通过RetryInvocationHandler实现通信重试策略,确保客户端与NameNode的通信稳定性。
摘要由CSDN通过智能技术生成

再上一篇文章Hadoop源码分析之DistributedFileSystem中说道:DistributedFileSystem的重点在于其成员变量DFSClient dfs,它执行了文件系统的具体的操作。所以现在就来学习一个DFSClient这个类。

再来看看这个图:

DFSClient就再HDFS客户端那里,DistributedFileSystem就是通过DFSClient来与NameNode和DataNode通信,由于这三类节点可能不在一台机器上面,所以其通信方式就使用了RPC机制进行通信,在Java中RPC可以使用RMI来实现,但是Hadoop没有使用Java RMI方式,而是重新实现了一种节点间通信的方法,称为Hadoop IPC(Inter-Process Communication,进程间通信),因为Java RMI方式进行进程间通信,开销较大,而Hadoop需要精确控制进程间通信,如连接,超时,缓存等通信细节,关于Hadoop IPC在后面的分析中会学习到,现在先来看看DFSClient对象的创建。

DFSClient对象创建

再DistributedFileSystem的initialize()方法中的代码this.dfs = new DFSClient(namenode, conf, statistics);是用于初始化dfs变量,其中namenode为NameNode节点的网络地址,conf为配置信息,statistics用于统计信息的记录。DFSClient有四个构造方法,但最终都是调用的同一个构造方法(参数最多的),代码如下:

public DFSClient(Configuration conf) throws IOException {
    this(NameNode.getAddress(conf), conf);
  }

  public DFSClient(InetSocketAddress nameNodeAddr, Configuration conf
      ) throws IOException {
    this(nameNodeAddr, conf, null);
  }

  public DFSClient(InetSocketAddress nameNodeAddr, Configuration conf,
                   FileSystem.Statistics stats)
    throws IOException {
    this(nameNodeAddr, null, conf, stats);
  }

  /** 
   * Create a new DFSClient connected to the given nameNodeAddr or rpcNamenode.
   * Exactly one of nameNodeAddr or rpcNamenode must be null.
   */
  DFSClient(InetSocketAddress nameNodeAddr, ClientProtocol rpcNamenode,
      Configuration conf, FileSystem.Statistics stats)
    throws IOException {
    this.conf = conf;
    this.stats = stats;
    //和网络相关的一些参数
    /**NameNode网络地址**/
    this.nnAddress = nameNodeAddr;
    /**socket连接超时时间**/
    this.socketTimeout = conf.getInt("dfs.socket.timeout", 
                                     HdfsConstants.READ_TIMEOUT);
    this.datanodeWriteTimeout = conf.getInt("dfs.datanode.socket.write.timeout",
                                            HdfsConstants.WRITE_TIMEOUT);
    this.timeoutValue = this.socketTimeout;
    this.socketFactory = NetUtils.getSocketFactory(conf, ClientProtocol.class);
    // dfs.write.packet.size is an internal config variable
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值