Example usage for org.apache.commons.exec CommandLine CommandLine

List of usage examples for org.apache.commons.exec CommandLine CommandLine

Introduction

In this page you can find the example usage for org.apache.commons.exec CommandLine CommandLine.

Prototype

public CommandLine(final CommandLine other) 

Source Link

Document

Copy constructor.

Usage

From source file:nl.tudelft.graphalytics.graphlab.GraphLabPlatform.java

/**
 * Execute the python script belonging to a given AlgorithmType with the given graph location and extra arguments
 * and return the Process created by the Java Runtime.
 * @param job The GraphLab job to execute
 * @return The exit code of the python subprocess
 * @throws IOException When an I/O error occurs
 */// ww  w . ja v  a  2  s .  com
private int executePythonJob(GraphLabJob job) throws IOException {
    LOG.entry(job);

    if (job == null) {
        LOG.warn("GraphLab job set to execute is null, skipping execution.");
        return LOG.exit(-1);
    }

    // Extract the script resource file
    File scriptFile = extractFile(job.getPythonFile());
    if (scriptFile == null) {
        return LOG.exit(-1);
    }

    // Construct the commandline execution pattern starting with the python executable
    CommandLine commandLine = new CommandLine("python2");

    // Add the arguments that are the same for all jobs
    commandLine.addArgument(scriptFile.getAbsolutePath());
    commandLine.addArgument("--target");
    commandLine.addArgument(TARGET);
    if (USE_HADOOP) {
        commandLine.addArgument("--virtual-cores");
        commandLine.addArgument(VIRTUAL_CORES, false);
        commandLine.addArgument("--heap-size");
        commandLine.addArgument(HEAP_SIZE, false);
    }

    // Add the save_graph_result parameter is true (default false, but can be set to true for automated testing)
    if (saveGraphResult) {
        commandLine.addArgument("--save-result");
    }

    // Let the job format it's arguments and add it to the commandline
    commandLine.addArguments(job.formatParametersAsStrings(), false);

    // Set the executor of the command, if desired this can be changed to a custom implementation
    DefaultExecutor executor = new DefaultExecutor();

    // Set the OutputStream to enable printing the output of the algorithm
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(outputStream));

    int result;
    try {
        // Execute the actual command and store the return code
        result = executor.execute(commandLine);
        // Print the command output
        System.out.println(outputStream.toString());
    } catch (ExecuteException e) {
        // Catch the exception thrown when the process exits with result != 0
        System.out.println(outputStream.toString());
        LOG.catching(Level.ERROR, e);
        return LOG.exit(e.getExitValue());
    }
    return LOG.exit(result);
}

From source file:org.apache.bigtop.itest.hive.TestBeeline.java

@Test
public void checkBeeline() {
    results = HiveHelper.execCommand(new CommandLine(beelineBaseCommand));
    String consoleMsg = results.get("outputStream").toLowerCase();
    Assert.assertEquals("beeline -u FAILED. \n" + results.get("outputStream"), true,
            consoleMsg.contains("connecting to " + beelineUrl.toLowerCase()) && !consoleMsg.contains("error")
                    && !consoleMsg.contains("exception"));
}

From source file:org.apache.bigtop.itest.hive.TestBeeline.java

@Test
public void checkBeelineConnect() {
    try (PrintWriter out = new PrintWriter("connect.url")) {
        out.println("!connect " + beelineUrl + " " + beelineUser + " " + beelinePasswd);
        out.println("!quit");
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();//from w  w w.j a  v  a 2 s  . c om
    }
    results = HiveHelper.execCommand(
            new CommandLine("/bin/sh").addArgument("-c").addArgument("beeline -f connect.url", false));
    String consoleMsg = results.get("outputStream").toLowerCase();
    Assert.assertEquals("beeline !connect FAILED. \n" + results.get("outputStream"), true,
            consoleMsg.contains("connecting to " + beelineUrl.toLowerCase()) && !consoleMsg.contains("error")
                    && !consoleMsg.contains("exception"));
}

From source file:org.apache.bigtop.itest.hive.TestBeeline.java

@Test
public void checkBeelineHelp() {
    results = HiveHelper.execCommand(new CommandLine("beeline").addArgument("--help"));
    String consoleMsg = results.get("outputStream").toLowerCase();
    Assert.assertEquals("beeline --help FAILED. \n" + results.get("outputStream"), true,
            consoleMsg.contains("display this message")
                    && consoleMsg.contains("usage: java org.apache.hive.cli.beeline.beeline")
                    && !consoleMsg.contains("exception"));
}

From source file:org.apache.bigtop.itest.hive.TestBeeline.java

