我们知道在使用HBase的过程中首要的是和服务器端取得链接,那么客户端是如何去链接的,它是怎么找到master和regionserver的? 参与该过程中的主要组件又有哪些?这些组件之间是如何协同工作的呢? 今天就让我们来一起解析.
HBase的连接代码很简单,如下:
HBase的连接代码很简单,如下:
try (Connection connection = ConnectionFactory.createConnection(conf))
这里用到了工厂模式进行Connection实例的创建,需要传入的是配置参数管理类Configuration,在创建中首先需要把用户信息添加进去:
if (user == null) {
UserProvider provider = UserProvider.instantiate(conf);
user = provider.getCurrent();
}
return createConnection(conf, false, pool, user);
String className = conf.get(HConnection.HBASE_CLIENT_CONNECTION_IMPL,
ConnectionManager.HConnectionImplementation.class.getName());
Class<?> clazz = null;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
try {
// Default HCM#HCI is not accessible; make it so before invoking.
Constructor<?> constructor =
clazz.getDeclaredConstructor(Configuration.class,
boolean.class, ExecutorService.class, User.class);
constructor.setAccessible(true);
return (Connection) constructor.newInstance(conf, managed, pool, user);