-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[ZEPPELIN-1824] Add MetaData exploration to JDBC Interpreter #1776
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
Changes from all commits
92bd1d4
3df75f5
c9d319c
b561519
c94a7a1
7e5f9a2
a046041
fde7b40
76113c2
aa5d5e1
04dfeda
40378f7
4b6e750
5519e2f
85bd918
8d3ce29
320c73f
01d35a3
25484fa
38de569
891aba8
ac3bbef
382e339
527c383
59989d9
a1800ae
81ca4d4
6cbde87
76443ae
fb1bd89
c2a77b5
be10925
319c93a
897c2a9
257ab5a
6a6c7ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,6 +109,10 @@ public class JDBCInterpreter extends Interpreter { | |
| private final String CONCURRENT_EXECUTION_COUNT = "zeppelin.jdbc.concurrent.max_connection"; | ||
| private final String DBCP_STRING = "jdbc:apache:commons:dbcp:"; | ||
|
|
||
| private static final String[] TABLE_TYPES = { "TABLE", "VIEW", "SYSTEM TABLE", | ||
| "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM" }; | ||
| private static final String METADATA_KEYWORD = "explore"; | ||
|
|
||
| private final HashMap<String, Properties> basePropretiesMap; | ||
| private final HashMap<String, JDBCUserConfigurations> jdbcUserConfigurationsMap; | ||
| private final Map<String, SqlCompleter> propertyKeySqlCompleterMap; | ||
|
|
@@ -512,6 +516,67 @@ private InterpreterResult executeSql(String propertyKey, String sql, | |
| } | ||
| } | ||
|
|
||
| private InterpreterResult getMetaData(String propertyKey, | ||
| String cmd, InterpreterContext interpreterContext) { | ||
| Connection connection; | ||
| String user = interpreterContext.getAuthenticationInfo().getUser(); | ||
| DatabaseMetaData dataBaseMetaData; | ||
| ResultSet resultSet = null; | ||
| String tableName = null; | ||
| String results; | ||
|
|
||
| if (cmd.split(" +").length > 1) { | ||
| tableName = cmd.split(" +")[1]; | ||
| } | ||
|
|
||
| try { | ||
| connection = getConnection(propertyKey, interpreterContext); | ||
|
|
||
| if (connection == null) { | ||
| return new InterpreterResult(Code.ERROR, "Prefix not found."); | ||
| } | ||
|
|
||
| try { | ||
| dataBaseMetaData = connection.getMetaData(); | ||
| if (tableName == null) { | ||
| // if a table name is supplied get table metadata | ||
| resultSet = dataBaseMetaData.getTables(null, null, "%", TABLE_TYPES); | ||
| } else { | ||
| // if not, get database metadata | ||
| resultSet = dataBaseMetaData.getColumns(null, null, tableName, null); | ||
| } | ||
| results = getResults(resultSet, true); | ||
| } finally { | ||
| if (resultSet != null) { | ||
| try { | ||
| resultSet.close(); | ||
| } catch (SQLException e) { /*ignored*/ } | ||
| } | ||
| if (connection != null) { | ||
| try { | ||
| connection.close(); | ||
| } catch (SQLException e) { /*ignored*/ } | ||
| } | ||
| } | ||
| return new InterpreterResult(Code.SUCCESS, results); | ||
|
|
||
| } catch (Exception e) { | ||
| logger.error("Cannot fetch metadata.", e); | ||
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| PrintStream ps = new PrintStream(baos); | ||
| e.printStackTrace(ps); | ||
| String errorMsg = new String(baos.toByteArray(), StandardCharsets.UTF_8); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I remember, |
||
|
|
||
| try { | ||
| closeDBPool(user, propertyKey); | ||
| } catch (SQLException e1) { | ||
| e1.printStackTrace(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to use logger, not to write to System.err
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huge 👍 for using Logger here, as in the rest of the project |
||
| } | ||
|
|
||
| return new InterpreterResult(Code.ERROR, errorMsg); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * For %table response replace Tab and Newline characters from the content. | ||
| */ | ||
|
|
@@ -524,7 +589,7 @@ private String replaceReservedChars(String str) { | |
|
|
||
| @Override | ||
| public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) { | ||
| logger.info("Run SQL command '{}'", cmd); | ||
| logger.info("Run Interpreter command '{}'", cmd); | ||
| String propertyKey = getPropertyKey(cmd); | ||
|
|
||
| if (null != propertyKey && !propertyKey.equals(DEFAULT_KEY)) { | ||
|
|
@@ -533,8 +598,15 @@ public InterpreterResult interpret(String cmd, InterpreterContext contextInterpr | |
|
|
||
| cmd = cmd.trim(); | ||
|
|
||
| logger.info("PropertyKey: {}, SQL command: '{}'", propertyKey, cmd); | ||
| return executeSql(propertyKey, cmd, contextInterpreter); | ||
| if (cmd.split(" ")[0].toLowerCase().equals(METADATA_KEYWORD)) { | ||
| // if the command starts with the METADATA_KEYWORD, call getMetaData | ||
| logger.info("PropertyKey: {}, MetaData command: '{}'", propertyKey, cmd); | ||
| return getMetaData(propertyKey, cmd, contextInterpreter); | ||
| } else { | ||
| // otherwise all executeSql | ||
| logger.info("PropertyKey: {}, SQL command: '{}'", propertyKey, cmd); | ||
| return executeSql(propertyKey, cmd, contextInterpreter); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
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.
connectioncan't be null here because we return at line 533 in case of null