Hive MetaStore整体代码分析
远程metastore服务端和客户端之间使用Thrift协议通信。IMetaStoreClient接口定义了Metastore的thrift api,该接口中定义了操作元数据的各种方法。Hive中IMetaStoreClient的实现类是HiveMetaStoreClient。
Hive.getMSC() ➔ createMetaStoreClient()
↳ RetryingMetaStoreClient.getProxy()//动态代理类创建代理对象
↳RetryingMetaStoreClient.RetryingMetaStoreClient()
↳MetaStoreUtils.newInstance()//反射实例化对象
↳SessionHiveMetaStoreClient.SessionHiveMetaStoreClient()
↳HiveMetaStoreClient.HiveMetaStoreClient()
↳HiveMetaStoreClient.open()
1 public HiveMetaStoreClient(HiveConf conf, HiveMetaHookLoader hookLoader)
2 throws MetaException {
3
4 this.hookLoader = hookLoader;
5 if (conf == null) {
6 conf = new HiveConf(HiveMetaStoreClient.class);
7 }
8 this.conf = conf;
9 filterHook = loadFilterHooks();
10 //根据hive-site.xml中的hive.metastore.uris配置,如果配置该参数,则认为是远程连接,否则为本地连接
11 String msUri = conf.getVar(HiveConf.ConfVars.METASTOREURIS);
12 localMetaStore = HiveConfUtil.isEmbeddedMetaStore(msUri);
13 if (localMetaStore) {
//本地连接直接连接HiveMetaStore
16 client = HiveMetaStore.newRetryingHMSHandler("hive client", conf, true);
17 isConnected = true;
18 snapshotActiveConf();
19 return;
20 }
21
22 //获取配置中的重试次数及timeout时间
23 retries = HiveConf.getIntVar(conf, HiveConf.ConfVars.METASTORETHRIFTCONNECTIONRETRIES);
24 retryDelaySeconds = conf.getTimeVar(
25 ConfVars.METASTORE_CLIENT_CONNECT_RETRY_DELAY, TimeUnit.SECONDS);
26
27 //拼接metastore uri
28 if (conf.getVar(HiveConf.ConfVars.METASTOREURIS) != null) {
29 String metastoreUrisString[] = conf.getVar(
30 HiveConf.ConfVars.METASTOREURIS).split(",");
31 metastoreUris = new URI[metastoreUrisString.length];
32