我正在尝试处理连接到 HBase 时的故障情况。目前,我正在使用 HBaseAdmin.checkHBaseAvailable(conf);
检查 HBase 是否正在运行。但是这个 api 会在 ~40 秒后在 MasterNotRunning
或 ZookeeperConnectionException
的情况下抛出异常。因此,我尝试设置以下重试次数,hbase.client.retries.number = 1
和 zookeeper.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/