@Test
public void checkBeelineQueryExecFromCmdLine() {
    results = HiveHelper// ww  w  . j  a v a2 s.  c o m
            .execCommand(new CommandLine(beelineBaseCommand).addArgument("-e").addArgument("SHOW DATABASES;"));
    if (!results.get("outputStream").contains("bigtop_runtime_hive")) {
        results = HiveHelper.execCommand(new CommandLine(beelineBaseCommand).addArgument("-e")
                .addArgument("CREATE DATABASE bigtop_runtime_hive;"));
        results = HiveHelper.execCommand(
                new CommandLine(beelineBaseCommand).addArgument("-e").addArgument("SHOW DATABASES;"));
    } else {
        results = HiveHelper.execCommand(new CommandLine(beelineBaseCommand).addArgument("-e")
                .addArgument("DROP DATABASE bigtop_runtime_hive;"));
        results = HiveHelper.execCommand(new CommandLine(beelineBaseCommand).addArgument("-e")
                .addArgument("CREATE DATABASE bigtop_runtime_hive;"));
        results = HiveHelper.execCommand(
                new CommandLine(beelineBaseCommand).addArgument("-e").addArgument("SHOW DATABASES;"));
    }
    String consoleMsg = results.get("outputStream").toLowerCase();
    Assert.assertEquals("beeline -e FAILED. \n" + results.get("outputStream"), true,
            consoleMsg.contains("bigtop_runtime_hive") && !consoleMsg.contains("error")
                    && !consoleMsg.contains("exception"));
    HiveHelper.execCommand(new CommandLine(beelineBaseCommand).addArgument("-e")
            .addArgument("DROP DATABASE bigtop_runtime_hive"));
}

From source file:org.apache.bigtop.itest.hive.TestBeeline.java

@Test
public void checkBeelineQueryExecFromFile() throws FileNotFoundException {

    try (PrintWriter out = new PrintWriter("beeline-f1.sql")) {
        out.println("SHOW DATABASES;");
    }/*from w w  w . ja v  a2  s.c o  m*/
    try (PrintWriter out = new PrintWriter("beeline-f2.sql")) {
        out.println("CREATE DATABASE bigtop_runtime_hive;");
    }
    try (PrintWriter out = new PrintWriter("beeline-f3.sql")) {
        out.println("DROP DATABASE bigtop_runtime_hive;");
        out.println("CREATE DATABASE bigtop_runtime_hive;");
    }
    try (PrintWriter out = new PrintWriter("beeline-f4.sql")) {
        out.println("DROP DATABASE bigtop_runtime_hive;");
    }
    results = HiveHelper.execCommand(
            new CommandLine(beelineBaseCommand).addArgument("-f").addArgument("beeline-f1.sql", false));

    if (!results.get("outputStream").contains("bigtop_runtime_hive")) {
        results = HiveHelper.execCommand(
                new CommandLine(beelineBaseCommand).addArgument("-f").addArgument("beeline-f2.sql", false));
    } else {
        results = HiveHelper.execCommand(
                new CommandLine(beelineBaseCommand).addArgument("-f").addArgument("beeline-f3.sql", false));
    }

    results = HiveHelper.execCommand(
            new CommandLine(beelineBaseCommand).addArgument("-f").addArgument("beeline-f1.sql", false));

    String consoleMsg = results.get("outputStream").toLowerCase();
    Assert.assertEquals("beeline -f FAILED. \n" + results.get("outputStream"), true,
            consoleMsg.contains("bigtop_runtime_hive") && !consoleMsg.contains("error")
                    && !consoleMsg.contains("exception"));
    HiveHelper.execCommand(
            new CommandLine(beelineBaseCommand).addArgument("-f").addArgument("beeline-f4.sql", false));
}

From source file:org.apache.bigtop.itest.hive.TestBeeline.java

