Search in sources :

Example 1 with JLineShell

use of org.springframework.shell.core.JLineShell 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 2 with JLineShell

use of org.springframework.shell.core.JLineShell in project zeppelin by apache.

the class LensInterpreter method cancel.

@Override
public void cancel(InterpreterContext context) {
    if (!s_paraToQH.containsKey(context.getParagraphId())) {
        s_logger.error("ignoring cancel from " + context.getParagraphId());
        return;
    }
    String qh = s_paraToQH.get(context.getParagraphId()).getQueryHandle();
    s_logger.info("preparing to cancel : (" + context.getParagraphId() + ") :" + qh);
    Bootstrap bs = createBootstrap();
    JLineShell shell = getJLineShell(bs);
    LensClient lensClient = null;
    try {
        lensClient = createAndSetLensClient(bs);
        s_clientMap.put(lensClient, true);
        s_logger.info("invoke query kill (" + context.getParagraphId() + ") " + qh);
        CommandResult res = shell.executeCommand("query kill " + qh);
        s_logger.info("query kill returned (" + context.getParagraphId() + ") " + qh + " with: " + res.getResult());
    } catch (Exception e) {
        s_logger.error("unable to kill query (" + context.getParagraphId() + ") " + qh, e);
    } finally {
        try {
            if (lensClient != null) {
                closeLensClient(lensClient);
                s_clientMap.remove(lensClient);
            }
            closeLensClient(s_paraToQH.get(context.getParagraphId()).getLensClient());
            closeShell(s_paraToQH.get(context.getParagraphId()).getShell());
        } catch (Exception e) {
            // ignore
            s_logger.info("Exception in LensInterpreter while cancel finally, ignore", e);
        }
        s_paraToQH.remove(context.getParagraphId());
        closeShell(shell);
    }
}
Also used : JLineShell(org.springframework.shell.core.JLineShell) Bootstrap(org.springframework.shell.Bootstrap) LensClient(org.apache.lens.client.LensClient) CommandResult(org.springframework.shell.core.CommandResult)

Example 3 with JLineShell

use of org.springframework.shell.core.JLineShell in project zeppelin by apache.

the class LensInterpreter method getProgress.

@Override
public int getProgress(InterpreterContext context) {
    if (s_paraToQH.containsKey(context.getParagraphId())) {
        s_logger.info("number of items for which progress can be reported :" + s_paraToQH.size());
        s_logger.info("number of open lensclient :" + s_clientMap.size());
        Bootstrap bs = createBootstrap();
        JLineShell shell = getJLineShell(bs);
        LensClient lensClient = null;
        String qh = s_paraToQH.get(context.getParagraphId()).getQueryHandle();
        try {
            s_logger.info("fetch query status for : (" + context.getParagraphId() + ") :" + qh);
            lensClient = createAndSetLensClient(bs);
            s_clientMap.put(lensClient, true);
            CommandResult res = shell.executeCommand("query status " + qh);
            s_logger.info(context.getParagraphId() + " --> " + res.getResult().toString());
            //change to debug
            Pattern pattern = Pattern.compile(".*(Progress : (\\d\\.\\d)).*");
            Matcher matcher = pattern.matcher(res.getResult().toString().replaceAll("\\n", " "));
            if (matcher.find(2)) {
                Double d = Double.parseDouble(matcher.group(2)) * 100;
                if (d.intValue() == 100) {
                    s_paraToQH.remove(context.getParagraphId());
                }
                return d.intValue();
            } else {
                return 1;
            }
        } catch (Exception e) {
            s_logger.error("unable to get progress for (" + context.getParagraphId() + ") :" + qh, e);
            s_paraToQH.remove(context.getParagraphId());
            return 0;
        } finally {
            if (lensClient != null) {
                closeLensClient(lensClient);
                s_clientMap.remove(lensClient);
            }
            if (shell != null) {
                closeShell(shell);
            }
        }
    }
    return 0;
}
Also used : Pattern(java.util.regex.Pattern) JLineShell(org.springframework.shell.core.JLineShell) Matcher(java.util.regex.Matcher) Bootstrap(org.springframework.shell.Bootstrap) LensClient(org.apache.lens.client.LensClient) CommandResult(org.springframework.shell.core.CommandResult)

Aggregations

LensClient (org.apache.lens.client.LensClient)3 Bootstrap (org.springframework.shell.Bootstrap)3 CommandResult (org.springframework.shell.core.CommandResult)3 JLineShell (org.springframework.shell.core.JLineShell)3 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1