-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
scriptis left database menu bar supports hiveserver2 to get library table fields #4909
Open
MrFengqin
wants to merge
6
commits into
apache:master
Choose a base branch
from
MrFengqin:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f310ee
scriptis 左侧数据库菜单栏支持hiveserver2获取库表字段
MrFengqin e99b1c0
scriptis left database menu bar supports hiveserver2 to get library t…
MrFengqin 922de12
scriptis left database menu bar supports hiveserver2 to get library t…
MrFengqin 9452ba5
Change the Chinese comments to English
MrFengqin f1a1c0e
scriptis left database menu bar supports hiveserver2 to get library t…
MrFengqin 42e7b04
Translation of comments to English
MrFengqin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
232 changes: 232 additions & 0 deletions
232
...urce/linkis-metadata/src/main/java/org/apache/linkis/metadata/util/HiveService2Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
/* | ||
* 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.linkis.metadata.util; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
public class HiveService2Utils { | ||
|
||
private static final String driverName = "org.apache.hive.jdbc.HiveDriver"; | ||
|
||
private static final String defaultDb = "default"; | ||
private static final String defaultPassword = "123456"; | ||
|
||
static ObjectMapper jsonMapper = new ObjectMapper(); | ||
static SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); | ||
|
||
/** Determine whether to start hiveServer2 Query the left menu bar */ | ||
public static Boolean checkHiveServer2Enable() { | ||
return DWSConfig.HIVE_SERVER2_ENABLE.getValue(); | ||
} | ||
|
||
/** Get connection */ | ||
private static Connection getConn(String username, String db) throws Exception { | ||
Class.forName(driverName); | ||
String hiveServer2Address = DWSConfig.HIVE_SERVER2_URL.getValue(); | ||
String url = | ||
hiveServer2Address.endsWith("/") ? hiveServer2Address + db : hiveServer2Address + "/" + db; | ||
return DriverManager.getConnection(url, username, defaultPassword); | ||
} | ||
|
||
/** Get database */ | ||
public static JsonNode getDbs(String username) throws Exception { | ||
ArrayNode dbsNode = jsonMapper.createArrayNode(); | ||
List<String> dbs = new ArrayList<>(); | ||
Connection conn = null; | ||
Statement stat = null; | ||
ResultSet rs = null; | ||
try { | ||
conn = getConn(username, defaultDb); | ||
stat = conn.createStatement(); | ||
rs = stat.executeQuery("show databases"); | ||
while (rs.next()) { | ||
dbs.add(rs.getString(1)); | ||
} | ||
} finally { | ||
close(conn, stat, rs); | ||
} | ||
for (String db : dbs) { | ||
ObjectNode dbNode = jsonMapper.createObjectNode(); | ||
dbNode.put("dbName", db); | ||
dbsNode.add(dbNode); | ||
} | ||
return dbsNode; | ||
} | ||
|
||
/** Gets all tables for the specified database */ | ||
public static JsonNode getTables(String username, String dbName) throws Exception { | ||
ArrayNode tablesNode = jsonMapper.createArrayNode(); | ||
Connection conn = null; | ||
Statement stat = null; | ||
ResultSet rs = null; | ||
try { | ||
List<String> tableNames = new ArrayList<>(); | ||
conn = getConn(username, dbName); | ||
stat = conn.createStatement(); | ||
rs = stat.executeQuery("show tables"); | ||
while (rs.next()) { | ||
String tableName = rs.getString(1); | ||
tableNames.add(tableName); | ||
} | ||
|
||
// Get detailed information about each table | ||
for (String tableName : tableNames) { | ||
ObjectNode tableNode = jsonMapper.createObjectNode(); | ||
ResultSet describeRs = stat.executeQuery("DESCRIBE FORMATTED " + tableName); | ||
while (describeRs.next()) { | ||
String columnName = describeRs.getString(1); | ||
String dataType = describeRs.getString(2); | ||
if (null != columnName) { | ||
columnName = columnName.trim(); | ||
} | ||
if (null != dataType) { | ||
dataType = dataType.trim(); | ||
} | ||
if (columnName.contains("Owner:")) { | ||
tableNode.put("createdBy", dataType); | ||
} | ||
if (columnName.contains("CreateTime:")) { | ||
long createdAt = sdf.parse(dataType).getTime() / 1000; | ||
tableNode.put("createdAt", createdAt); | ||
break; | ||
} | ||
} | ||
describeRs.close(); | ||
tableNode.put("databaseName", dbName); | ||
tableNode.put("tableName", tableName); | ||
tableNode.put("lastAccessAt", 0); | ||
tableNode.put("isView", false); | ||
tablesNode.add(tableNode); | ||
} | ||
} finally { | ||
close(conn, stat, rs); | ||
} | ||
|
||
return tablesNode; | ||
} | ||
|
||
/** Gets information about all fields of a specified table */ | ||
public static JsonNode getColumns(String username, String dbName, String tbName) | ||
throws Exception { | ||
ArrayNode columnsNode = jsonMapper.createArrayNode(); | ||
List<Map<String, Object>> columnMapList = new ArrayList<>(); | ||
List<String> partitionColumnList = new ArrayList<>(); | ||
Connection conn = null; | ||
Statement stat = null; | ||
ResultSet rs = null; | ||
try { | ||
conn = getConn(username, dbName); | ||
stat = conn.createStatement(); | ||
rs = stat.executeQuery("desc " + tbName); | ||
while (rs.next()) { | ||
Map<String, Object> colum = new HashMap<>(); | ||
String colName = rs.getString("col_name"); | ||
String dataType = rs.getString("data_type"); | ||
if (StringUtils.isNotBlank(colName) | ||
&& StringUtils.isNotBlank(dataType) | ||
&& !colName.contains("# Partition Information") | ||
&& !colName.contains("# col_name")) { | ||
colum.put("columnName", rs.getString("col_name")); | ||
colum.put("columnType", rs.getString("data_type")); | ||
colum.put("columnComment", rs.getString("comment")); | ||
columnMapList.add(colum); | ||
} | ||
} | ||
|
||
boolean partition = false; | ||
boolean parColName = false; | ||
ResultSet describeRs = stat.executeQuery("DESCRIBE FORMATTED " + tbName); | ||
while (describeRs.next()) { | ||
String columnName = describeRs.getString(1); | ||
String dataType = describeRs.getString(2); | ||
if (null != columnName) { | ||
columnName = columnName.trim(); | ||
} | ||
if (null != dataType) { | ||
dataType = dataType.trim(); | ||
} | ||
|
||
// Partition field judgment | ||
if (columnName.contains("# Partition Information")) { | ||
partition = true; | ||
parColName = false; | ||
continue; | ||
} | ||
if (columnName.contains("# col_name")) { | ||
parColName = true; | ||
continue; | ||
} | ||
|
||
if (partition && parColName) { | ||
if ("".equals(columnName) && null == dataType) { | ||
partition = false; | ||
parColName = false; | ||
} else { | ||
partitionColumnList.add(columnName); | ||
} | ||
} | ||
} | ||
describeRs.close(); | ||
} finally { | ||
close(conn, stat, rs); | ||
} | ||
|
||
for (Map<String, Object> map : columnMapList.stream().distinct().collect(Collectors.toList())) { | ||
ObjectNode fieldNode = jsonMapper.createObjectNode(); | ||
String columnName = map.get("columnName").toString(); | ||
fieldNode.put("columnName", columnName); | ||
fieldNode.put("columnType", map.get("columnType").toString()); | ||
fieldNode.put("columnComment", map.get("columnComment").toString()); | ||
fieldNode.put("partitioned", partitionColumnList.contains(columnName)); | ||
columnsNode.add(fieldNode); | ||
} | ||
|
||
return columnsNode; | ||
} | ||
|
||
/** Close resource */ | ||
private static void close(Connection conn, Statement stat, ResultSet rs) throws SQLException { | ||
if (rs != null) { | ||
rs.close(); | ||
} | ||
if (stat != null) { | ||
stat.close(); | ||
} | ||
if (conn != null) { | ||
conn.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to be set defaultPassword ?