add flag to enable display app info in frontend

This commit is contained in:
Jeff Zhang 2016-10-10 09:55:39 +08:00
parent a087a1d74d
commit eb7ec274a9
4 changed files with 46 additions and 24 deletions

View file

@ -136,15 +136,13 @@ public class LivyHelper {
final Map<String, Integer> userSessionMap,
LivyOutputStream out,
String appId,
String webUI) {
String webUI,
boolean displayAppInfo) {
try {
out.setInterpreterOutput(context.out);
context.out.clear();
out.write("%angular ");
String incomplete = "";
boolean inComment = false;
out.write("<pre><code>");
String[] lines = stringLines.split("\n");
String[] linesToRun = new String[lines.length + 1];
for (int i = 0; i < lines.length; i++) {
@ -152,6 +150,7 @@ public class LivyHelper {
}
linesToRun[lines.length] = "print(\"\")";
Code r = null;
StringBuilder outputBuilder = new StringBuilder();
for (int l = 0; l < linesToRun.length; l++) {
String s = linesToRun[l];
// check if next line starts with "." (but not ".." or "./") it is treated as an invocation
@ -200,19 +199,26 @@ public class LivyHelper {
} else if (r == Code.INCOMPLETE) {
incomplete += s + "\n";
} else {
out.write((res.message() + "\n"));
outputBuilder.append(res.message() + "\n");
incomplete = "";
}
}
out.write("</code></pre>");
out.write("<hr/>");
out.write("Spark Application Id:" + appId + "<br/>");
out.write("Spark WebUI: <a href=" + webUI + ">" + webUI + "</a>");
if (r == Code.INCOMPLETE) {
out.setInterpreterOutput(null);
return new InterpreterResult(r, "Incomplete expression");
} else {
if (displayAppInfo) {
out.write("%angular ");
out.write("<pre><code>");
out.write(outputBuilder.toString());
out.write("</code></pre>");
out.write("<hr/>");
out.write("Spark Application Id:" + appId + "<br/>");
out.write("Spark WebUI: <a href=" + webUI + ">" + webUI + "</a>");
} else {
out.write(outputBuilder.toString());
}
out.setInterpreterOutput(null);
return new InterpreterResult(Code.SUCCESS);
}

View file

@ -17,6 +17,8 @@
package org.apache.zeppelin.livy;
import org.apache.zeppelin.interpreter.InterpreterOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.OutputStream;
@ -25,6 +27,8 @@ import java.io.OutputStream;
* InterpreterOutput can be attached / detached.
*/
public class LivyOutputStream extends OutputStream {
private static Logger LOGGER = LoggerFactory.getLogger(LivyOutputStream.class);
InterpreterOutput interpreterOutput;
public LivyOutputStream() {
@ -53,6 +57,7 @@ public class LivyOutputStream extends OutputStream {
}
public void write(String text) throws IOException {
LOGGER.debug("livy output:" + text);
write(text.getBytes("UTF-8"));
}

View file

@ -42,6 +42,7 @@ public class LivySparkInterpreter extends Interpreter {
protected static Map<Integer, String> sessionId2WebUIMap;
private LivyHelper livyHelper;
private boolean displayAppInfo;
public LivySparkInterpreter(Properties property) {
super(property);
@ -50,6 +51,7 @@ public class LivySparkInterpreter extends Interpreter {
sessionId2WebUIMap = new HashMap<>();
livyHelper = new LivyHelper(property);
out = new LivyOutputStream();
this.displayAppInfo = Boolean.parseBoolean(getProperty("zeppelin.livy.displayAppInfo"));
}
protected static Map<String, Integer> getUserSessionMap() {
@ -77,20 +79,24 @@ public class LivySparkInterpreter extends Interpreter {
try {
sessionId = livyHelper.createSession(interpreterContext, "spark");
userSessionMap.put(interpreterContext.getAuthenticationInfo().getUser(), sessionId);
String appId = extractStatementResult(
livyHelper.interpret("sc.applicationId", interpreterContext, userSessionMap)
.message());
livyHelper.interpret(
"val webui=sc.getClass.getMethod(\"ui\").invoke(sc).asInstanceOf[Some[_]].get",
interpreterContext, userSessionMap);
String webUI = extractStatementResult(
livyHelper.interpret(
"webui.getClass.getMethod(\"appUIAddress\").invoke(webui)",
interpreterContext, userSessionMap).message());
sessionId2AppIdMap.put(sessionId, appId);
sessionId2WebUIMap.put(sessionId, webUI);
LOGGER.info("Create livy session with sessionId: {}, appId: {}, webUI: {}",
sessionId, appId, webUI);
if (displayAppInfo) {
String appId = extractStatementResult(
livyHelper.interpret("sc.applicationId", interpreterContext, userSessionMap)
.message());
livyHelper.interpret(
"val webui=sc.getClass.getMethod(\"ui\").invoke(sc).asInstanceOf[Some[_]].get",
interpreterContext, userSessionMap);
String webUI = extractStatementResult(
livyHelper.interpret(
"webui.getClass.getMethod(\"appUIAddress\").invoke(webui)",
interpreterContext, userSessionMap).message());
sessionId2AppIdMap.put(sessionId, appId);
sessionId2WebUIMap.put(sessionId, webUI);
LOGGER.info("Create livy session with sessionId: {}, appId: {}, webUI: {}",
sessionId, appId, webUI);
} else {
LOGGER.info("Create livy session with sessionId: {}", sessionId);
}
} catch (Exception e) {
LOGGER.error("Exception in LivySparkInterpreter while interpret ", e);
return new InterpreterResult(InterpreterResult.Code.ERROR, e.getMessage());
@ -103,7 +109,7 @@ public class LivySparkInterpreter extends Interpreter {
}
return livyHelper.interpretInput(line, interpreterContext, userSessionMap, out,
sessionId2AppIdMap.get(sessionId), sessionId2WebUIMap.get(sessionId));
sessionId2AppIdMap.get(sessionId), sessionId2WebUIMap.get(sessionId), displayAppInfo);
} catch (Exception e) {
LOGGER.error("Exception in LivySparkInterpreter while interpret ", e);
return new InterpreterResult(InterpreterResult.Code.ERROR,

View file

@ -86,6 +86,11 @@
"propertyName": "livy.spark.jars.packages",
"defaultValue": "",
"description": "Adding extra libraries to livy interpreter"
},
"livy.spark.displayAppInfo": {
"propertyName": "livy.spark.displayAppInfo",
"defaultValue": "false",
"description": "Whether display app info"
}
},
"editor": {