Search in sources :

Example 46 with InterpreterResult

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

the class MockEntity method testWithDefault.

@Test
public void testWithDefault() {
    KylinInterpreter t = new MockKylinInterpreter(getDefaultProperties());
    InterpreterResult result = t.interpret("select a.date,sum(b.measure) as measure from kylin_fact_table a " + "inner join kylin_lookup_table b on a.date=b.date group by a.date", null);
    assertEquals("default", t.getProject("select a.date,sum(b.measure) as measure " + "from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date group by a.date"));
    assertEquals(InterpreterResult.Type.TABLE, result.message().get(0).getType());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Test(org.junit.Test)

Example 47 with InterpreterResult

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

the class LensInterpreter method interpret.

@Override
public InterpreterResult interpret(String input, InterpreterContext context) {
    if (input == null || input.length() == 0) {
        return new InterpreterResult(Code.ERROR, "no command submitted");
    }
    String st = input.replaceAll("\\n", " ");
    s_logger.info("LensInterpreter command: " + st);
    Bootstrap bs = createBootstrap();
    JLineShell shell = getJLineShell(bs);
    CommandResult res = null;
    LensClient lensClient = null;
    String qh = null;
    if (st.trim().startsWith("help")) {
        return HandleHelp(shell, st);
    }
    try {
        lensClient = createAndSetLensClient(bs);
        s_clientMap.put(lensClient, true);
        String lensCommand = modifyQueryStatement(st);
        s_logger.info("executing command : " + lensCommand);
        res = shell.executeCommand(lensCommand);
        if (!lensCommand.equals(st) && res != null && res.getResult() != null && res.getResult().toString().trim().matches("[a-z0-9-]+")) {
            // setup query progress tracking
            qh = res.getResult().toString();
            s_paraToQH.put(context.getParagraphId(), new ExecutionDetail(qh, lensClient, shell));
            String getResultsCmd = "query results --async false " + qh;
            s_logger.info("executing query results command : " + context.getParagraphId() + " : " + getResultsCmd);
            res = shell.executeCommand(getResultsCmd);
            s_paraToQH.remove(context.getParagraphId());
        }
    } catch (Exception ex) {
        s_logger.error("error in interpret", ex);
        return new InterpreterResult(Code.ERROR, ex.getMessage());
    } finally {
        if (shell != null) {
            closeShell(shell);
        }
        if (lensClient != null) {
            closeLensClient(lensClient);
            s_clientMap.remove(lensClient);
        }
        if (qh != null) {
            s_paraToQH.remove(context.getParagraphId());
        }
    }
    return new InterpreterResult(Code.SUCCESS, formatResult(st, res));
}
Also used : JLineShell(org.springframework.shell.core.JLineShell) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Bootstrap(org.springframework.shell.Bootstrap) LensClient(org.apache.lens.client.LensClient) CommandResult(org.springframework.shell.core.CommandResult)

Example 48 with InterpreterResult

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

the class LensInterpreter method HandleHelp.

private InterpreterResult HandleHelp(JLineShell shell, String st) {
    java.util.logging.StreamHandler sh = null;
    java.util.logging.Logger springLogger = null;
    java.util.logging.Formatter formatter = new java.util.logging.Formatter() {

        public String format(java.util.logging.LogRecord record) {
            return record.getMessage();
        }
    };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        sh = new java.util.logging.StreamHandler(baos, formatter);
        springLogger = HandlerUtils.getLogger(org.springframework.shell.core.SimpleParser.class);
        springLogger.addHandler(sh);
        shell.executeCommand(st);
    } catch (Exception e) {
        s_logger.error(e.getMessage(), e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    } finally {
        sh.flush();
        springLogger.removeHandler(sh);
        sh.close();
    }
    return new InterpreterResult(Code.SUCCESS, baos.toString());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 49 with InterpreterResult

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

the class LensInterpreterTest method test.

@Test
public void test() {
    Properties prop = new Properties();
    prop.setProperty(LENS_SERVER_URL, "http://127.0.0.1:9999/lensapi");
    prop.setProperty(LENS_CLIENT_DBNAME, "default");
    prop.setProperty(LENS_PERSIST_RESULTSET, "false");
    prop.setProperty(LENS_SESSION_CLUSTER_USER, "default");
    prop.setProperty(ZEPPELIN_MAX_ROWS, "1000");
    prop.setProperty(ZEPPELIN_LENS_RUN_CONCURRENT_SESSION, "true");
    prop.setProperty(ZEPPELIN_LENS_CONCURRENT_SESSIONS, "10");
    LensInterpreter t = new MockLensInterpreter(prop);
    t.open();
    //simple help test
    InterpreterResult result = t.interpret("help", null);
    assertEquals(result.message().get(0).getType(), InterpreterResult.Type.TEXT);
    //assertEquals("unable to find 'query execute' in help message", 
    //  result.message().contains("query execute"), result.message());
    t.close();
}
Also used : LensInterpreter(org.apache.zeppelin.lens.LensInterpreter) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Properties(java.util.Properties) Test(org.junit.Test)

Example 50 with InterpreterResult

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

the class FlinkInterpreter method interpret.

public InterpreterResult interpret(String[] lines, InterpreterContext context) {
    final IMain imain = flinkIloop.intp();
    String[] linesToRun = new String[lines.length + 1];
    for (int i = 0; i < lines.length; i++) {
        linesToRun[i] = lines[i];
    }
    linesToRun[lines.length] = "print(\"\")";
    System.setOut(new PrintStream(out));
    out.reset();
    Code r = null;
    String incomplete = "";
    boolean inComment = false;
    for (int l = 0; l < linesToRun.length; l++) {
        final 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;
            }
        }
        final String currentCommand = incomplete;
        scala.tools.nsc.interpreter.Results.Result res = null;
        try {
            res = Console.withOut(System.out, new AbstractFunction0<Results.Result>() {

                @Override
                public Results.Result apply() {
                    return imain.interpret(currentCommand + s);
                }
            });
        } catch (Exception e) {
            logger.info("Interpreter exception", e);
            return new InterpreterResult(Code.ERROR, InterpreterUtils.getMostRelevantMessage(e));
        }
        r = getResultCode(res);
        if (r == Code.ERROR) {
            return new InterpreterResult(r, out.toString());
        } else if (r == Code.INCOMPLETE) {
            incomplete += s + "\n";
        } else {
            incomplete = "";
        }
    }
    if (r == Code.INCOMPLETE) {
        return new InterpreterResult(r, "Incomplete expression");
    } else {
        return new InterpreterResult(r, out.toString());
    }
}
Also used : PrintStream(java.io.PrintStream) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) IMain(scala.tools.nsc.interpreter.IMain) AbstractFunction0(scala.runtime.AbstractFunction0) Code(org.apache.zeppelin.interpreter.InterpreterResult.Code) InvocationTargetException(java.lang.reflect.InvocationTargetException) Results(scala.tools.nsc.interpreter.Results)

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