Search in sources :

Example 16 with InterpreterResult

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

the class ScaldingInterpreterTest method testReferencingUndefinedVal.

@Test
public void testReferencingUndefinedVal() {
    InterpreterResult result = repl.interpret("def category(min: Int) = {" + "    if (0 <= value) \"error\"" + "}", context);
    assertEquals(Code.ERROR, result.code());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Test(org.junit.Test)

Example 17 with InterpreterResult

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

the class ShellInterpreter method interpret.

@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
    LOGGER.debug("Run shell command '" + cmd + "'");
    OutputStream outStream = new ByteArrayOutputStream();
    CommandLine cmdLine = CommandLine.parse(shell);
    // they need to be delimited by '&&' instead
    if (isWindows) {
        String[] lines = StringUtils.split(cmd, "\n");
        cmd = StringUtils.join(lines, " && ");
    }
    cmdLine.addArgument(cmd, false);
    try {
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(contextInterpreter.out, contextInterpreter.out));
        executor.setWatchdog(new ExecuteWatchdog(Long.valueOf(getProperty(TIMEOUT_PROPERTY))));
        executors.put(contextInterpreter.getParagraphId(), executor);
        int exitVal = executor.execute(cmdLine);
        LOGGER.info("Paragraph " + contextInterpreter.getParagraphId() + " return with exit value: " + exitVal);
        return new InterpreterResult(Code.SUCCESS, outStream.toString());
    } catch (ExecuteException e) {
        int exitValue = e.getExitValue();
        LOGGER.error("Can not run " + cmd, e);
        Code code = Code.ERROR;
        String message = outStream.toString();
        if (exitValue == 143) {
            code = Code.INCOMPLETE;
            message += "Paragraph received a SIGTERM\n";
            LOGGER.info("The paragraph " + contextInterpreter.getParagraphId() + " stopped executing: " + message);
        }
        message += "ExitValue: " + exitValue;
        return new InterpreterResult(code, message);
    } catch (IOException e) {
        LOGGER.error("Can not run " + cmd, e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    } finally {
        executors.remove(contextInterpreter.getParagraphId());
    }
}
Also used : DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 18 with InterpreterResult

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

the class PigInterpreter method interpret.

@Override
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
    // remember the origial stdout, because we will redirect stdout to capture
    // the pig dump output.
    PrintStream originalStdOut = System.out;
    ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream();
    File tmpFile = null;
    try {
        pigServer.setJobName(createJobName(cmd, contextInterpreter));
        tmpFile = PigUtils.createTempPigScript(cmd);
        System.setOut(new PrintStream(bytesOutput));
        // each thread should its own ScriptState & PigStats
        ScriptState.start(pigServer.getPigContext().getExecutionEngine().instantiateScriptState());
        // reset PigStats, otherwise you may get the PigStats of last job in the same thread
        // because PigStats is ThreadLocal variable
        PigStats.start(pigServer.getPigContext().getExecutionEngine().instantiatePigStats());
        PigScriptListener scriptListener = new PigScriptListener();
        ScriptState.get().registerListener(scriptListener);
        listenerMap.put(contextInterpreter.getParagraphId(), scriptListener);
        pigServer.registerScript(tmpFile.getAbsolutePath());
    } catch (IOException e) {
        if (e instanceof FrontendException) {
            FrontendException fe = (FrontendException) e;
            if (!fe.getMessage().contains("Backend error :")) {
                // If the error message contains "Backend error :", that means the exception is from
                // backend.
                LOGGER.error("Fail to run pig script.", e);
                return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
            }
        }
        PigStats stats = PigStats.get();
        if (stats != null) {
            String errorMsg = PigUtils.extactJobStats(stats);
            if (errorMsg != null) {
                LOGGER.error("Fail to run pig script, " + errorMsg);
                return new InterpreterResult(Code.ERROR, errorMsg);
            }
        }
        LOGGER.error("Fail to run pig script.", e);
        return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
    } finally {
        System.setOut(originalStdOut);
        listenerMap.remove(contextInterpreter.getParagraphId());
        if (tmpFile != null) {
            tmpFile.delete();
        }
    }
    StringBuilder outputBuilder = new StringBuilder();
    PigStats stats = PigStats.get();
    if (stats != null && includeJobStats) {
        String jobStats = PigUtils.extactJobStats(stats);
        if (jobStats != null) {
            outputBuilder.append(jobStats);
        }
    }
    outputBuilder.append(bytesOutput.toString());
    return new InterpreterResult(Code.SUCCESS, outputBuilder.toString());
}
Also used : PrintStream(java.io.PrintStream) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) FrontendException(org.apache.pig.impl.logicalLayer.FrontendException)

Example 19 with InterpreterResult

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

the class PigInterpreterTezTest method testIncludeJobStats.

@Test
public void testIncludeJobStats() throws IOException {
    setUpTez(true);
    String content = "1\tandy\n" + "2\tpeter\n";
    File tmpFile = File.createTempFile("zeppelin", "test");
    FileWriter writer = new FileWriter(tmpFile);
    IOUtils.write(content, writer);
    writer.close();
    // simple pig script using dump
    String pigscript = "a = load '" + tmpFile.getAbsolutePath() + "';" + "dump a;";
    InterpreterResult result = pigInterpreter.interpret(pigscript, context);
    assertEquals(Type.TEXT, result.message().get(0).getType());
    assertEquals(Code.SUCCESS, result.code());
    assertTrue(result.message().get(0).getData().contains("Vertex Stats"));
    assertTrue(result.message().get(0).getData().contains("(1,andy)\n(2,peter)"));
    // describe
    pigscript = "a = load '" + tmpFile.getAbsolutePath() + "' as (id: int, name: bytearray);" + "describe a;";
    result = pigInterpreter.interpret(pigscript, context);
    assertEquals(Type.TEXT, result.message().get(0).getType());
    assertEquals(Code.SUCCESS, result.code());
    // no job is launched, so no jobStats
    assertTrue(!result.message().get(0).getData().contains("Vertex Stats"));
    assertTrue(result.message().get(0).getData().contains("a: {id: int,name: bytearray}"));
    // syntax error (compilation error)
    pigscript = "a = loa '" + tmpFile.getAbsolutePath() + "';" + "describe a;";
    result = pigInterpreter.interpret(pigscript, context);
    assertEquals(Type.TEXT, result.message().get(0).getType());
    assertEquals(Code.ERROR, result.code());
    // no job is launched, so no jobStats
    assertTrue(!result.message().get(0).getData().contains("Vertex Stats"));
    assertTrue(result.message().get(0).getData().contains("Syntax error, unexpected symbol at or near 'a'"));
    // execution error
    pigscript = "a = load 'invalid_path';" + "dump a;";
    result = pigInterpreter.interpret(pigscript, context);
    assertEquals(Type.TEXT, result.message().get(0).getType());
    assertEquals(Code.ERROR, result.code());
    assertTrue(!result.message().get(0).getData().contains("Vertex Stats"));
    assertTrue(result.message().get(0).getData().contains("Input path does not exist"));
}
Also used : FileWriter(java.io.FileWriter) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) File(java.io.File) Test(org.junit.Test)

Example 20 with InterpreterResult

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

the class SparkInterpreter method interpret.

public InterpreterResult interpret(String[] lines, InterpreterContext context) {
    synchronized (this) {
        z.setGui(context.getGui());
        sc.setJobGroup(Utils.buildJobGroupId(context), "Zeppelin", false);
        InterpreterResult r = interpretInput(lines, context);
        sc.clearJobGroup();
        return r;
    }
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult)

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