/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.kylin.source.hive;

import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.hive.hcatalog.common.HCatUtil;
import org.apache.kylin.common.KylinConfig;

import java.io.IOException;

public class HiveMetaStoreClientFactory {

    /**
     * Get hivemetastoreclient. At present, it supports hivecatalog and glue catalog. When it is configured as hcatalog,
     * you can directly new hivemetastoreclient (hiveconf), which is more efficient.
     * But if you need to use hcatutil.gethivemetastoreclient (hiveconf) to configure gluecatalog,
     * you can get: com.amazon aws.glue.catalog.metastore.awsgluedatacataloghiveclientfactory according to the configuration file
     *
     * @param hiveConf
     * @return metaStoreClient
     * @throws MetaException
     * @throws IOException
     */
    public static IMetaStoreClient getHiveMetaStoreClient(HiveConf hiveConf) throws MetaException, IOException {
        IMetaStoreClient metaStoreClient = null;
        if ("hcatalog".equals(KylinConfig.getInstanceFromEnv().getHiveMetaDataType())) {
            metaStoreClient = new HiveMetaStoreClient(hiveConf);
        } else if ("gluecatalog".equals(KylinConfig.getInstanceFromEnv().getHiveMetaDataType())) {
            metaStoreClient = HCatUtil.getHiveMetastoreClient(hiveConf);
        }
        return metaStoreClient;
    }

}

@@ -1066,57 +1066,8 @@
 on;%0A
-import org.apache.hive.hcatalog.common.HCatUtil;%0A
 impo
@@ -1134,16 +1134,49 @@
 ception;
+%0Aimport java.lang.reflect.Method;
 %0A%0Apublic
@@ -1954,30 +1954,36 @@
     
-if (%22hcatalog%22.equals(
+String hiveMetadataOption = 
 Kyli
@@ -2024,32 +2024,82 @@
 veMetaDataType()
+;%0A        if (%22hcatalog%22.equals(hiveMetadataOption
 )) %7B%0A           
@@ -2196,67 +2196,320 @@
 als(
-KylinConfig.getInstanceFromEnv().getHiveMetaDataType())) %7B%0A
+hiveMetadataOption)) %7B%0A            // getHiveMetastoreClient is not available in CDH profile%0A            try %7B%0A                Class%3C?%3E clazz = Class.forName(%22org.apache.hive.hcatalog.common.HCatUtil%22);%0A                Method getHiveMetastoreClientMethod = clazz.getDeclaredMethod(%22getHiveMetastoreClient%22);%0A    
     
@@ -2538,17 +2538,27 @@
 t = 
-HCatUtil.
+(IMetaStoreClient) 
 getH
@@ -2571,33 +2571,332 @@
 astoreClient
-(hiveConf
+Method.invoke(null, hiveConf);%0A            %7D catch (Exception exp) %7B%0A                throw new IllegalStateException(%22Unable to create MetaStoreClient for %22 + hiveMetadataOption, exp);%0A            %7D%0A        %7D else %7B%0A            throw new IllegalArgumentException(hiveMetadataOption + %22 is not a good option.%22
 );%0A        %7D
@@ -2934,11 +2934,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Fix for KYLIN-4206