Search in sources :

Example 1 with StreamPrinter

use of org.apache.hive.common.util.StreamPrinter in project hive by apache.

the class QTestUtil method executeCmd.

private static int executeCmd(String[] args, String outFile, String errFile) throws Exception {
    System.out.println("Running: " + org.apache.commons.lang.StringUtils.join(args, ' '));
    PrintStream out = outFile == null ? SessionState.getConsole().getChildOutStream() : new PrintStream(new FileOutputStream(outFile), true);
    PrintStream err = errFile == null ? SessionState.getConsole().getChildErrStream() : new PrintStream(new FileOutputStream(errFile), true);
    Process executor = Runtime.getRuntime().exec(args);
    StreamPrinter errPrinter = new StreamPrinter(executor.getErrorStream(), null, err);
    StreamPrinter outPrinter = new StreamPrinter(executor.getInputStream(), null, out);
    outPrinter.start();
    errPrinter.start();
    int result = executor.waitFor();
    outPrinter.join();
    errPrinter.join();
    if (outFile != null) {
        out.close();
    }
    if (errFile != null) {
        err.close();
    }
    return result;
}
Also used : SortAndDigestPrintStream(org.apache.hadoop.hive.common.io.SortAndDigestPrintStream) CachingPrintStream(org.apache.hadoop.hive.common.io.CachingPrintStream) DigestPrintStream(org.apache.hadoop.hive.common.io.DigestPrintStream) SortPrintStream(org.apache.hadoop.hive.common.io.SortPrintStream) PrintStream(java.io.PrintStream) StreamPrinter(org.apache.hive.common.util.StreamPrinter) FileOutputStream(java.io.FileOutputStream)

Example 2 with StreamPrinter

use of org.apache.hive.common.util.StreamPrinter in project hive by apache.

the class QFile method executeDiff.

private boolean executeDiff() throws IOException, InterruptedException {
    List<String> diffCommandArgs = new ArrayList<String>();
    diffCommandArgs.add("diff");
    // Text file comparison
    diffCommandArgs.add("-a");
    if (Shell.WINDOWS) {
        // Ignore changes in the amount of white space
        diffCommandArgs.add("-b");
        // Files created on Windows machines have different line endings
        // than files created on Unix/Linux. Windows uses carriage return and line feed
        // ("\r\n") as a line ending, whereas Unix uses just line feed ("\n").
        // Also StringBuilder.toString(), Stream to String conversions adds extra
        // spaces at the end of the line.
        // Strip trailing carriage return on input
        diffCommandArgs.add("--strip-trailing-cr");
        // Ignore changes whose lines are all blank
        diffCommandArgs.add("-B");
    }
    // Add files to compare to the arguments list
    diffCommandArgs.add(getQuotedString(expcetedOutputFile));
    diffCommandArgs.add(getQuotedString(outputFile));
    System.out.println("Running: " + org.apache.commons.lang.StringUtils.join(diffCommandArgs, ' '));
    Process executor = Runtime.getRuntime().exec(diffCommandArgs.toArray(new String[diffCommandArgs.size()]));
    StreamPrinter errPrinter = new StreamPrinter(executor.getErrorStream(), null, System.err);
    StreamPrinter outPrinter = new StreamPrinter(executor.getInputStream(), null, System.out);
    outPrinter.start();
    errPrinter.start();
    int result = executor.waitFor();
    outPrinter.join();
    errPrinter.join();
    executor.waitFor();
    return (result == 0);
}
Also used : StreamPrinter(org.apache.hive.common.util.StreamPrinter) ArrayList(java.util.ArrayList)

Example 3 with StreamPrinter

use of org.apache.hive.common.util.StreamPrinter in project hive by apache.

the class ShellCmdExecutor method execute.

