Example usage for org.apache.hadoop.util GenericOptionsParser getCommandLine

List of usage examples for org.apache.hadoop.util GenericOptionsParser getCommandLine

Introduction

In this page you can find the example usage for org.apache.hadoop.util GenericOptionsParser getCommandLine.

Prototype

public CommandLine getCommandLine() 

Source Link

Document

Returns the commons-cli CommandLine object to process the parsed arguments.

Usage

From source file:edu.indiana.d2i.htrc.corpus.analysis.LDAAnalysisDriver.java

License:Apache License

public static void main(String[] args) throws Exception {

    GenericOptionsParser parser = new GenericOptionsParser(new Configuration(), args);

    CommandLine commandLine = parser.getCommandLine();

    Option[] options = commandLine.getOptions();

    /**/*from w ww  . ja v  a  2 s  .  c  o m*/
     * appArgs[0] = <path/to/input/directory> (where sequence files reside)
     * appArgs[1] = <path/to/output/directory/prefix> (where LDA state file
     * should go) appArgs[2] = <path/local/property/file>
     * 
     * Note: the passed in <path/to/output/directory/prefix> is only a
     * prefix, we automatically append the iteration number suffix
     */
    String[] appArgs = parser.getRemainingArgs();

    // load property file
    Properties prop = new Properties();
    prop.load(new FileInputStream(appArgs[2]));

    int maxIterationNum = Integer.parseInt(
            prop.getProperty(Constants.LDA_ANALYSIS_MAX_ITER, Constants.LDA_ANALYSIS_DEFAULT_MAX_ITER));

    int iterationCount = 0;

    /**
     * in the first iteration (iteration 0), there is no LDA state
     */
    String[] arguments = generateArgs(options, new String[0], appArgs[0],
            appArgs[1] + "-iter-" + iterationCount);

    /**
     * iterate until convergence or maximum iteration number reached
     */
    while (true) {

        int exitCode = ToolRunner.run(new LDAAnalysisDriver(), arguments);

        System.out.println(String.format("LDA analysis finished iteration %d, with exitCode = %d",
                iterationCount, exitCode));

        /**
         * LDA state is the output (sequence file) from current iteration
         * and is used to initialize the words-topics table and
         * topics-documents table for the next iteration
         */
        String ldaStateFilePath = appArgs[1] + "-iter-" + iterationCount + File.separator + "part-r-00000";

        /**
         * load LDA state to check whether it is converged
         */
        if (isAnalysisConverged(ldaStateFilePath)) {
            System.out.println(String.format("LDA analysis converged at iteration %d", iterationCount));
            break;
        }

        if ((iterationCount + 1) >= maxIterationNum) {
            System.out.println(String.format(
                    "LDA analysis reached the maximum iteration number %d, going to stop", maxIterationNum));
            break;
        }

        String[] otherOps = { "-D", "user.args.lda.state.filepath=" + ldaStateFilePath };

        /**
         * generate arguments for the next iteration and increase iteration
         * count
         */
        arguments = generateArgs(options, otherOps, appArgs[0], appArgs[1] + "-iter-" + ++iterationCount);
    }

}

From source file:edu.indiana.d2i.htrc.test.TestSuite.java

License:Apache License

public static void testCommandLineParser(String[] args) throws IOException {
    GenericOptionsParser parser = new GenericOptionsParser(new Configuration(), args);

    CommandLine commandLine = parser.getCommandLine();

    Option[] options = commandLine.getOptions();

    for (Option op : options) {
        System.out.println(String.format("optName=%s, optValue=%s", op.getOpt(), op.getValue()));
    }/*ww w. ja  v a 2  s.  c o m*/

    // second application argument is the output folder prefix
    String[] appArgs = parser.getRemainingArgs();

    for (String appArg : appArgs) {
        System.out.println(String.format("appArg=%s", appArg));
    }

    int iterationNum = 0;

    String[] arguments = LDAAnalysisDriver.generateArgs(options, new String[0], appArgs[0],
            appArgs[1] + "-iter-" + iterationNum);

    System.out.println("Passed in arguments in the first iteration");
    for (String arg : arguments) {
        System.out.println(String.format("argument=%s", arg));
    }

    String[] otherOps = { "-D", "user.args.lda.state.filepath=" + appArgs[1] + "-iter-" + iterationNum
            + File.separator + "part-r-00000" };

    arguments = LDAAnalysisDriver.generateArgs(options, otherOps, appArgs[0],
            appArgs[1] + "-iter-" + ++iterationNum);

    System.out.println("Passed in arguments in the second iteration");
    for (String arg : arguments) {
        System.out.println(String.format("argument=%s", arg));
    }
}

