Search in sources :

Example 16 with CommandLineParser

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

the class RegistryCli method rm.

@SuppressWarnings("unchecked")
public int rm(String[] args) {
    Option recursive = OptionBuilder.withArgName("recursive").withDescription("delete recursively").create("r");
    Options rmOption = new Options();
    rmOption.addOption(recursive);
    boolean recursiveOpt = false;
    CommandLineParser parser = new GnuParser();
    try {
        CommandLine line = parser.parse(rmOption, args);
        List<String> argsList = line.getArgList();
        if (argsList.size() != 2) {
            return usageError("RM requires exactly one path argument", RM_USAGE);
        }
        if (!validatePath(argsList.get(1))) {
            return -1;
        }
        try {
            if (line.hasOption("r")) {
                recursiveOpt = true;
            }
            registry.delete(argsList.get(1), recursiveOpt);
            return 0;
        } catch (Exception e) {
            syserr.println(analyzeException("rm", e, argsList));
        }
        return -1;
    } catch (ParseException exp) {
        return usageError("Invalid syntax " + exp.toString(), RM_USAGE);
    }
}
Also used : Options(org.apache.commons.cli.Options) 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) URISyntaxException(java.net.URISyntaxException) InvalidRecordException(org.apache.hadoop.registry.client.exceptions.InvalidRecordException) InvalidPathnameException(org.apache.hadoop.registry.client.exceptions.InvalidPathnameException) AuthenticationFailedException(org.apache.hadoop.registry.client.exceptions.AuthenticationFailedException) PathNotFoundException(org.apache.hadoop.fs.PathNotFoundException) NoRecordException(org.apache.hadoop.registry.client.exceptions.NoRecordException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) AccessControlException(org.apache.hadoop.security.AccessControlException) NoPathPermissionsException(org.apache.hadoop.registry.client.exceptions.NoPathPermissionsException)

Example 17 with CommandLineParser

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

the class RegistryCli method resolve.

@SuppressWarnings("unchecked")
public int resolve(String[] args) {
    Options resolveOption = new Options();
    CommandLineParser parser = new GnuParser();
    try {
        CommandLine line = parser.parse(resolveOption, args);
        List<String> argsList = line.getArgList();
        if (argsList.size() != 2) {
            return usageError("resolve requires exactly one path argument", RESOLVE_USAGE);
        }
        if (!validatePath(argsList.get(1))) {
            return -1;
        }
        try {
            ServiceRecord record = registry.resolve(argsList.get(1));
            for (Endpoint endpoint : record.external) {
                sysout.println(" Endpoint(ProtocolType=" + endpoint.protocolType + ", Api=" + endpoint.api + ");" + " Addresses(AddressType=" + endpoint.addressType + ") are: ");
                for (Map<String, String> address : endpoint.addresses) {
                    sysout.println("[ ");
                    for (Map.Entry<String, String> entry : address.entrySet()) {
                        sysout.print("\t" + entry.getKey() + ":" + entry.getValue());
                    }
                    sysout.println("\n]");
                }
                sysout.println();
            }
            return 0;
        } catch (Exception e) {
            syserr.println(analyzeException("resolve", e, argsList));
        }
        return -1;
    } catch (ParseException exp) {
        return usageError("Invalid syntax " + exp, RESOLVE_USAGE);
    }
}
Also used : Options(org.apache.commons.cli.Options) GnuParser(org.apache.commons.cli.GnuParser) URISyntaxException(java.net.URISyntaxException) InvalidRecordException(org.apache.hadoop.registry.client.exceptions.InvalidRecordException) InvalidPathnameException(org.apache.hadoop.registry.client.exceptions.InvalidPathnameException) AuthenticationFailedException(org.apache.hadoop.registry.client.exceptions.AuthenticationFailedException) PathNotFoundException(org.apache.hadoop.fs.PathNotFoundException) NoRecordException(org.apache.hadoop.registry.client.exceptions.NoRecordException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) AccessControlException(org.apache.hadoop.security.AccessControlException) NoPathPermissionsException(org.apache.hadoop.registry.client.exceptions.NoPathPermissionsException) ServiceRecord(org.apache.hadoop.registry.client.types.ServiceRecord) CommandLine(org.apache.commons.cli.CommandLine) Endpoint(org.apache.hadoop.registry.client.types.Endpoint) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) Map(java.util.Map)

Example 18 with CommandLineParser

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

the class SLSRunner method main.

public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("inputrumen", true, "input rumen files");
    options.addOption("inputsls", true, "input sls files");
    options.addOption("nodes", true, "input topology");
    options.addOption("output", true, "output directory");
    options.addOption("trackjobs", true, "jobs to be tracked during simulating");
    options.addOption("printsimulation", false, "print out simulation information");
    CommandLineParser parser = new GnuParser();
    CommandLine cmd = parser.parse(options, args);
    String inputRumen = cmd.getOptionValue("inputrumen");
    String inputSLS = cmd.getOptionValue("inputsls");
    String output = cmd.getOptionValue("output");
    if ((inputRumen == null && inputSLS == null) || output == null) {
        System.err.println();
        System.err.println("ERROR: Missing input or output file");
        System.err.println();
        System.err.println("Options: -inputrumen|-inputsls FILE,FILE... " + "-output FILE [-nodes FILE] [-trackjobs JobId,JobId...] " + "[-printsimulation]");
        System.err.println();
        System.exit(1);
    }
    File outputFile = new File(output);
    if (!outputFile.exists() && !outputFile.mkdirs()) {
        System.err.println("ERROR: Cannot create output directory " + outputFile.getAbsolutePath());
        System.exit(1);
    }
    Set<String> trackedJobSet = new HashSet<String>();
    if (cmd.hasOption("trackjobs")) {
        String trackjobs = cmd.getOptionValue("trackjobs");
        String[] jobIds = trackjobs.split(",");
        trackedJobSet.addAll(Arrays.asList(jobIds));
    }
    String nodeFile = cmd.hasOption("nodes") ? cmd.getOptionValue("nodes") : "";
    boolean isSLS = inputSLS != null;
    String[] inputFiles = isSLS ? inputSLS.split(",") : inputRumen.split(",");
    SLSRunner sls = new SLSRunner(isSLS, inputFiles, nodeFile, output, trackedJobSet, cmd.hasOption("printsimulation"));
    sls.start();
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) File(java.io.File) HashSet(java.util.HashSet)

