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

 ##########
 File path: 
service/src/main/java/org/apache/griffin/core/metastore/hive/HiveMetaStoreServiceJdbcImpl.java
 ##########
 @@ -0,0 +1,287 @@
+/*
+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 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.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;
+
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Service
+@Qualifier(value = "hive_jdbc")
+@CacheConfig(cacheNames = "jdbchive", keyGenerator = "cacheKeyGenerator")
+public class HiveMetaStoreServiceJdbcImpl implements HiveMetaStoreService {
+
+    private static final Logger LOGGER = LoggerFactory
+            .getLogger(HiveMetaStoreService.class);
+
+    @Value("${hive.jdbc.className}")
+    private String hiveClassName;
+
+    @Value("${hive.jdbc.url}")
+    private String hiveUrl;
+
+    private Connection conn;
+
+    public void setConn(Connection conn) {
+        this.conn = conn;
+    }
+
+    public void setHiveClassName(String hiveClassName) {
+        this.hiveClassName = hiveClassName;
+    }
+
+    @Override
+    @Cacheable(unless = "#result==null")
+    public Iterable<String> getAllDatabases() {
+        String sql = "show databases";
+        return queryHiveString(sql);
+    }
+
+    @Override
+    @Cacheable(unless = "#result==null")
+    public Iterable<String> getAllTableNames(String dbName) {
+        String sql = "show tables in " + dbName;
+        return queryHiveString(sql);
+    }
+
+    @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;
+    }
+
+    @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);
+
+        try {
+            Class.forName(hiveClassName);
+            if (conn == null) {
+                conn = DriverManager.getConnection(hiveUrl);
+            }
+        } catch (ClassNotFoundException | SQLException e) {
+            e.printStackTrace();
+        }
+
+        LOGGER.info("got connection");
+
+        String sql = "show create table " + dbName + "." + tableName;
+        Statement stmt = null;
+        ResultSet rs = null;
+        StringBuilder sb = new StringBuilder();
+
+        try {
+            stmt = conn.createStatement();
+            rs = stmt.executeQuery(sql);
+            while (rs.next()) {
+                String s = rs.getString(1);
+                sb.append(s);
+            }
+            String location = getLocation(sb.toString());
+            List<FieldSchema> cols = getColums(sb.toString());
+            StorageDescriptor sd = new StorageDescriptor();
+            sd.setLocation(location);
+            sd.setCols(cols);
+            result.setSd(sd);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            closeConnection(conn, stmt, rs);
+        }
+        return result;
+    }
+
+    @Scheduled(fixedRateString =
+            "${cache.evict.hive.fixedRate.in.milliseconds}")
+    @CacheEvict(
+            cacheNames = "hivejdbc",
+            allEntries = true,
+            beforeInvocation = true)
+    public void evictHiveCache() {
+        LOGGER.info("Evict hive cache");
+    }
+
+    private void closeConnection(Connection conn, Statement stmt, ResultSet 
rs) {
+        try {
+            if (rs != null) {
+                rs.close();
+            }
+            if (stmt != null) {
+                stmt.close();
+            }
+            if (conn != null) {
+                conn.close();
+            }
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Query Hive for Show tables or show databases, which will return List of 
String
+     * @param sql sql string
+     * @return
+     */
+    private Iterable<String> queryHiveString(String sql) {
+        List<String> res = new ArrayList<>();
+        Statement stmt = null;
+        ResultSet rs = null;
+
+        try {
+            Class.forName(hiveClassName);
+            if (conn == null) {
+                conn = DriverManager.getConnection(hiveUrl);
+            }
+            LOGGER.info("got connection");
+        } catch (ClassNotFoundException | SQLException e) {
+            e.printStackTrace();
+        }
+
+        try {
+            stmt = conn.createStatement();
+            rs = stmt.executeQuery(sql);
+            while (rs.next()) {
+                res.add(rs.getString(1));
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            closeConnection(conn, stmt, rs);
+        }
+        return res;
+    }
+
+    /**
+     * Get the Hive table location from hive table metadata string
+     * @param tableMetadata hive table metadata string
+     * @return Hive table location
+     */
+    public String getLocation(String tableMetadata) {
+        tableMetadata = tableMetadata.toLowerCase();
+        int index = tableMetadata.indexOf("location");
+        if (index == -1) {
+            return "";
+        }
+
+        int start = tableMetadata.indexOf("\'", index);
+        int end = tableMetadata.indexOf("\'", start + 1);
+
+        if (start == -1 || end == -1) {
+            return "";
+        }
+
+        return tableMetadata.substring(start + 1, end);
+    }
+
+    /**
+     * Get the Hive table schema: column name, column type, column comment
+     * @param tableMetadata hive table metadata string
+     * @return List of FieldSchema
+     */
+    public List<FieldSchema> getColums(String tableMetadata) {
 
 Review comment:
   done

----------------------------------------------------------------
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