ahutsunshine commented on a change in pull request #506: add hive metadata jdbc retrieval URL: https://github.com/apache/griffin/pull/506#discussion_r305180193
########## 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(); + } + Review comment: 1.Pls use `LOGGER.error ` instead of `e.printStackTrace()` 2.If exception occurs, here just catches it and doesn't do anything. Also, the next code is still to report exceptions. May be class name register and hive connection can be done after class created, like ``` @PostConstruct public void init() { Class.forName(hiveClassName); if (conn == null) { conn = DriverManager.getConnection(hiveUrl); } } ``` You can also add retry times to connect hive. Of course, this is just my advice. ---------------------------------------------------------------- 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