Example 19 with CommandLineParser

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

the class WALPrettyPrinter method run.

/**
   * Pass one or more log file names and formatting options and it will dump out
   * a text version of the contents on <code>stdout</code>.
   *
   * @param args
   *          Command line arguments
   * @throws IOException
   *           Thrown upon file system errors etc.
   */
public static void run(String[] args) throws IOException {
    // create options
    Options options = new Options();
    options.addOption("h", "help", false, "Output help message");
    options.addOption("j", "json", false, "Output JSON");
    options.addOption("p", "printvals", false, "Print values");
    options.addOption("r", "region", true, "Region to filter by. Pass encoded region name; e.g. '9192caead6a5a20acb4454ffbc79fa14'");
    options.addOption("s", "sequence", true, "Sequence to filter by. Pass sequence number.");
    options.addOption("w", "row", true, "Row to filter by. Pass row name.");
    WALPrettyPrinter printer = new WALPrettyPrinter();
    CommandLineParser parser = new PosixParser();
    List<?> files = null;
    try {
        CommandLine cmd = parser.parse(options, args);
        files = cmd.getArgList();
        if (files.isEmpty() || cmd.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("WAL <filename...>", options, true);
            System.exit(-1);
        }
        // configure the pretty printer using command line options
        if (cmd.hasOption("p"))
            printer.enableValues();
        if (cmd.hasOption("j"))
            printer.enableJSON();
        if (cmd.hasOption("r"))
            printer.setRegionFilter(cmd.getOptionValue("r"));
        if (cmd.hasOption("s"))
            printer.setSequenceFilter(Long.parseLong(cmd.getOptionValue("s")));
        if (cmd.hasOption("w"))
            printer.setRowFilter(cmd.getOptionValue("w"));
    } catch (ParseException e) {
        e.printStackTrace();
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("HFile filename(s) ", options, true);
        System.exit(-1);
    }
    // get configuration, file system, and process the given files
    Configuration conf = HBaseConfiguration.create();
    FSUtils.setFsDefault(conf, FSUtils.getRootDir(conf));
    // begin output
    printer.beginPersistentOutput();
    for (Object f : files) {
        Path file = new Path((String) f);
        FileSystem fs = file.getFileSystem(conf);
        if (!fs.exists(file)) {
            System.err.println("ERROR, file doesnt exist: " + file);
            return;
        }
        printer.processFile(conf, file);
    }
    printer.endPersistentOutput();
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Path(org.apache.hadoop.fs.Path) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) Configuration(org.apache.hadoop.conf.Configuration) HBaseConfiguration(org.apache.hadoop.hbase.HBaseConfiguration) PosixParser(org.apache.commons.cli.PosixParser) FileSystem(org.apache.hadoop.fs.FileSystem) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 20 with CommandLineParser

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

the class TimelineSchemaCreator method parseArgs.

/**
   * Parse command-line arguments.
   *
   * @param args
   *          command line arguments passed to program.
   * @return parsed command line.
   * @throws ParseException
   */
private static CommandLine parseArgs(String[] args) throws ParseException {
    Options options = new Options();
    // Input
    Option o = new Option(ENTITY_TABLE_NAME_SHORT, "entityTableName", true, "entity table name");
    o.setArgName("entityTableName");
    o.setRequired(false);
    options.addOption(o);
    o = new Option(TTL_OPTION_SHORT, "metricsTTL", true, "TTL for metrics column family");
    o.setArgName("metricsTTL");
    o.setRequired(false);
    options.addOption(o);
    o = new Option(APP_TO_FLOW_TABLE_NAME_SHORT, "appToflowTableName", true, "app to flow table name");
    o.setArgName("appToflowTableName");
    o.setRequired(false);
    options.addOption(o);
    o = new Option(APP_TABLE_NAME_SHORT, "applicationTableName", true, "application table name");
    o.setArgName("applicationTableName");
    o.setRequired(false);
    options.addOption(o);
    // Options without an argument
    // No need to set arg name since we do not need an argument here
    o = new Option(SKIP_EXISTING_TABLE_OPTION_SHORT, "skipExistingTable", false, "skip existing Hbase tables and continue to create new tables");
    o.setRequired(false);
    options.addOption(o);
    CommandLineParser parser = new PosixParser();
    CommandLine commandLine = null;
    try {
        commandLine = parser.parse(options, args);
    } catch (Exception e) {
        LOG.error("ERROR: " + e.getMessage() + "\n");
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(NAME + " ", options, true);
        System.exit(-1);
    }
    return commandLine;
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) IOException(java.io.IOException) 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