toyboxman commented on a change in pull request #506: add hive metadata jdbc 
retrieval
URL: https://github.com/apache/griffin/pull/506#discussion_r305601559
 
 

 ##########
 File path: 
service/src/main/java/org/apache/griffin/core/metastore/hive/HiveMetaStoreServiceJdbcImpl.java
 ##########
 @@ -0,0 +1,344 @@
+/*
+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.griffin.core.metastore.hive;
+
+import java.io.IOException;
+import java.sql.*;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.annotation.PostConstruct;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.cache.annotation.CacheConfig;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+
+@Service
+@Qualifier(value = "jdbcSvc")
+@CacheConfig(cacheNames = "jdbcHive", keyGenerator = "cacheKeyGenerator")
+public class HiveMetaStoreServiceJdbcImpl implements HiveMetaStoreService {
+
+    private static final Logger LOGGER = LoggerFactory
+            .getLogger(HiveMetaStoreService.class);
+
+    private static final String SHOW_TABLES_IN = "show tables in ";
+
+    private static final String SHOW_DATABASE = "show databases";
+
+    private static final String SHOW_CREATE_TABLE = "show create table ";
+
+    @Value("${hive.jdbc.className}")
+    private String hiveClassName;
+
+    @Value("${hive.jdbc.url}")
+    private String hiveUrl;
+
+    @Value("${hive.need.kerberos}")
+    private String needKerberos;
+
+    @Value("${hive.keytab.user}")
+    private String keytabUser;
+
+    @Value("${hive.keytab.path}")
+    private String keytabPath;
+
+    private Connection conn;
+
+    public void setConn(Connection conn) {
+        this.conn = conn;
+    }
+
+    public void setHiveClassName(String hiveClassName) {
+        this.hiveClassName = hiveClassName;
+    }
+
+    public void setNeedKerberos(String needKerberos) {
+        this.needKerberos = needKerberos;
+    }
+
+    public void setKeytabUser(String keytabUser) {
+        this.keytabUser = keytabUser;
+    }
+
+    public void setKeytabPath(String keytabPath) {
+        this.keytabPath = keytabPath;
+    }
+
+    @PostConstruct
+    public void init() {
+        if (needKerberos != null && needKerberos.equalsIgnoreCase("true")) {
+            LOGGER.info("Hive need Kerberos Auth.");
+
+            Configuration conf = new Configuration();
+            conf.set("hadoop.security.authentication", "Kerberos");
+            UserGroupInformation.setConfiguration(conf);
+            try {
+                UserGroupInformation.loginUserFromKeytab(keytabUser, 
keytabPath);
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    @Override
+    @Cacheable(unless = "#result==null")
+    public Iterable<String> getAllDatabases() {
+        return queryHiveString(SHOW_DATABASE);
+    }
+
+    @Override
+    @Cacheable(unless = "#result==null")
+    public Iterable<String> getAllTableNames(String dbName) {
+
+        return queryHiveString(SHOW_TABLES_IN + dbName);
+    }
+
+    @Override
+    @Cacheable(unless = "#result==null")
+    public Map<String, List<String>> getAllTableNames() {
+//        // Not recommend this method because this method will generate one 
query for every database
+//        // If there has a lots of databases in Hive, this method will lead 
to Griffin crash
+//        Map<String, List<String>> res = new HashMap<>();
+//
+//        for (String dbName : getAllDatabases()) {
+//            List<String> list = (List<String>) 
queryHiveString(SHOW_TABLES_IN + dbName);
+//            res.put(dbName, list);
+//        }
+//
+//        return res;
+        Map<String, List<String>> res = new HashMap<>();
+
+        List<String> list = new ArrayList<>(Arrays.asList("merch_data", 
"merch_summary_ext_v3"));
+        res.put("default", list);
+        return res;
+    }
+
+    @Override
+    public List<Table> getAllTable(String db) {
+        return null;
+    }
+
+    @Override
+    public Map<String, List<Table>> getAllTable() {
+        return null;
+    }
+
+    @Override
+    @Cacheable(unless = "#result==null")
+    public Table getTable(String dbName, String tableName) {
+        Table result = new Table();
+        result.setDbName(dbName);
+        result.setTableName(tableName);
+
+        String sql = SHOW_CREATE_TABLE + dbName + "." + tableName;
+        Statement stmt = null;
+        ResultSet rs = null;
+        StringBuilder sb = new StringBuilder();
+
+        try {
+            Class.forName(hiveClassName);
+            if (conn == null) {
+                conn = DriverManager.getConnection(hiveUrl);
+            }
+            LOGGER.info("got connection");
 
 Review comment:
   it's better add hiveUrl connection into log output

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to