public int execute() throws Exception {
    try {
        Process executor = Runtime.getRuntime().exec(cmd);
        StreamPrinter outPrinter = new StreamPrinter(executor.getInputStream(), null, out);
        StreamPrinter errPrinter = new StreamPrinter(executor.getErrorStream(), null, err);
        outPrinter.start();
        errPrinter.start();
        int ret = executor.waitFor();
        outPrinter.join();
        errPrinter.join();
        return ret;
    } catch (IOException ex) {
        throw new Exception("Failed to execute " + cmd, ex);
    }
}
Also used : StreamPrinter(org.apache.hive.common.util.StreamPrinter) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with StreamPrinter

use of org.apache.hive.common.util.StreamPrinter in project phoenix by apache.

the class HiveTestUtil method executeCmd.

private static int executeCmd(String[] args, String outFile, String errFile) throws Exception {
    LOG.info("Running: " + org.apache.commons.lang.StringUtils.join(args, ' '));
    PrintStream out = outFile == null ? SessionState.getConsole().getChildOutStream() : new PrintStream(new FileOutputStream(outFile), true);
    PrintStream err = errFile == null ? SessionState.getConsole().getChildErrStream() : new PrintStream(new FileOutputStream(errFile), true);
    Process executor = Runtime.getRuntime().exec(args);
    StreamPrinter errPrinter = new StreamPrinter(executor.getErrorStream(), null, err);
    StreamPrinter outPrinter = new StreamPrinter(executor.getInputStream(), null, out);
    outPrinter.start();
    errPrinter.start();
    int result = executor.waitFor();
    outPrinter.join();
    errPrinter.join();
    if (outFile != null) {
        out.close();
    }
    if (errFile != null) {
        err.close();
    }
    return result;
}
Also used : CachingPrintStream(org.apache.hadoop.hive.common.io.CachingPrintStream) DigestPrintStream(org.apache.hadoop.hive.common.io.DigestPrintStream) SortPrintStream(org.apache.hadoop.hive.common.io.SortPrintStream) SortAndDigestPrintStream(org.apache.hadoop.hive.common.io.SortAndDigestPrintStream) PrintStream(java.io.PrintStream) StreamPrinter(org.apache.hive.common.util.StreamPrinter) FileOutputStream(java.io.FileOutputStream)

Example 5 with StreamPrinter

use of org.apache.hive.common.util.StreamPrinter in project hive by apache.

the class MapRedTask method execute.

