diff --git a/spark/src/main/java/org/apache/zeppelin/spark/SparkInterpreter.java b/spark/src/main/java/org/apache/zeppelin/spark/SparkInterpreter.java index 3c1288e1af..11720a9ecd 100644 --- a/spark/src/main/java/org/apache/zeppelin/spark/SparkInterpreter.java +++ b/spark/src/main/java/org/apache/zeppelin/spark/SparkInterpreter.java @@ -993,6 +993,7 @@ public class SparkInterpreter extends Interpreter { } private Results.Result interpret(String line) { + out.ignoreLeadingNewLinesFromScalaReporter(); return (Results.Result) Utils.invokeMethod( intp, "interpret", @@ -1261,7 +1262,6 @@ public class SparkInterpreter extends Interpreter { if (varName == null || varName.isEmpty()) { return; } - Object lastObj = null; try { if (Utils.isScala2_10()) { diff --git a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/util/InterpreterOutputStream.java b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/util/InterpreterOutputStream.java index b6f01b1a7c..b0bcc5a4e9 100644 --- a/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/util/InterpreterOutputStream.java +++ b/zeppelin-interpreter/src/main/java/org/apache/zeppelin/interpreter/util/InterpreterOutputStream.java @@ -30,6 +30,7 @@ import java.io.IOException; public class InterpreterOutputStream extends LogOutputStream { public static Logger logger; InterpreterOutput interpreterOutput; + boolean ignoreLeadingNewLinesFromScalaReporter = false; public InterpreterOutputStream(Logger logger) { this.logger = logger; @@ -45,6 +46,18 @@ public class InterpreterOutputStream extends LogOutputStream { @Override public void write(int b) throws IOException { + if (ignoreLeadingNewLinesFromScalaReporter && b == '\n') { + StackTraceElement[] stacks = Thread.currentThread().getStackTrace(); + for (StackTraceElement stack : stacks) { + if (stack.getClassName().equals("scala.tools.nsc.interpreter.ReplReporter") && + stack.getMethodName().equals("error")) { + // ignore + return; + } + } + } else { + ignoreLeadingNewLinesFromScalaReporter = false; + } super.write(b); if (interpreterOutput != null) { interpreterOutput.write(b); @@ -53,17 +66,13 @@ public class InterpreterOutputStream extends LogOutputStream { @Override public void write(byte [] b) throws IOException { - super.write(b); - if (interpreterOutput != null) { - interpreterOutput.write(b); - } + write(b, 0, b.length); } @Override - public void write(byte [] b, int offset, int len) throws IOException { - super.write(b, offset, len); - if (interpreterOutput != null) { - interpreterOutput.write(b, offset, len); + public void write(byte [] b, int off, int len) throws IOException { + for (int i = off; i < len; i++) { + write(b[i]); } } @@ -80,7 +89,6 @@ public class InterpreterOutputStream extends LogOutputStream { } } - @Override public void flush() throws IOException { super.flush(); @@ -88,4 +96,8 @@ public class InterpreterOutputStream extends LogOutputStream { interpreterOutput.flush(); } } + + public void ignoreLeadingNewLinesFromScalaReporter() { + ignoreLeadingNewLinesFromScalaReporter = true; + } }