Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions hive/src/main/java/org/apache/zeppelin/hive/HiveInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
Expand All @@ -32,6 +31,7 @@
import org.apache.zeppelin.interpreter.InterpreterPropertyBuilder;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.interpreter.InterpreterResult.Code;
import org.apache.zeppelin.interpreter.JdbcInterpreterResult;
import org.apache.zeppelin.scheduler.Scheduler;
import org.apache.zeppelin.scheduler.SchedulerFactory;
import org.slf4j.Logger;
Expand Down Expand Up @@ -117,10 +117,12 @@ public void close() {
Statement currentStatement;
private InterpreterResult executeSql(String sql) {
try {
logger.debug("Running execute.");
if (exceptionOnConnect != null) {
return new InterpreterResult(Code.ERROR, exceptionOnConnect.getMessage());
}
currentStatement = jdbcConnection.createStatement();
logger.debug("Created statement.");
StringBuilder msg = null;
if (StringUtils.containsIgnoreCase(sql, "EXPLAIN ")) {
//return the explain as text, make this visual explain later
Expand All @@ -129,23 +131,12 @@ private InterpreterResult executeSql(String sql) {
else {
msg = new StringBuilder("%table ");
}
logger.debug("Building JDBC result");

ResultSet res = currentStatement.executeQuery(sql);

try {
ResultSetMetaData md = res.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 (res.next()) {
for (int i = 1; i < md.getColumnCount() + 1; i++) {
msg.append(res.getString(i) + "\t");
}
msg.append("\n");
}
return new JdbcInterpreterResult(Code.SUCCESS, res);
}
finally {
try {
Expand All @@ -157,8 +148,6 @@ private InterpreterResult executeSql(String sql) {
}
}

InterpreterResult rett = new InterpreterResult(Code.SUCCESS, msg.toString());
return rett;
}
catch (SQLException ex) {
logger.error("Can not run " + sql, ex);
Expand Down
7 changes: 7 additions & 0 deletions zeppelin-interpreter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,12 @@
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.0</version>
</dependency>

</dependencies>
</project>
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>}
Copy link
Member

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 @author tag 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely!

*
*/
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.apache.zeppelin.interpreter.Interpreter;
import org.apache.zeppelin.interpreter.InterpreterSerializer;
import org.apache.zeppelin.interpreter.InterpreterResult;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.zeppelin.conf.ZeppelinConfiguration;
import org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars;
import org.apache.zeppelin.interpreter.InterpreterFactory;
import org.apache.zeppelin.interpreter.ResultRepoFactory;
import org.apache.zeppelin.notebook.Notebook;
import org.apache.zeppelin.notebook.repo.NotebookRepo;
import org.apache.zeppelin.notebook.repo.NotebookRepoSync;
Expand Down Expand Up @@ -68,6 +69,8 @@ public class ZeppelinServer extends Application {
private static final Logger LOG = LoggerFactory.getLogger(ZeppelinServer.class);

private SchedulerFactory schedulerFactory;
private ResultRepoFactory resultRepoFactory;

public static Notebook notebook;

public static NotebookServer notebookServer;
Expand Down Expand Up @@ -252,8 +255,10 @@ public ZeppelinServer() throws Exception {

this.schedulerFactory = new SchedulerFactory();

this.replFactory = new InterpreterFactory(conf, notebookServer);
this.resultRepoFactory = new ResultRepoFactory(conf);
this.replFactory = new InterpreterFactory(conf, notebookServer, resultRepoFactory);
this.notebookRepo = new NotebookRepoSync(conf);

notebook = new Notebook(conf, notebookRepo, schedulerFactory, replFactory, notebookServer);
}

Expand Down
Loading