Search in sources :

Example 76 with InterpreterResult

use of org.apache.zeppelin.interpreter.InterpreterResult in project zeppelin by apache.

the class NotebookRestApi method runParagraphSynchronously.

/**
   * Run synchronously a paragraph REST API
   *
   * @param noteId - noteId
   * @param paragraphId - paragraphId
   * @param message - JSON with params if user wants to update dynamic form's value
   *                null, empty string, empty json if user doesn't want to update
   *
   * @return JSON with status.OK
   * @throws IOException, IllegalArgumentException
   */
@POST
@Path("run/{noteId}/{paragraphId}")
@ZeppelinApi
public Response runParagraphSynchronously(@PathParam("noteId") String noteId, @PathParam("paragraphId") String paragraphId, String message) throws IOException, IllegalArgumentException {
    LOG.info("run paragraph synchronously {} {} {}", noteId, paragraphId, message);
    Note note = notebook.getNote(noteId);
    checkIfNoteIsNotNull(note);
    checkIfUserCanWrite(noteId, "Insufficient privileges you cannot run paragraph");
    Paragraph paragraph = note.getParagraph(paragraphId);
    checkIfParagraphIsNotNull(paragraph);
    // handle params if presented
    handleParagraphParams(message, note, paragraph);
    if (paragraph.getListener() == null) {
        note.initializeJobListenerForParagraph(paragraph);
    }
    paragraph.run();
    final InterpreterResult result = paragraph.getResult();
    if (result.code() == InterpreterResult.Code.SUCCESS) {
        return new JsonResponse<>(Status.OK, result).build();
    } else {
        return new JsonResponse<>(Status.INTERNAL_SERVER_ERROR, result).build();
    }
}
Also used : Note(org.apache.zeppelin.notebook.Note) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Paragraph(org.apache.zeppelin.notebook.Paragraph) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

Example 77 with InterpreterResult

use of org.apache.zeppelin.interpreter.InterpreterResult in project zeppelin by apache.

the class NotebookRestApiTest method testClearAllParagraphOutput.

@Test
public void testClearAllParagraphOutput() throws IOException {
    // Create note and set result explicitly
    Note note = ZeppelinServer.notebook.createNote(anonymous);
    Paragraph p1 = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    InterpreterResult result = new InterpreterResult(InterpreterResult.Code.SUCCESS, InterpreterResult.Type.TEXT, "result");
    p1.setResult(result);
    Paragraph p2 = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    p2.setReturn(result, new Throwable());
    // clear paragraph result
    PutMethod put = httpPut("/notebook/" + note.getId() + "/clear", "");
    LOG.info("test clear paragraph output response\n" + put.getResponseBodyAsString());
    assertThat(put, isAllowed());
    put.releaseConnection();
    // check if paragraph results are cleared
    GetMethod get = httpGet("/notebook/" + note.getId() + "/paragraph/" + p1.getId());
    assertThat(get, isAllowed());
    Map<String, Object> resp1 = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    Map<String, Object> resp1Body = (Map<String, Object>) resp1.get("body");
    assertNull(resp1Body.get("result"));
    get = httpGet("/notebook/" + note.getId() + "/paragraph/" + p2.getId());
    assertThat(get, isAllowed());
    Map<String, Object> resp2 = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    Map<String, Object> resp2Body = (Map<String, Object>) resp2.get("body");
    assertNull(resp2Body.get("result"));
    get.releaseConnection();
    //cleanup
    ZeppelinServer.notebook.removeNote(note.getId(), anonymous);
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) Note(org.apache.zeppelin.notebook.Note) GetMethod(org.apache.commons.httpclient.methods.GetMethod) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 78 with InterpreterResult

use of org.apache.zeppelin.interpreter.InterpreterResult in project metron by apache.

the class StellarInterpreterTest method testExecuteWithStellarList.

/**
 * Ensure that Stellar lists are displayed correctly in Zeppelin.
 */
@Test
public void testExecuteWithStellarList() {
    final String expected = "[1, 2, 3, 4, 5]";
    InterpreterResult result = interpreter.interpret("[1,2,3,4,5]", context);
    // validate the result
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals(1, result.message().size());
    // validate the message
    InterpreterResultMessage message = result.message().get(0);
    assertEquals(expected, message.getData());
    assertEquals(InterpreterResult.Type.TEXT, message.getType());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.Test)

Example 79 with InterpreterResult

use of org.apache.zeppelin.interpreter.InterpreterResult in project zeppelin by apache.

the class SparkInterpreter method interpretInput.

