用于检查 hbase 是否已启动并正在运行的 Java api

标签 java exception-handling error-handling hbase

我正在尝试处理连接到 HBase 时的故障情况。目前,我正在使用 HBaseAdmin.checkHBaseAvailable(conf); 检查 HBase 是否正在运行。但是这个 api 会在 ~40 秒后在 MasterNotRunningZookeeperConnectionException 的情况下抛出异常。因此,我尝试设置以下重试次数,hbase.client.retries.number = 1zookeeper.recovery.retry = 1。这让我在 6-7 秒内处理异常。但是,我需要一种更快的方法来了解 HBase 集群是否正在运行,因为我在我的应用程序中通过同步调用登录到 HBase。

最佳答案

以这种方式检查连接怎么样:

import java.net.InetSocketAddress;
import org.apache.hadoop.hbase.ipc.HBaseRPC;
import org.apache.hadoop.hbase.ipc.HMasterInterface;
import org.apache.hadoop.hbase.ipc.RpcEngine;
...

public static boolean isRunning() {
    boolean result = false;
    Configuration conf = HBaseConfiguration.create();
    conf.clear();
    conf.set("hbase.zookeeper.quorum", "myhost");
    conf.set("hbase.zookeeper.property.clientPort", "2181");
    conf.set("hbase.master", "myhost:60000");

    RpcEngine rpcEngine = null;
    try {

        InetSocketAddress isa = new InetSocketAddress("myhost", 60000);
        rpcEngine = HBaseRPC.getProtocolEngine(conf);
        HMasterInterface master = rpcEngine.getProxy(HMasterInterface.class,
                HMasterInterface.VERSION, isa, conf, 1000);
        result = master.isMasterRunning();
    }
    catch (Exception e) {
        // ...
    }
    finally {
        if (rpcEngine != null) {
            rpcEngine.close();
        }
    }
    return result;
}

注意:我这里假设版本 >= 0.94.5

关于用于检查 hbase 是否已启动并正在运行的 Java api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18877594/

相关文章:

java - 如何为设备所有者配置自定义 Android Google 设置向导?

exception-handling - 为什么 resharper 说 'Catch clause with single ' throw '语句是多余的'?

c# - ASP.NET C# 捕获类中的所有异常

c - 标准错误和标准输出

python - 导入模块时的错误处理

java - 输出到java文件

Java ArrayList 具有空元素的大小 []

java - 短语查询和使用 shingle 过滤器有什么区别?

c# - 为什么当内部 "finally"抛出时外部 "catch"没有被执行?

java - 捕获 Java 错误