Java tutorial
/* * Copyright (C) 2017 Baifendian Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.baifendian.swordfish.common.hive.metastore; import org.apache.commons.pool.impl.GenericObjectPool; import org.apache.hadoop.hive.metastore.HiveMetaStoreClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HiveMetaPoolClient { private static final Logger logger = LoggerFactory.getLogger(HiveMetaPoolClient.class); private GenericObjectPool pool; /** * */ private int maxActive = 256; /** * */ private int maxIdle = 64; /** * */ private int minIdle = 0; /** * ? */ private int maxWait = 2000; /** * -1 */ private int timeBetweenEvictionRunsMillis = 180000; /** * ??? true */ private boolean testWhileIdle = true; /** * hive */ private static HiveMetaPoolClient hiveMetaPoolClient; private HiveMetaPoolClient(String metastoreUris) { try { pool = bulidClientPool(metastoreUris); } catch (Exception e) { logger.error("build client pool exception", e); } } public static void init(String metastoreUris) { if (hiveMetaPoolClient == null) { synchronized (HiveMetaPoolClient.class) { if (hiveMetaPoolClient == null) { hiveMetaPoolClient = new HiveMetaPoolClient(metastoreUris); } } } } public static HiveMetaPoolClient getInstance() { if (hiveMetaPoolClient == null) { logger.error("Get HiveMetaPoolClient failedplease init first"); throw new RuntimeException("Get HiveMetaPoolClient failedplease init first"); } return hiveMetaPoolClient; } protected GenericObjectPool bulidClientPool(String metastoreUris) { // poolConfig GenericObjectPool.Config poolConfig = new GenericObjectPool.Config(); poolConfig.maxActive = maxActive; poolConfig.maxIdle = maxIdle; poolConfig.minIdle = minIdle; poolConfig.maxWait = maxWait; poolConfig.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; poolConfig.testWhileIdle = testWhileIdle; poolConfig.testOnBorrow = true; poolConfig.testOnReturn = true; HiveMetaStorePoolFactory clientFactory = new HiveMetaStorePoolFactory(metastoreUris); return new GenericObjectPool(clientFactory, poolConfig); } /** * ? hive */ public HiveMetaStoreClient borrowClient() throws Exception { return (HiveMetaStoreClient) pool.borrowObject(); } /** * hive */ public void returnClient(HiveMetaStoreClient client) { if (client != null) { try { pool.returnObject(client); } catch (Exception e) { logger.warn("HiveMetaStoreClient returnClient exception", e); } } } /** * ??? */ public void invalidateObject(HiveMetaStoreClient client) { try { pool.invalidateObject(client); } catch (Exception e) { logger.error("HiveMetaStoreClient invalidateObject error", e); } } /** * */ public void clear() { pool.clear(); } }