public InterpreterResult interpretInput(String[] lines, InterpreterContext context) {
    SparkEnv.set(env);
    String[] linesToRun = new String[lines.length];
    for (int i = 0; i < lines.length; i++) {
        linesToRun[i] = lines[i];
    }
    Console.setOut(context.out);
    out.setInterpreterOutput(context.out);
    context.out.clear();
    Code r = null;
    String incomplete = "";
    boolean inComment = false;
    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
        if (l + 1 < linesToRun.length) {
            String nextLine = linesToRun[l + 1].trim();
            boolean continuation = false;
            if (nextLine.isEmpty() || // skip empty line or comment
            nextLine.startsWith("//") || nextLine.startsWith("}") || nextLine.startsWith("object")) {
                // include "} object" for Scala companion object
                continuation = true;
            } else if (!inComment && nextLine.startsWith("/*")) {
                inComment = true;
                continuation = true;
            } else if (inComment && nextLine.lastIndexOf("*/") >= 0) {
                inComment = false;
                continuation = true;
            } else if (nextLine.length() > 1 && nextLine.charAt(0) == '.' && // ".."
            nextLine.charAt(1) != '.' && nextLine.charAt(1) != '/') {
                // "./"
                continuation = true;
            } else if (inComment) {
                continuation = true;
            }
            if (continuation) {
                incomplete += s + "\n";
                continue;
            }
        }
        scala.tools.nsc.interpreter.Results.Result res = null;
        try {
            res = interpret(incomplete + s);
        } catch (Exception e) {
            sc.clearJobGroup();
            out.setInterpreterOutput(null);
            logger.info("Interpreter exception", e);
            return new InterpreterResult(Code.ERROR, InterpreterUtils.getMostRelevantMessage(e));
        }
        r = getResultCode(res);
        if (r == Code.ERROR) {
            sc.clearJobGroup();
            out.setInterpreterOutput(null);
            return new InterpreterResult(r, "");
        } else if (r == Code.INCOMPLETE) {
            incomplete += s + "\n";
        } else {
            incomplete = "";
        }
    }
    // make sure code does not finish with comment
    if (r == Code.INCOMPLETE) {
        scala.tools.nsc.interpreter.Results.Result res = null;
        res = interpret(incomplete + "\nprint(\"\")");
        r = getResultCode(res);
    }
    if (r == Code.INCOMPLETE) {
        sc.clearJobGroup();
        out.setInterpreterOutput(null);
        return new InterpreterResult(r, "Incomplete expression");
    } else {
        sc.clearJobGroup();
        putLatestVarInResourcePool(context);
        out.setInterpreterOutput(null);
        return new InterpreterResult(Code.SUCCESS);
    }
}
Also used : Results(scala.tools.nsc.interpreter.Results) scala(scala) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Example 80 with InterpreterResult

use of org.apache.zeppelin.interpreter.InterpreterResult in project zeppelin by apache.

the class SparkSqlInterpreter method interpret.

@Override
public InterpreterResult interpret(String st, InterpreterContext context) {
    SQLContext sqlc = null;
    SparkInterpreter sparkInterpreter = getSparkInterpreter();
    if (sparkInterpreter.getSparkVersion().isUnsupportedVersion()) {
        return new InterpreterResult(Code.ERROR, "Spark " + sparkInterpreter.getSparkVersion().toString() + " is not supported");
    }
    sparkInterpreter.populateSparkWebUrl(context);
    sqlc = getSparkInterpreter().getSQLContext();
    SparkContext sc = sqlc.sparkContext();
    if (concurrentSQL()) {
        sc.setLocalProperty("spark.scheduler.pool", "fair");
    } else {
        sc.setLocalProperty("spark.scheduler.pool", null);
    }
    sc.setJobGroup(Utils.buildJobGroupId(context), "Zeppelin", false);
    Object rdd = null;
    try {
        // method signature of sqlc.sql() is changed
        // from  def sql(sqlText: String): SchemaRDD (1.2 and prior)
        // to    def sql(sqlText: String): DataFrame (1.3 and later).
        // Therefore need to use reflection to keep binary compatibility for all spark versions.
        Method sqlMethod = sqlc.getClass().getMethod("sql", String.class);
        rdd = sqlMethod.invoke(sqlc, st);
    } catch (InvocationTargetException ite) {
        if (Boolean.parseBoolean(getProperty("zeppelin.spark.sql.stacktrace"))) {
            throw new InterpreterException(ite);
        }
        logger.error("Invocation target exception", ite);
        String msg = ite.getTargetException().getMessage() + "\nset zeppelin.spark.sql.stacktrace = true to see full stacktrace";
        return new InterpreterResult(Code.ERROR, msg);
    } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException e) {
        throw new InterpreterException(e);
    }
    String msg = ZeppelinContext.showDF(sc, context, rdd, maxResult);
    sc.clearJobGroup();
    return new InterpreterResult(Code.SUCCESS, msg);
}
Also used : InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SparkContext(org.apache.spark.SparkContext) SQLContext(org.apache.spark.sql.SQLContext)

Aggregations

InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)159 Test (org.junit.Test)68 Properties (java.util.Properties)17 File (java.io.File)10 IOException (java.io.IOException)9 AlluxioURI (alluxio.AlluxioURI)8 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)7 Theory (org.junit.experimental.theories.Theory)7 FileWriter (java.io.FileWriter)6 PrintStream (java.io.PrintStream)5 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)5 Code (org.apache.zeppelin.interpreter.InterpreterResult.Code)5 InterpreterResultMessage (org.apache.zeppelin.interpreter.InterpreterResultMessage)5 ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)4 FileInStream (alluxio.client.file.FileInStream)3 JsonObject (com.google.gson.JsonObject)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3