hbase0.98 源码分析-读数据流程

这篇博客详细分析了HBase 0.98版本中读取数据的过程,从客户端的HTable构造及get方法开始,经过HConnection的建立、ExecutorService的配置、RpcCallerFactory的创建,深入到RpcRetryingCaller的调用,重点讲解了如何通过protobuf进行region server的定位和通信。博主还提到了0.96版本后hbase采用protobuf作为通信架构,并给出了Client.proto中的Get相关接口。最后,简述了region server端的HRegionServer.get()方法处理请求的流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先贴一个总体架构图吧:



我们的客户端程序读取数据(以get为例)通过HTable和Get进行操作,我们从客户端代码开始分析读取数据的流程


conf 里主要是配置zookeeper的连接配置的:


HTable的构造函数:

public HTable(Configuration conf, final TableName tableName)
  throws IOException {
    this.tableName = tableName;
    this.cleanupPoolOnClose = this.cleanupConnectionOnClose = true;
    if (conf == null) {
      this.connection = null;
      return;
    }
    this.connection = HConnectionManager.getConnection(conf);
    this.configuration = conf;

    this.pool = getDefaultExecutor(conf);
    this.finishSetup();
  }

这个方法有三个比较重要的操作:

1、获取HConnection


HConnectionManager 内部缓存着 HConnectionKey 和 HConnectionImplementation 的映射,如果之前已经有连接,就直接从缓存中取就行,如果没有直接创建一个连接:
 HConnectionImplementation connection = CONNECTION_INSTANCES.get(connectionKey);
      if (connection == null) {
        connection = (HConnectionImplementation)createConnection(conf, true);
        CONNECTION_INSTANCES.put(connectionKey, connection);

createConnection 方法通过反射生成HConnectionImplementation对象,通过这个反射对象进行连接

 String className = conf.get("hbase.client.connection.impl",
      HConnectionManager.HConnectionImplementation.class.getName());
    Class<?> clazz = null;
    try {
      clazz = Class.forName(className);

反射之后,创建连接:

 Constructor<?> constructor =
        clazz.getDeclaredConstructor(Configuration.class,
          boolean.class, ExecutorService.class, User.class);
      constructor.setAccessible(true);
      return (HConnection) constructor.newInstance(conf, managed, pool, user);

2、获取ExecutorService,这是一个线程池,这个线程是的一个线程对应一个regionserver

3、finishSetup:配置HTable相关参数,创建 rpcCallerFactory 和 rpcCallerFactory,用于和 regionserver 进行 rpc 调用;还会初始化一个AsyncProcess,用于处理autoflush为false 或者 multiputs 的操作:

/** The Async process for puts with autoflush set to false or multiputs */
  protected AsyncProcess<Object> ap;
……

 this.rpcCallerFactory = RpcRetryingCallerFactory.instantiate(configuration);
    this.rpcControllerFactory = RpcControllerFactory.instantiate(configuration);
    ap = new AsyncProcess<Object>(connection, tableName, pool, null,
        configuration, rpcCallerFactory, rpcControllerFactory);


ok,HTable初始化基本完成,进入HTable.get 方法

 public Result get(final Get get) throws IOException {
    // have to instanatiate this and set the priority here since in protobuf util we don't pass in
    // the tablename... an unfortunate side-effect of public interfaces :-/ In 0.99+ we put all the
    // logic back into HTable
    final PayloadCarryingRpcController controller = rpcControllerFactory.newController();
    controller.setPriority(tableName);
    RegionServerCallable<Result> callable =
        new RegionServerCallable<Result>(this.connection, getName(), get.getRow()) {
          public Result call() throws IOException {
            return ProtobufUtil.
### 下载 HBase 0.98.23 源码压缩包 要下载 HBase 0.98.23 的源码压缩包,可以通过 Apache 官方镜像站点或者 Maven 中央仓库完成。以下是具体方法: #### 方法一:通过 Apache 镜像站点下载 Apache 提供了官方的镜像站点用于下载各个版本的 HBase 软件。对于 HBase 0.98.23 版本,可以直接访问以下链接并找到对应的 `src` 文件: - 访问地址:[https://archive.apache.org/dist/hbase/](https://archive.apache.org/dist/hbase/) - 找到对应版本路径:`hbase-0.98.23/` - 下载源码文件:`hbase-0.98.23-src.tar.gz` 命令如下所示: ```bash wget https://archive.apache.org/dist/hbase/0.98.23/hbase-0.98.23-src.tar.gz ``` 此方式适用于需要稳定版本的情况,并且能够验证文件签名和校验值。 #### 方法二:通过 Maven 中央仓库下载 如果更倾向于使用构建工具管理依赖项,则可以从 Maven 中央仓库拉取 HBase 源码。执行以下命令克隆项目或直接下载 JAR 包及其关联资源。 Maven 坐标定义如下: ```xml <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>0.98.23-hadoop2</version> </dependency> ``` 运行 Maven 构建脚本来提取所需模块即可获得完整的源代码结构[^4]。 需要注意的是,在实际部署过程中还需要设置好 Java 环境以及相关配置文件如 `hbase-site.xml`, 同时确保所有节点间网络互通正常以便集群模式下顺利工作[^1]. 另外提醒一点,HBase作为分布式数据库系统其稳定性很大程度上取决于底层存储引擎(HDFS)的表现,因此建议同步升级至兼容性更高的Hadoop版本以减少潜在风险. ```python import os def download_hbase_source(version="0.98.23"): url = f"https://archive.apache.org/dist/hbase/{version}/hbase-{version}-src.tar.gz" file_name = f"hbase-{version}-src.tar.gz" try: import urllib.request with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file: data = response.read() # a `bytes` object out_file.write(data) print(f"{file_name} has been downloaded successfully.") except Exception as e: print(f"Failed to download {file_name}. Error: {e}") if __name__ == "__main__": download_hbase_source() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值