Search in sources :

Example 6 with CommandLineParser

use of org.apache.commons.cli.CommandLineParser in project flink by apache.

the class FlinkYarnSessionCliTest method testDynamicProperties.

@Test
public void testDynamicProperties() throws IOException {
    Map<String, String> map = new HashMap<String, String>(System.getenv());
    File tmpFolder = tmp.newFolder();
    File fakeConf = new File(tmpFolder, "flink-conf.yaml");
    fakeConf.createNewFile();
    map.put(ConfigConstants.ENV_FLINK_CONF_DIR, tmpFolder.getAbsolutePath());
    TestBaseUtils.setEnv(map);
    FlinkYarnSessionCli cli = new FlinkYarnSessionCli("", "", false);
    Options options = new Options();
    cli.addGeneralOptions(options);
    cli.addRunOptions(options);
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, new String[] { "run", "-j", "fake.jar", "-n", "15", "-D", "akka.ask.timeout=5 min" });
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("Parsing failed with " + e.getMessage());
    }
    AbstractYarnClusterDescriptor flinkYarnDescriptor = cli.createDescriptor(null, cmd);
    Assert.assertNotNull(flinkYarnDescriptor);
    Map<String, String> dynProperties = FlinkYarnSessionCli.getDynamicProperties(flinkYarnDescriptor.getDynamicPropertiesEncoded());
    Assert.assertEquals(1, dynProperties.size());
    Assert.assertEquals("5 min", dynProperties.get("akka.ask.timeout"));
}
Also used : Options(org.apache.commons.cli.Options) RunOptions(org.apache.flink.client.cli.RunOptions) CommandLine(org.apache.commons.cli.CommandLine) HashMap(java.util.HashMap) CommandLineParser(org.apache.commons.cli.CommandLineParser) File(java.io.File) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) IOException(java.io.IOException) FlinkYarnSessionCli(org.apache.flink.yarn.cli.FlinkYarnSessionCli) DefaultParser(org.apache.commons.cli.DefaultParser) Test(org.junit.Test)

Example 7 with CommandLineParser

use of org.apache.commons.cli.CommandLineParser in project hadoop by apache.

the class MiniDFSClusterManager method parseArguments.

/**
   * Parses arguments and fills out the member variables.
   * @param args Command-line arguments.
   * @return true on successful parse; false to indicate that the
   * program should exit.
   */
private boolean parseArguments(String[] args) {
    Options options = makeOptions();
    CommandLine cli;
    try {
        CommandLineParser parser = new GnuParser();
        cli = parser.parse(options, args);
    } catch (ParseException e) {
        LOG.warn("options parsing failed:  " + e.getMessage());
        new HelpFormatter().printHelp("...", options);
        return false;
    }
    if (cli.hasOption("help")) {
        new HelpFormatter().printHelp("...", options);
        return false;
    }
    if (cli.getArgs().length > 0) {
        for (String arg : cli.getArgs()) {
            LOG.error("Unrecognized option: " + arg);
            new HelpFormatter().printHelp("...", options);
            return false;
        }
    }
    // HDFS
    numDataNodes = intArgument(cli, "datanodes", 1);
    nameNodePort = intArgument(cli, "nnport", 0);
    nameNodeHttpPort = intArgument(cli, "httpport", 0);
    if (cli.hasOption("format")) {
        dfsOpts = StartupOption.FORMAT;
        format = true;
    } else {
        dfsOpts = StartupOption.REGULAR;
        format = false;
    }
    // Runner
    writeDetails = cli.getOptionValue("writeDetails");
    writeConfig = cli.getOptionValue("writeConfig");
    // General
    conf = new HdfsConfiguration();
    updateConfiguration(conf, cli.getOptionValues("D"));
    return true;
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration)

Example 8 with CommandLineParser

use of org.apache.commons.cli.CommandLineParser in project hadoop by apache.

the class OfflineImageViewerPB method run.

