-
Notifications
You must be signed in to change notification settings - Fork 2.8k
ZEPPELIN-374: Adding result API #413
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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
140 changes: 140 additions & 0 deletions
140
...elin-interpreter/src/main/java/org/apache/zeppelin/interpreter/JdbcInterpreterResult.java
This file contains hidden or 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,140 @@ | ||
| package org.apache.zeppelin.interpreter; | ||
|
|
||
| import java.io.PrintWriter; | ||
| import java.io.StringWriter; | ||
| import java.sql.ResultSet; | ||
| import java.sql.ResultSetMetaData; | ||
|
|
||
| import javax.sql.rowset.RowSetProvider; | ||
| import javax.sql.rowset.CachedRowSet; | ||
| import java.sql.SQLException; | ||
|
|
||
|
|
||
| import org.apache.commons.codec.binary.StringUtils; | ||
| import java.util.UUID; | ||
| import com.google.gson.GsonBuilder; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Unlike a regular interpreter result, a JdbcInterpreter Result caches its | ||
| * output so that it can be used later and persisted independently of the regular result. | ||
| * It also has a standard return for all tabular SQL data. | ||
| * @author Rusty Phillips {@literal: <rusty@cloudspace.com>} | ||
| * | ||
| */ | ||
| public class JdbcInterpreterResult extends InterpreterResult { | ||
|
|
||
| private ResultSet results; | ||
| private String id; | ||
| private String repoName; | ||
|
|
||
| private InterpreterResult innerResult; | ||
|
|
||
| private Logger logger() { | ||
|
|
||
| Logger logger = LoggerFactory.getLogger(JdbcInterpreterResult.class); | ||
| return logger; | ||
| } | ||
|
|
||
|
|
||
| public String getRepoName() { | ||
| return repoName; | ||
| } | ||
|
|
||
| public void setRepoName(String repoName) { | ||
| this.repoName = repoName; | ||
| } | ||
|
|
||
| public ResultSet getResults() { | ||
| return results; | ||
| } | ||
|
|
||
|
|
||
| public JdbcInterpreterResult(Code code, String Id) | ||
| { | ||
| super(code); | ||
| this.type = Type.TABLE; | ||
| this.setId(Id); | ||
| } | ||
|
|
||
| public JdbcInterpreterResult(Code code, ResultSet resultSet) { | ||
| this(code, resultSet, | ||
| UUID.randomUUID().toString()); | ||
| } | ||
|
|
||
| public JdbcInterpreterResult(Code code, ResultSet resultSet, String Id) { | ||
| super(code); | ||
| try { | ||
|
|
||
| this.type = Type.TABLE; | ||
| // Not necessary at this time, but may be something to consider in the future. | ||
| // the results in memory, it is not necessarily a better option than the previous one. | ||
| /* | ||
| CachedRowSet impl = RowSetProvider.newFactory().createCachedRowSet(); | ||
| logger().debug("Populating result set"); | ||
| impl.populate(resultSet); | ||
| */ | ||
| logger().debug("Finished populating result set."); | ||
| this.results = resultSet; | ||
| this.id = Id; | ||
| message(); | ||
| } catch (Exception ex) { | ||
| StringWriter sw = new StringWriter(); | ||
| PrintWriter pw = new PrintWriter(sw); | ||
| this.code = code.ERROR; | ||
| ex.printStackTrace(pw); | ||
| logger().debug("Failed to populate result set.\n{}\n{}", | ||
| ex.getMessage(), sw.toString()); | ||
| this.msg = ex.getMessage(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public String message() { | ||
| if (this.msg != null) | ||
| { | ||
| return this.msg; | ||
| } | ||
|
|
||
| StringBuilder msg = new StringBuilder(); | ||
| if (code == code.ERROR) { return this.msg; } | ||
| try { | ||
| if (this.results == null) | ||
| { | ||
| this.code = code.ERROR; | ||
| this.msg = "Unable to find any results table"; | ||
| return this.msg; | ||
| } | ||
| ResultSetMetaData md = this.results.getMetaData(); | ||
| for (int i = 1; i < md.getColumnCount() + 1; i++) { | ||
| if (i == 1) { | ||
| msg.append(md.getColumnName(i)); | ||
| } else { | ||
| msg.append("\t" + md.getColumnName(i)); | ||
| } | ||
| } | ||
| msg.append("\n"); | ||
| while (this.results.next()) { | ||
| for (int i = 1; i < md.getColumnCount() + 1; i++) { | ||
| msg.append(results.getString(i) + "\t"); | ||
| } | ||
| msg.append("\n"); | ||
| } | ||
| this.msg = msg.toString(); | ||
| } catch (SQLException ex) { | ||
| code = code.ERROR; | ||
| this.msg = ex.getMessage(); | ||
| } | ||
| return this.msg; | ||
| } | ||
|
|
||
| public String getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(String id) { | ||
| this.id = id; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
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.
Would you be willing to remove
@authortag from JavaDoc here and below please?Although not clearly documented we, as many other ASF projects, do not use them to keep track of contributions, but use git logs and JIRA issues instead.
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.
Absolutely!