Search in sources :

Example 1 with Bootstrap

use of org.springframework.shell.Bootstrap 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 Bootstrap

use of org.springframework.shell.Bootstrap 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 Bootstrap

use of org.springframework.shell.Bootstrap 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)

Example 4 with Bootstrap

use of org.springframework.shell.Bootstrap in project incubator-rya by apache.

the class AccumuloRyaConnectionCommandsIT method connectToInstance.

@Test
public void connectToInstance() throws IOException {
    final MiniAccumuloCluster cluster = getCluster();
    final Bootstrap bootstrap = getTestBootstrap();
    final JLineShellComponent shell = getTestShell();
    // Mock the user entering the correct password.
    final ApplicationContext context = bootstrap.getApplicationContext();
    final PasswordPrompt mockPrompt = context.getBean(PasswordPrompt.class);
    when(mockPrompt.getPassword()).thenReturn("password".toCharArray());
    // Connect to the mini accumulo instance.
    String cmd = RyaConnectionCommands.CONNECT_ACCUMULO_CMD + " " + "--username root " + "--instanceName " + cluster.getInstanceName() + " " + "--zookeepers " + cluster.getZooKeepers();
    CommandResult result = shell.executeCommand(cmd);
    // Install an instance of rya.
    final String instanceName = "testInstance";
    final InstallConfiguration installConf = InstallConfiguration.builder().build();
    final InstallPrompt installPrompt = context.getBean(InstallPrompt.class);
    when(installPrompt.promptInstanceName()).thenReturn("testInstance");
    when(installPrompt.promptInstallConfiguration("testInstance")).thenReturn(installConf);
    when(installPrompt.promptVerified(instanceName, installConf)).thenReturn(true);
    result = shell.executeCommand(RyaAdminCommands.INSTALL_CMD);
    assertTrue(result.isSuccess());
    // Connect to the instance that was just installed.
    cmd = RyaConnectionCommands.CONNECT_INSTANCE_CMD + " --instance " + instanceName;
    result = shell.executeCommand(cmd);
    assertTrue(result.isSuccess());
    // Verify the shell state indicates it is connected to an instance.
    final SharedShellState sharedState = context.getBean(SharedShellState.class);
    final ShellState state = sharedState.getShellState();
    assertEquals(ConnectionState.CONNECTED_TO_INSTANCE, state.getConnectionState());
}
Also used : JLineShellComponent(org.springframework.shell.core.JLineShellComponent) ApplicationContext(org.springframework.context.ApplicationContext) ShellState(org.apache.rya.shell.SharedShellState.ShellState) MiniAccumuloCluster(org.apache.accumulo.minicluster.MiniAccumuloCluster) Bootstrap(org.springframework.shell.Bootstrap) InstallConfiguration(org.apache.rya.api.client.Install.InstallConfiguration) PasswordPrompt(org.apache.rya.shell.util.PasswordPrompt) InstallPrompt(org.apache.rya.shell.util.InstallPrompt) CommandResult(org.springframework.shell.core.CommandResult) Test(org.junit.Test)

Example 5 with Bootstrap

use of org.springframework.shell.Bootstrap in project incubator-rya by apache.

the class AccumuloRyaConnectionCommandsIT method disconnect.

@Test
public void disconnect() throws IOException {
    final MiniAccumuloCluster cluster = getCluster();
    final Bootstrap bootstrap = getTestBootstrap();
    final JLineShellComponent shell = getTestShell();
    // Mock the user entering the correct password.
    final ApplicationContext context = bootstrap.getApplicationContext();
    final PasswordPrompt mockPrompt = context.getBean(PasswordPrompt.class);
    when(mockPrompt.getPassword()).thenReturn("password".toCharArray());
    // Connect to the mini accumulo instance.
    final String cmd = RyaConnectionCommands.CONNECT_ACCUMULO_CMD + " " + "--username root " + "--instanceName " + cluster.getInstanceName() + " " + "--zookeepers " + cluster.getZooKeepers();
    shell.executeCommand(cmd);
    // Disconnect from it.
    final CommandResult disconnectResult = shell.executeCommand(RyaConnectionCommands.DISCONNECT_COMMAND_NAME_CMD);
    assertTrue(disconnectResult.isSuccess());
}
Also used : JLineShellComponent(org.springframework.shell.core.JLineShellComponent) ApplicationContext(org.springframework.context.ApplicationContext) MiniAccumuloCluster(org.apache.accumulo.minicluster.MiniAccumuloCluster) Bootstrap(org.springframework.shell.Bootstrap) PasswordPrompt(org.apache.rya.shell.util.PasswordPrompt) CommandResult(org.springframework.shell.core.CommandResult) Test(org.junit.Test)

Aggregations

Bootstrap (org.springframework.shell.Bootstrap)14 CommandResult (org.springframework.shell.core.CommandResult)11 Test (org.junit.Test)8 ApplicationContext (org.springframework.context.ApplicationContext)8 JLineShellComponent (org.springframework.shell.core.JLineShellComponent)8 PasswordPrompt (org.apache.rya.shell.util.PasswordPrompt)7 MiniAccumuloCluster (org.apache.accumulo.minicluster.MiniAccumuloCluster)6 LensClient (org.apache.lens.client.LensClient)3 JLineShell (org.springframework.shell.core.JLineShell)3 InstallConfiguration (org.apache.rya.api.client.Install.InstallConfiguration)2 ShellState (org.apache.rya.shell.SharedShellState.ShellState)2 InstallPrompt (org.apache.rya.shell.util.InstallPrompt)2 Before (org.junit.Before)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1 BeforeClass (org.junit.BeforeClass)1