public static int run(String[] args) throws Exception {
    Options options = buildOptions();
    if (args.length == 0) {
        printUsage();
        return 0;
    }
    // print help and exit with zero exit code
    if (args.length == 1 && isHelpOption(args[0])) {
        printUsage();
        return 0;
    }
    CommandLineParser parser = new PosixParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println("Error parsing command-line options: ");
        printUsage();
        return -1;
    }
    if (cmd.hasOption("h")) {
        // print help and exit with non zero exit code since
        // it is not expected to give help and other options together.
        printUsage();
        return -1;
    }
    String inputFile = cmd.getOptionValue("i");
    String processor = cmd.getOptionValue("p", "Web");
    String outputFile = cmd.getOptionValue("o", "-");
    String delimiter = cmd.getOptionValue("delimiter", PBImageDelimitedTextWriter.DEFAULT_DELIMITER);
    String tempPath = cmd.getOptionValue("t", "");
    Configuration conf = new Configuration();
    try (PrintStream out = outputFile.equals("-") ? System.out : new PrintStream(outputFile, "UTF-8")) {
        switch(processor) {
            case "FileDistribution":
                long maxSize = Long.parseLong(cmd.getOptionValue("maxSize", "0"));
                int step = Integer.parseInt(cmd.getOptionValue("step", "0"));
                boolean formatOutput = cmd.hasOption("format");
                new FileDistributionCalculator(conf, maxSize, step, formatOutput, out).visit(new RandomAccessFile(inputFile, "r"));
                break;
            case "XML":
                new PBImageXmlWriter(conf, out).visit(new RandomAccessFile(inputFile, "r"));
                break;
            case "ReverseXML":
                try {
                    OfflineImageReconstructor.run(inputFile, outputFile);
                } catch (Exception e) {
                    System.err.println("OfflineImageReconstructor failed: " + e.getMessage());
                    e.printStackTrace(System.err);
                    System.exit(1);
                }
                break;
            case "Web":
                String addr = cmd.getOptionValue("addr", "localhost:5978");
                try (WebImageViewer viewer = new WebImageViewer(NetUtils.createSocketAddr(addr))) {
                    viewer.start(inputFile);
                }
                break;
            case "Delimited":
                try (PBImageDelimitedTextWriter writer = new PBImageDelimitedTextWriter(out, delimiter, tempPath)) {
                    writer.visit(new RandomAccessFile(inputFile, "r"));
                }
                break;
            default:
                System.err.println("Invalid processor specified : " + processor);
                printUsage();
                return -1;
        }
        return 0;
    } catch (EOFException e) {
        System.err.println("Input file ended unexpectedly. Exiting");
    } catch (IOException e) {
        System.err.println("Encountered exception.  Exiting: " + e.getMessage());
        e.printStackTrace(System.err);
    }
    return -1;
}
Also used : Options(org.apache.commons.cli.Options) PrintStream(java.io.PrintStream) Configuration(org.apache.hadoop.conf.Configuration) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) ParseException(org.apache.commons.cli.ParseException) CommandLine(org.apache.commons.cli.CommandLine) RandomAccessFile(java.io.RandomAccessFile) EOFException(java.io.EOFException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 9 with CommandLineParser

use of org.apache.commons.cli.CommandLineParser in project hadoop by apache.

the class OfflineImageViewer method main.

/**
   * Entry point to command-line-driven operation.  User may specify
   * options and start fsimage viewer from the command line.  Program
   * will process image file and exit cleanly or, if an error is
   * encountered, inform user and exit.
   *
   * @param args Command line options
   * @throws IOException 
   */
public static void main(String[] args) throws IOException {
    Options options = buildOptions();
    if (args.length == 0) {
        printUsage();
        return;
    }
    CommandLineParser parser = new PosixParser();
    CommandLine cmd;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println("Error parsing command-line options: ");
        printUsage();
        return;
    }
    if (cmd.hasOption("h")) {
        // print help and exit
        printUsage();
        return;
    }
    boolean skipBlocks = cmd.hasOption("skipBlocks");
    boolean printToScreen = cmd.hasOption("printToScreen");
    String inputFile = cmd.getOptionValue("i");
    String processor = cmd.getOptionValue("p", "Ls");
    String outputFile = cmd.getOptionValue("o");
    String delimiter = cmd.getOptionValue("delimiter");
    if (!(delimiter == null || processor.equals("Delimited"))) {
        System.out.println("Can only specify -delimiter with Delimited processor");
        printUsage();
        return;
    }
    ImageVisitor v;
    if (processor.equals("Indented")) {
        v = new IndentedImageVisitor(outputFile, printToScreen);
    } else if (processor.equals("XML")) {
        v = new XmlImageVisitor(outputFile, printToScreen);
    } else if (processor.equals("Delimited")) {
        v = delimiter == null ? new DelimitedImageVisitor(outputFile, printToScreen) : new DelimitedImageVisitor(outputFile, printToScreen, delimiter);
        skipBlocks = false;
    } else if (processor.equals("FileDistribution")) {
        long maxSize = Long.parseLong(cmd.getOptionValue("maxSize", "0"));
        int step = Integer.parseInt(cmd.getOptionValue("step", "0"));
        boolean formatOutput = cmd.hasOption("format");
        v = new FileDistributionVisitor(outputFile, maxSize, step, formatOutput);
    } else if (processor.equals("NameDistribution")) {
        v = new NameDistributionVisitor(outputFile, printToScreen);
    } else {
        v = new LsImageVisitor(outputFile, printToScreen);
        skipBlocks = false;
    }
    try {
        OfflineImageViewer d = new OfflineImageViewer(inputFile, v, skipBlocks);
        d.go();
    } catch (EOFException e) {
        System.err.println("Input file ended unexpectedly.  Exiting");
    } catch (IOException e) {
        System.err.println("Encountered exception.  Exiting: " + e.getMessage());
    }
}
Also used : Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) CommandLine(org.apache.commons.cli.CommandLine) EOFException(java.io.EOFException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 10 with CommandLineParser

use of org.apache.commons.cli.CommandLineParser in project hadoop by apache.

the class JMXGet method parseArgs.

/**
   * parse args
   */
private static CommandLine parseArgs(Options opts, String... args) throws IllegalArgumentException {
    OptionBuilder.withArgName("NameNode|DataNode");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("specify jmx service (NameNode by default)");
    Option jmx_service = OptionBuilder.create("service");
    OptionBuilder.withArgName("mbean server");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("specify mbean server (localhost by default)");
    Option jmx_server = OptionBuilder.create("server");
    OptionBuilder.withDescription("print help");
    Option jmx_help = OptionBuilder.create("help");
    OptionBuilder.withArgName("mbean server port");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("specify mbean server port, " + "if missing - it will try to connect to MBean Server in the same VM");
    Option jmx_port = OptionBuilder.create("port");
    OptionBuilder.withArgName("VM's connector url");
    OptionBuilder.hasArg();
    OptionBuilder.withDescription("connect to the VM on the same machine;" + "\n use:\n jstat -J-Djstat.showUnsupported=true -snap <vmpid> | " + "grep sun.management.JMXConnectorServer.address\n " + "to find the url");
    Option jmx_localVM = OptionBuilder.create("localVM");
    opts.addOption(jmx_server);
    opts.addOption(jmx_help);
    opts.addOption(jmx_service);
    opts.addOption(jmx_port);
    opts.addOption(jmx_localVM);
    CommandLine commandLine = null;
    CommandLineParser parser = new GnuParser();
    try {
        commandLine = parser.parse(opts, args, true);
    } catch (ParseException e) {
        printUsage(opts);
        throw new IllegalArgumentException("invalid args: " + e.getMessage());
    }
    return commandLine;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Aggregations

CommandLineParser (org.apache.commons.cli.CommandLineParser)265 CommandLine (org.apache.commons.cli.CommandLine)246 Options (org.apache.commons.cli.Options)206 ParseException (org.apache.commons.cli.ParseException)186 GnuParser (org.apache.commons.cli.GnuParser)158 HelpFormatter (org.apache.commons.cli.HelpFormatter)111 PosixParser (org.apache.commons.cli.PosixParser)61 Option (org.apache.commons.cli.Option)52 IOException (java.io.IOException)48 Path (org.apache.hadoop.fs.Path)42 File (java.io.File)41 DefaultParser (org.apache.commons.cli.DefaultParser)29 Job (org.apache.hadoop.mapreduce.Job)27 Configuration (org.apache.hadoop.conf.Configuration)19 FileInputStream (java.io.FileInputStream)16 Properties (java.util.Properties)15 ArrayList (java.util.ArrayList)14 BasicParser (org.apache.commons.cli.BasicParser)14 FileSystem (org.apache.hadoop.fs.FileSystem)12 URI (java.net.URI)10