@Test public void testGetTable() throws SQLException { String meta = "CREATE EXTERNAL TABLE `default.session_data`( `session_date` string COMMENT 'this is session date', `site_id` int COMMENT '', `guid` string COMMENT '', `user_id` string COMMENT '')COMMENT 'session_data for session team' PARTITIONED BY ( `dt` string, `place` int) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe' STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat' LOCATION 'hdfs: when(conn.createStatement()).thenReturn(stmt); when(stmt.executeQuery(anyString())).thenReturn(rs); when(rs.next()).thenReturn(true).thenReturn(false); when(rs.getString(anyInt())).thenReturn(meta); Table res = serviceJdbc.getTable("default", "session_data"); assert (res.getDbName().equals("default")); assert (res.getTableName().equals("session_data")); assert (res.getSd().getLocation().equals("hdfs: List<FieldSchema> fieldSchemas = res.getSd().getCols(); for (FieldSchema fieldSchema : fieldSchemas) { Assert.assertEquals(fieldSchema.getName(),"session_date"); Assert.assertEquals(fieldSchema.getType(),"string"); Assert.assertEquals(fieldSchema.getComment(),"this is session date"); break; } }
@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"); 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) { LOGGER.error("Query Hive Table metadata has error. {}", e.getMessage()); } finally { closeConnection(stmt, rs); } return result; }
HiveMetaStoreServiceJdbcImpl implements HiveMetaStoreService { @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"); 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) { LOGGER.error("Query Hive Table metadata has error. {}", e.getMessage()); } finally { closeConnection(stmt, rs); } return result; } }
HiveMetaStoreServiceJdbcImpl implements HiveMetaStoreService { @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"); 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) { LOGGER.error("Query Hive Table metadata has error. {}", e.getMessage()); } finally { closeConnection(stmt, rs); } return result; }  }
HiveMetaStoreServiceJdbcImpl implements HiveMetaStoreService { @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"); 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) { LOGGER.error("Query Hive Table metadata has error. {}", e.getMessage()); } finally { closeConnection(stmt, rs); } return result; }  void setConn(Connection conn); void setHiveClassName(String hiveClassName); void setNeedKerberos(String needKerberos); void setKeytabUser(String keytabUser); void setKeytabPath(String keytabPath); @PostConstruct void init(); @Override @Cacheable(unless = "#result==null") Iterable<String> getAllDatabases(); @Override @Cacheable(unless = "#result==null") Iterable<String> getAllTableNames(String dbName); @Override @Cacheable(unless = "#result==null") Map<String, List<String>> getAllTableNames(); @Override List<Table> getAllTable(String db); @Override Map<String, List<Table>> getAllTable(); @Override @Cacheable(unless = "#result==null") Table getTable(String dbName, String tableName); @Scheduled(fixedRateString = "${cache.evict.hive.fixedRate.in.milliseconds}") @CacheEvict( cacheNames = "jdbcHive", allEntries = true, beforeInvocation = true) void evictHiveCache(); String getLocation(String tableMetadata); List<FieldSchema> getColums(String tableMetadata); String getComment(String colStr); }
HiveMetaStoreServiceJdbcImpl implements HiveMetaStoreService { @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"); 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) { LOGGER.error("Query Hive Table metadata has error. {}", e.getMessage()); } finally { closeConnection(stmt, rs); } return result; }  void setConn(Connection conn); void setHiveClassName(String hiveClassName); void setNeedKerberos(String needKerberos); void setKeytabUser(String keytabUser); void setKeytabPath(String keytabPath); @PostConstruct void init(); @Override @Cacheable(unless = "#result==null") Iterable<String> getAllDatabases(); @Override @Cacheable(unless = "#result==null") Iterable<String> getAllTableNames(String dbName); @Override @Cacheable(unless = "#result==null") Map<String, List<String>> getAllTableNames(); @Override List<Table> getAllTable(String db); @Override Map<String, List<Table>> getAllTable(); @Override @Cacheable(unless = "#result==null") Table getTable(String dbName, String tableName); @Scheduled(fixedRateString = "${cache.evict.hive.fixedRate.in.milliseconds}") @CacheEvict( cacheNames = "jdbcHive", allEntries = true, beforeInvocation = true) void evictHiveCache(); String getLocation(String tableMetadata); List<FieldSchema> getColums(String tableMetadata); String getComment(String colStr);  }