Search in sources :

Example 1 with ProcessExecutor

use of org.zeroturnaround.exec.ProcessExecutor in project mongo-hadoop by mongodb.

the class BaseHadoopTest method mongoImport.

public void mongoImport(final String collection, final File file) {
    try {
        final List<String> command = new ArrayList<String>();
        command.addAll(asList(MONGO_IMPORT, "--drop", "--db", "mongo_hadoop", "--collection", collection, "--file", file.getAbsolutePath()));
        if (isAuthEnabled()) {
            final List<String> list = new ArrayList<String>(asList("-u", "bob", "-p", "pwd123"));
            if (!System.getProperty("mongodb_server", "").equals("22-release")) {
                list.addAll(asList("--authenticationDatabase", "admin"));
            }
            command.addAll(list);
        }
        final StringBuilder output = new StringBuilder();
        final Iterator<String> iterator = command.iterator();
        while (iterator.hasNext()) {
            final String s = iterator.next();
            if (output.length() != 0) {
                output.append("\t");
            } else {
                output.append("\n");
            }
            output.append(s);
            if (iterator.hasNext()) {
                output.append(" \\");
            }
            output.append("\n");
        }
        LOG.info(output.toString());
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        final ByteArrayOutputStream errStream = new ByteArrayOutputStream();
        final ProcessExecutor executor = new ProcessExecutor().command(command).readOutput(true).redirectOutput(outStream).redirectError(errStream);
        final ProcessResult result = executor.execute();
        if (result.getExitValue() != 0) {
            LOG.error(result.getOutput().getString());
            throw new RuntimeException(String.format("mongoimport failed with exit code %d: %s", result.getExitValue(), result.getOutput().getString()));
        }
    } catch (final IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (final InterruptedException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (final TimeoutException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) ProcessResult(org.zeroturnaround.exec.ProcessResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ProcessExecutor(org.zeroturnaround.exec.ProcessExecutor) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with ProcessExecutor

use of org.zeroturnaround.exec.ProcessExecutor in project morphia by mongodb.

the class ZipCodeDataSetTest method installSampleData.

public void installSampleData() throws IOException, TimeoutException, InterruptedException {
    File file = new File("zips.json");
    if (!file.exists()) {
        file = new File(System.getProperty("java.io.tmpdir"), "zips.json");
        if (!file.exists()) {
            download(new URL("http://media.mongodb.org/zips.json"), file);
        }
    }
    DBCollection zips = getDb().getCollection("zips");
    if (zips.count() == 0) {
        new ProcessExecutor().command(MONGO_IMPORT, "--db", getDb().getName(), "--collection", "zipcodes", "--file", file.getAbsolutePath()).redirectError(System.err).execute();
    }
}
Also used : DBCollection(com.mongodb.DBCollection) ProcessExecutor(org.zeroturnaround.exec.ProcessExecutor) File(java.io.File) URL(java.net.URL)

Example 3 with ProcessExecutor

use of org.zeroturnaround.exec.ProcessExecutor in project ninja by ninjaframework.

the class RunClassInSeparateJvmMachine method buildProcessExecutor.

ProcessExecutor buildProcessExecutor() {
    List<String> commandLine = new ArrayList<>();
    String javaHome = System.getProperty("java.home");
    String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
    commandLine.add(javaBin);
    commandLine.addAll(jvmArguments);
    commandLine.add("-cp");
    commandLine.add(classpath);
    commandLine.add(classNameWithMainToRun);
    // not redirecting error stream used for unit tests
    return new ProcessExecutor(commandLine).directory(mavenBaseDir).destroyOnExit().addListener(new ProcessListener() {

        @Override
        public void afterStop(Process process) {
            if (!restarting.get()) {
                log.error("JVM process for {} terminated (next file change will attempt to restart it)", name);
            }
        }
    }).redirectErrorStream(true).redirectOutput(this.output);
}
Also used : ProcessListener(org.zeroturnaround.exec.listener.ProcessListener) ArrayList(java.util.ArrayList) StartedProcess(org.zeroturnaround.exec.StartedProcess) ProcessExecutor(org.zeroturnaround.exec.ProcessExecutor)

Example 4 with ProcessExecutor

use of org.zeroturnaround.exec.ProcessExecutor in project mongo-hadoop by mongodb.

the class HiveTest method loadIntoHDFS.

protected void loadIntoHDFS(final String localPath, final String hdfsPath) {
    try {
        new ProcessExecutor(HADOOP_HOME + "/bin/hadoop", "fs", "-mkdir", "-p", hdfsPath).redirectOutput(System.out).execute();
        new ProcessExecutor(HADOOP_HOME + "/bin/hadoop", "fs", "-put", localPath, hdfsPath).directory(PROJECT_HOME).redirectOutput(System.out).execute();
    } catch (final Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : ProcessExecutor(org.zeroturnaround.exec.ProcessExecutor) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException)

Aggregations

ProcessExecutor (org.zeroturnaround.exec.ProcessExecutor)4 ArrayList (java.util.ArrayList)2 DBCollection (com.mongodb.DBCollection)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 SQLException (java.sql.SQLException)1 TimeoutException (java.util.concurrent.TimeoutException)1 ProcessResult (org.zeroturnaround.exec.ProcessResult)1 StartedProcess (org.zeroturnaround.exec.StartedProcess)1 ProcessListener (org.zeroturnaround.exec.listener.ProcessListener)1