@Test
public void checkBeelineInitFile() throws FileNotFoundException {

    try (PrintWriter out = new PrintWriter("beeline-i1.sql")) {
        out.println("SHOW DATABASES;");
    }//ww  w.j  a  v a  2  s  .c  om
    try (PrintWriter out = new PrintWriter("beeline-i2.sql")) {
        out.println("CREATE DATABASE bigtop_runtime_beeline_init;");
    }
    try (PrintWriter out = new PrintWriter("beeline-i3.sql")) {
        out.println("DROP DATABASE bigtop_runtime_beeline_init;");
        out.println("CREATE DATABASE bigtop_runtime_beeline_init;");
    }
    try (PrintWriter out = new PrintWriter("beeline-i4.sql")) {
        out.println("DROP DATABASE bigtop_runtime_beeline_init;");
    }
    results = HiveHelper.execCommand(
            new CommandLine(beelineBaseCommand).addArgument("-i").addArgument("beeline-i1.sql", false));

    if (!results.get("outputStream").contains("bigtop_runtime_beeline_init")) {
        results = HiveHelper.execCommand(
                new CommandLine(beelineBaseCommand).addArgument("-i").addArgument("beeline-i2.sql", false));
    } else {
        results = HiveHelper.execCommand(
                new CommandLine(beelineBaseCommand).addArgument("-i").addArgument("beeline-i3.sql", false));
    }

    results = HiveHelper.execCommand(
            new CommandLine(beelineBaseCommand).addArgument("-i").addArgument("beeline-i1.sql", false));
    String consoleMsg = results.get("outputStream").toLowerCase();
    Assert.assertEquals("beeline -i FAILED. \n" + results.get("outputStream"), true,
            consoleMsg.contains("bigtop_runtime_beeline_init") && !consoleMsg.contains("error")
                    && !consoleMsg.contains("exception"));
    HiveHelper.execCommand(
            new CommandLine(beelineBaseCommand).addArgument("-i").addArgument("beeline-i4.sql", false));
}

From source file:org.apache.bigtop.itest.hive.TestBeeline.java

@Test
public void checkBeelineHiveVar() throws FileNotFoundException {

    try (PrintWriter out = new PrintWriter("beeline-hv1.sql")) {
        out.println("SHOW DATABASES;");
    }//from   w  w  w. java2  s. c om
    try (PrintWriter out = new PrintWriter("beeline-hv2.sql")) {
        out.println("CREATE DATABASE ${db};");
    }
    try (PrintWriter out = new PrintWriter("beeline-hv3.sql")) {
        out.println("DROP DATABASE ${db};");
        out.println("CREATE DATABASE ${db};");
    }
    try (PrintWriter out = new PrintWriter("beeline-hv4.sql")) {
        out.println("DROP DATABASE ${db};");
    }
    results = HiveHelper.execCommand(new CommandLine(beelineBaseCommand).addArgument("--hivevar")
            .addArgument("db=bigtop_runtime_beeline_hivevar").addArgument("-i")
            .addArgument("beeline-hv1.sql", false));

    if (!results.get("outputStream").contains("bigtop_runtime_beeline_hivevar")) {
        results = HiveHelper.execCommand(new CommandLine(beelineBaseCommand).addArgument("--hivevar")
                .addArgument("db=bigtop_runtime_beeline_hivevar").addArgument("-i")
                .addArgument("beeline-hv2.sql", false));
    } else {
        results = HiveHelper.execCommand(new CommandLine(beelineBaseCommand).addArgument("--hivevar")
                .addArgument("db=bigtop_runtime_beeline_hivevar").addArgument("-i")
                .addArgument("beeline-hv3.sql", false));
    }

    results = HiveHelper.execCommand(new CommandLine(beelineBaseCommand).addArgument("--hivevar")
            .addArgument("db=bigtop_runtime_beeline_hivevar").addArgument("-i")
            .addArgument("beeline-hv1.sql", false));
    String consoleMsg = results.get("outputStream").toLowerCase();
    Assert.assertEquals("beeline --hivevar FAILED. \n" + results.get("outputStream"), true,
            consoleMsg.contains("bigtop_runtime_beeline_hivevar") && !consoleMsg.contains("error")
                    && !consoleMsg.contains("exception"));
    HiveHelper.execCommand(new CommandLine(beelineBaseCommand).addArgument("--hivevar")
            .addArgument("db=bigtop_runtime_beeline_hivevar").addArgument("-i")
            .addArgument("beeline-hv4.sql", false));
}

From source file:org.apache.bigtop.itest.hive.TestBeeline.java

@Test
public void checkBeelineFastConnect() {
    results = HiveHelper.execCommand(new CommandLine(beelineBaseCommand).addArgument("--fastConnect=false"));
    String consoleMsg = results.get("outputStream").toLowerCase();
    Assert.assertEquals("beeline --fastConnect FAILED. \n" + results.get("outputStream"), true,
            consoleMsg.contains("set fastconnect to true to skip"));
}

From source file:org.apache.bigtop.itest.hive.TestBeeline.java

@Test
public void checkBeelineVerbose() {
    results = HiveHelper.execCommand(new CommandLine(beelineBaseCommand).addArgument("--verbose=true"));
    String consoleMsg = results.get("outputStream").toLowerCase();
    Assert.assertEquals("beeline --verbose FAILED." + results.get("outputStream"), true,
            consoleMsg.contains("issuing: !connect jdbc:hive2:") && !consoleMsg.contains("error")
                    && !consoleMsg.contains("exception"));
}