@Override
public int execute(DriverContext driverContext) {
    Context ctx = driverContext.getCtx();
    boolean ctxCreated = false;
    try {
        if (ctx == null) {
            ctx = new Context(conf);
            ctxCreated = true;
        }
        // estimate number of reducers
        setNumberOfReducers();
        // auto-determine local mode if allowed
        if (!ctx.isLocalOnlyExecutionMode() && conf.getBoolVar(HiveConf.ConfVars.LOCALMODEAUTO)) {
            if (inputSummary == null) {
                inputSummary = Utilities.getInputSummary(driverContext.getCtx(), work.getMapWork(), null);
            }
            // set the values of totalInputFileSize and totalInputNumFiles, estimating them
            // if percentage block sampling is being used
            double samplePercentage = Utilities.getHighestSamplePercentage(work.getMapWork());
            totalInputFileSize = Utilities.getTotalInputFileSize(inputSummary, work.getMapWork(), samplePercentage);
            totalInputNumFiles = Utilities.getTotalInputNumFiles(inputSummary, work.getMapWork(), samplePercentage);
            // at this point the number of reducers is precisely defined in the plan
            int numReducers = work.getReduceWork() == null ? 0 : work.getReduceWork().getNumReduceTasks();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Task: " + getId() + ", Summary: " + totalInputFileSize + "," + totalInputNumFiles + "," + numReducers);
            }
            String reason = MapRedTask.isEligibleForLocalMode(conf, numReducers, totalInputFileSize, totalInputNumFiles);
            if (reason == null) {
                // clone configuration before modifying it on per-task basis
                cloneConf();
                ShimLoader.getHadoopShims().setJobLauncherRpcAddress(conf, "local");
                console.printInfo("Selecting local mode for task: " + getId());
                this.setLocalMode(true);
            } else {
                console.printInfo("Cannot run job locally: " + reason);
                this.setLocalMode(false);
            }
        }
        runningViaChild = conf.getBoolVar(HiveConf.ConfVars.SUBMITVIACHILD);
        if (!runningViaChild) {
            // in ExecDriver as well to have proper local properties.
            if (this.isLocalMode()) {
                // save the original job tracker
                ctx.setOriginalTracker(ShimLoader.getHadoopShims().getJobLauncherRpcAddress(job));
                // change it to local
                ShimLoader.getHadoopShims().setJobLauncherRpcAddress(job, "local");
            }
            // we are not running this mapred task via child jvm
            // so directly invoke ExecDriver
            int ret = super.execute(driverContext);
            // restore the previous properties for framework name, RM address etc.
            if (this.isLocalMode()) {
                // restore the local job tracker back to original
                ctx.restoreOriginalTracker();
            }
            return ret;
        }
        // we need to edit the configuration to setup cmdline. clone it first
        cloneConf();
        // propagate input format if necessary
        super.setInputAttributes(conf);
        // enable assertion
        String hadoopExec = conf.getVar(HiveConf.ConfVars.HADOOPBIN);
        String hiveJar = conf.getJar();
        String libJars = super.getResource(conf, ResourceType.JAR);
        String libJarsOption = StringUtils.isEmpty(libJars) ? " " : " -libjars " + libJars + " ";
        // Generate the hiveConfArgs after potentially adding the jars
        String hiveConfArgs = generateCmdLine(conf, ctx);
        // write out the plan to a local file
        Path planPath = new Path(ctx.getLocalTmpPath(), "plan.xml");
        MapredWork plan = getWork();
        LOG.info("Generating plan file " + planPath.toString());
        OutputStream out = null;
        try {
            out = FileSystem.getLocal(conf).create(planPath);
            SerializationUtilities.serializePlan(plan, out);
            out.close();
            out = null;
        } finally {
            IOUtils.closeQuietly(out);
        }
        String isSilent = "true".equalsIgnoreCase(System.getProperty("test.silent")) ? "-nolog" : "";
        String jarCmd = hiveJar + " " + ExecDriver.class.getName() + libJarsOption;
        String cmdLine = hadoopExec + " jar " + jarCmd + " -plan " + planPath.toString() + " " + isSilent + " " + hiveConfArgs;
        String workDir = (new File(".")).getCanonicalPath();
        String files = super.getResource(conf, ResourceType.FILE);
        if (!files.isEmpty()) {
            cmdLine = cmdLine + " -files " + files;
            workDir = ctx.getLocalTmpPath().toUri().getPath();
            if (!(new File(workDir)).mkdir()) {
                throw new IOException("Cannot create tmp working dir: " + workDir);
            }
            for (String f : StringUtils.split(files, ',')) {
                Path p = new Path(f);
                String target = p.toUri().getPath();
                String link = workDir + Path.SEPARATOR + p.getName();
                if (FileUtil.symLink(target, link) != 0) {
                    throw new IOException("Cannot link to added file: " + target + " from: " + link);
                }
            }
        }
        LOG.info("Executing: " + cmdLine);
        // Inherit Java system variables
        String hadoopOpts;
        StringBuilder sb = new StringBuilder();
        Properties p = System.getProperties();
        for (String element : HIVE_SYS_PROP) {
            if (p.containsKey(element)) {
                sb.append(" -D" + element + "=" + p.getProperty(element));
            }
        }
        hadoopOpts = sb.toString();
        // Inherit the environment variables
        String[] env;
        Map<String, String> variables = new HashMap<String, String>(System.getenv());
        if (ShimLoader.getHadoopShims().isLocalMode(conf)) {
            // if we are running in local mode - then the amount of memory used
            // by the child jvm can no longer default to the memory used by the
            // parent jvm
            int hadoopMem = conf.getIntVar(HiveConf.ConfVars.HIVEHADOOPMAXMEM);
            if (hadoopMem == 0) {
                // remove env var that would default child jvm to use parent's memory
                // as default. child jvm would use default memory for a hadoop client
                variables.remove(HADOOP_MEM_KEY);
            } else {
                // user specified the memory for local mode hadoop run
                variables.put(HADOOP_MEM_KEY, String.valueOf(hadoopMem));
            }
        } else {
        // nothing to do - we are not running in local mode - only submitting
        // the job via a child process. in this case it's appropriate that the
        // child jvm use the same memory as the parent jvm
        }
        if (variables.containsKey(HADOOP_OPTS_KEY)) {
            variables.put(HADOOP_OPTS_KEY, variables.get(HADOOP_OPTS_KEY) + hadoopOpts);
        } else {
            variables.put(HADOOP_OPTS_KEY, hadoopOpts);
        }
        if (variables.containsKey(HIVE_DEBUG_RECURSIVE)) {
            configureDebugVariablesForChildJVM(variables);
        }
        env = new String[variables.size()];
        int pos = 0;
        for (Map.Entry<String, String> entry : variables.entrySet()) {
            String name = entry.getKey();
            String value = entry.getValue();
            env[pos++] = name + "=" + value;
        }
        // Run ExecDriver in another JVM
        executor = Runtime.getRuntime().exec(cmdLine, env, new File(workDir));
        CachingPrintStream errPrintStream = new CachingPrintStream(SessionState.getConsole().getChildErrStream());
        StreamPrinter outPrinter = new StreamPrinter(executor.getInputStream(), null, SessionState.getConsole().getChildOutStream());
        StreamPrinter errPrinter = new StreamPrinter(executor.getErrorStream(), null, errPrintStream);
        outPrinter.start();
        errPrinter.start();
        int exitVal = jobExecHelper.progressLocal(executor, getId());
        // wait for stream threads to finish
        outPrinter.join();
        errPrinter.join();
        if (exitVal != 0) {
            LOG.error("Execution failed with exit status: " + exitVal);
            if (SessionState.get() != null) {
                SessionState.get().addLocalMapRedErrors(getId(), errPrintStream.getOutput());
            }
        } else {
            LOG.info("Execution completed successfully");
        }
        return exitVal;
    } catch (Exception e) {
        LOG.error("Got exception", e);
        return (1);
    } finally {
        try {
            // sure to clear it out
            if (ctxCreated) {
                ctx.clear();
            }
        } catch (Exception e) {
            LOG.error("Exception: ", e);
        }
    }
}
Also used : Context(org.apache.hadoop.hive.ql.Context) DriverContext(org.apache.hadoop.hive.ql.DriverContext) Path(org.apache.hadoop.fs.Path) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) CachingPrintStream(org.apache.hadoop.hive.common.io.CachingPrintStream) MapredWork(org.apache.hadoop.hive.ql.plan.MapredWork) StreamPrinter(org.apache.hive.common.util.StreamPrinter) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

StreamPrinter (org.apache.hive.common.util.StreamPrinter)6 CachingPrintStream (org.apache.hadoop.hive.common.io.CachingPrintStream)4 IOException (java.io.IOException)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 PrintStream (java.io.PrintStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2 Path (org.apache.hadoop.fs.Path)2 DigestPrintStream (org.apache.hadoop.hive.common.io.DigestPrintStream)2 SortAndDigestPrintStream (org.apache.hadoop.hive.common.io.SortAndDigestPrintStream)2 SortPrintStream (org.apache.hadoop.hive.common.io.SortPrintStream)2 Context (org.apache.hadoop.hive.ql.Context)2 DriverContext (org.apache.hadoop.hive.ql.DriverContext)2 ArrayList (java.util.ArrayList)1 CompilationOpContext (org.apache.hadoop.hive.ql.CompilationOpContext)1 SecureCmdDoAs (org.apache.hadoop.hive.ql.exec.SecureCmdDoAs)1 MapJoinMemoryExhaustionException (org.apache.hadoop.hive.ql.exec.mapjoin.MapJoinMemoryExhaustionException)1