From source file:org.apache.ambari.servicemonitor.utils.ToolRunnerPlus.java

License:Apache License

/**
 * Get ready to run a tool but don't actually run it. This is for test purposes.
 * @param conf Configuration to start with
 * @param tool tool to set up/*from  w w w  .  java 2s. c  o  m*/
 * @param args command line arguments
 * @return either an array of unparsed elements, or null, meaning "preparation failed, do not execute"
 * @throws IOException on problems
 */
public static String[] prepareToExecute(Configuration conf, ToolPlus tool, String[] args) throws IOException {
    boolean canRun = true;
    if (conf == null) {
        conf = new Configuration();
    }
    Options options = tool.createToolSpecificOptions();
    if (options == null) {
        options = new Options();
    }

    options.addOption("p", "dump", false, "dump the current configuration");
    options.addOption("u", "usage", false, "Print the Usage");

    GenericOptionsParser parser = new GenericOptionsParser(conf, options, args);

    //process our local values

    //set the configuration back, so that Tool can configure itself
    Configuration configuration = parser.getConfiguration();
    CommandLine commandLine = parser.getCommandLine();

    if (commandLine == null) {
        dumpArguments(args);
        canRun = false;
    } else {
        if (commandLine.hasOption("u")) {
            usage(args, tool, options);
            canRun = false;
        } else {
            tool.setConf(configuration);
            tool.setCommandLine(commandLine);

            if (commandLine.hasOption("p")) {
                //dump the commands
                dumpArguments(args);
                //dump the configuration
                dumpConf(conf);

                dumpSystemState();

                String toolDump = tool.dump();
                if (toolDump != null && !toolDump.isEmpty()) {
                    println(toolDump);
                }
            }
        }
    }

    String[] toolArgs;
    if (canRun) {
        toolArgs = parser.getRemainingArgs();
    } else {
        toolArgs = null;
    }
    return toolArgs;
}

From source file:org.apache.tez.examples.TezExampleBase.java

License:Apache License

@Override
public final int run(String[] args) throws Exception {
    Configuration conf = getConf();
    GenericOptionsParser optionParser = new GenericOptionsParser(conf, getExtraOptions(), args);
    String[] otherArgs = optionParser.getRemainingArgs();
    if (optionParser.getCommandLine().hasOption(LOCAL_MODE)) {
        isLocalMode = true;//  ww w .  j  a  v  a2s  . c o m
    }
    if (optionParser.getCommandLine().hasOption(DISABLE_SPLIT_GROUPING)) {
        disableSplitGrouping = true;
    }
    return _execute(otherArgs, null, null);
}

From source file:org.apache.tez.examples.TezExampleBase.java

License:Apache License

/**
 * Utility method to use the example from within code or a test.
 *
 * @param conf      the tez configuration instance which will be used to crate the DAG and
 *                  possible the Tez Client.
 * @param args      arguments to the example
 * @param tezClient an existing running {@link org.apache.tez.client.TezClient} instance if one
 *                  exists. If no TezClient is specified (null), one will be created based on the
 *                  provided configuration. If TezClient is specified, local mode option can not been
 *                  specified in arguments, it takes no effect.
 * @return Zero indicates success, non-zero indicates failure
 * @throws Exception /*from   ww  w.  j  a  va2  s.com*/
 */
public int run(TezConfiguration conf, String[] args, @Nullable TezClient tezClient) throws Exception {
    setConf(conf);
    GenericOptionsParser optionParser = new GenericOptionsParser(conf, getExtraOptions(), args);
    if (optionParser.getCommandLine().hasOption(LOCAL_MODE)) {
        isLocalMode = true;
        if (tezClient != null) {
            throw new RuntimeException(
                    "can't specify local mode when TezClient is created, it takes no effect");
        }
    }
    if (optionParser.getCommandLine().hasOption(DISABLE_SPLIT_GROUPING)) {
        disableSplitGrouping = true;
    }
    String[] otherArgs = optionParser.getRemainingArgs();
    return _execute(otherArgs, conf, tezClient);
}