Search in sources :

Example 86 with Option

use of org.apache.commons.cli.Option in project hive by apache.

the class HCatCli method main.

@SuppressWarnings("static-access")
public static void main(String[] args) {
    try {
        LogUtils.initHiveLog4j();
    } catch (LogInitializationException e) {
    }
    LOG = LoggerFactory.getLogger(HCatCli.class);
    CliSessionState ss = new CliSessionState(new HiveConf(SessionState.class));
    ss.in = System.in;
    try {
        ss.out = new PrintStream(System.out, true, "UTF-8");
        ss.err = new PrintStream(System.err, true, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        System.exit(1);
    }
    HiveConf conf = ss.getConf();
    HiveConf.setVar(conf, ConfVars.SEMANTIC_ANALYZER_HOOK, HCatSemanticAnalyzer.class.getName());
    String engine = HiveConf.getVar(conf, ConfVars.HIVE_EXECUTION_ENGINE);
    final String MR_ENGINE = "mr";
    if (!MR_ENGINE.equalsIgnoreCase(engine)) {
        HiveConf.setVar(conf, ConfVars.HIVE_EXECUTION_ENGINE, MR_ENGINE);
        LOG.info("Forcing " + ConfVars.HIVE_EXECUTION_ENGINE + " to " + MR_ENGINE);
    }
    Options options = new Options();
    // -e 'quoted-query-string'
    options.addOption(OptionBuilder.hasArg().withArgName("exec").withDescription("hcat command given from command line").create('e'));
    // -f <query-file>
    options.addOption(OptionBuilder.hasArg().withArgName("file").withDescription("hcat commands in file").create('f'));
    // -g
    options.addOption(OptionBuilder.hasArg().withArgName("group").withDescription("group for the db/table specified in CREATE statement").create('g'));
    // -p
    options.addOption(OptionBuilder.hasArg().withArgName("perms").withDescription("permissions for the db/table specified in CREATE statement").create('p'));
    // -D
    options.addOption(OptionBuilder.hasArgs(2).withArgName("property=value").withValueSeparator().withDescription("use hadoop value for given property").create('D'));
    // [-h|--help]
    options.addOption(new Option("h", "help", false, "Print help information"));
    Parser parser = new GnuParser();
    CommandLine cmdLine = null;
    try {
        cmdLine = parser.parse(options, args);
    } catch (ParseException e) {
        printUsage(options, System.err);
        // Note, we print to System.err instead of ss.err, because if we can't parse our
        // commandline, we haven't even begun, and therefore cannot be expected to have
        // reasonably constructed or started the SessionState.
        System.exit(1);
    }
    // -D : process these first, so that we can instantiate SessionState appropriately.
    setConfProperties(conf, cmdLine.getOptionProperties("D"));
    // Now that the properties are in, we can instantiate SessionState.
    SessionState.start(ss);
    // -h
    if (cmdLine.hasOption('h')) {
        printUsage(options, ss.out);
        sysExit(ss, 0);
    }
    // -e
    String execString = (String) cmdLine.getOptionValue('e');
    // -f
    String fileName = (String) cmdLine.getOptionValue('f');
    if (execString != null && fileName != null) {
        ss.err.println("The '-e' and '-f' options cannot be specified simultaneously");
        printUsage(options, ss.err);
        sysExit(ss, 1);
    }
    // -p
    String perms = (String) cmdLine.getOptionValue('p');
    if (perms != null) {
        validatePermissions(ss, conf, perms);
    }
    // -g
    String grp = (String) cmdLine.getOptionValue('g');
    if (grp != null) {
        conf.set(HCatConstants.HCAT_GROUP, grp);
    }
    if (execString != null) {
        sysExit(ss, processLine(execString));
    }
    try {
        if (fileName != null) {
            sysExit(ss, processFile(fileName));
        }
    } catch (FileNotFoundException e) {
        ss.err.println("Input file not found. (" + e.getMessage() + ")");
        sysExit(ss, 1);
    } catch (IOException e) {
        ss.err.println("Could not open input file for reading. (" + e.getMessage() + ")");
        sysExit(ss, 1);
    }
    // -h
    printUsage(options, ss.err);
    sysExit(ss, 1);
}
Also used : CliSessionState(org.apache.hadoop.hive.cli.CliSessionState) SessionState(org.apache.hadoop.hive.ql.session.SessionState) PrintStream(java.io.PrintStream) Options(org.apache.commons.cli.Options) GnuParser(org.apache.commons.cli.GnuParser) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) CliSessionState(org.apache.hadoop.hive.cli.CliSessionState) GnuParser(org.apache.commons.cli.GnuParser) Parser(org.apache.commons.cli.Parser) LogInitializationException(org.apache.hadoop.hive.common.LogUtils.LogInitializationException) CommandLine(org.apache.commons.cli.CommandLine) HCatSemanticAnalyzer(org.apache.hive.hcatalog.cli.SemanticAnalysis.HCatSemanticAnalyzer) HiveConf(org.apache.hadoop.hive.conf.HiveConf) Option(org.apache.commons.cli.Option) ParseException(org.apache.commons.cli.ParseException)

Example 87 with Option

use of org.apache.commons.cli.Option in project glacier-cli by carlossg.

the class Glacier method commonOptions.

private static Options commonOptions() {
    Options options = new Options();
    Option properties = OptionBuilder.withArgName("properties").hasArg().withDescription("Path to an AWSCredentials properties file. Defaults to '~/AwsCredentials.properties'").create("properties");
    options.addOption(properties);
    Option region = OptionBuilder.withArgName("region").hasArg().withDescription("Specify URL as the web service URL to use. Defaults to 'us-east-1'").create("region");
    options.addOption(region);
    Option topic = OptionBuilder.withArgName("topic_name").hasArg().withDescription("SNS topic to use for inventory retrieval. Defaults to 'glacier'").create("topic");
    options.addOption(topic);
    Option queue = OptionBuilder.withArgName("queue_name").hasArg().withDescription("SQS queue to use for inventory retrieval. Defaults to 'glacier'").create("queue");
    options.addOption(queue);
    Option output = OptionBuilder.withArgName("file_name").hasArg().withDescription("File to save the inventory to. Defaults to 'glacier.json'").create("output");
    options.addOption(output);
    Option help = OptionBuilder.withArgName("help").withDescription("Show help information").create("help");
    options.addOption(help);
    return options;
}
Also used : Options(org.apache.commons.cli.Options) Option(org.apache.commons.cli.Option)

Example 88 with Option

use of org.apache.commons.cli.Option in project learning-spark by databricks.

the class LogAnalyzerAppMain method createOptions.

private static Options createOptions() {
    Options options = new Options();
    options.addOption(new Option(WINDOW_LENGTH, false, "The window length in seconds"));
    options.addOption(new Option(SLIDE_INTERVAL, false, "The slide interval in seconds"));
    options.addOption(new Option(LOGS_DIRECTORY, true, "The directory where logs are written"));
    options.addOption(new Option(OUTPUT_HTML_FILE, false, "Where to write output html file"));
    options.addOption(new Option(CHECKPOINT_DIRECTORY, false, "The checkpoint directory."));
    options.addOption(new Option(INDEX_HTML_TEMPLATE, true, "path to the index.html.template file - accessible from all workers"));
    options.addOption(new Option(OUTPUT_DIRECTORY, false, "path to output DSTreams too"));
    return options;
}
Also used : Options(org.apache.commons.cli.Options) Option(org.apache.commons.cli.Option)

Example 89 with Option

use of org.apache.commons.cli.Option in project druid by druid-io.

the class RabbitMQProducerMain method main.

public static void main(String[] args) throws Exception {
    // We use a List to keep track of option insertion order. See below.
    final List<Option> optionList = new ArrayList<Option>();
    optionList.add(OptionBuilder.withLongOpt("help").withDescription("display this help message").create("h"));
    optionList.add(OptionBuilder.withLongOpt("hostname").hasArg().withDescription("the hostname of the AMQP broker [defaults to AMQP library default]").create("b"));
    optionList.add(OptionBuilder.withLongOpt("port").hasArg().withDescription("the port of the AMQP broker [defaults to AMQP library default]").create("n"));
    optionList.add(OptionBuilder.withLongOpt("username").hasArg().withDescription("username to connect to the AMQP broker [defaults to AMQP library default]").create("u"));
    optionList.add(OptionBuilder.withLongOpt("password").hasArg().withDescription("password to connect to the AMQP broker [defaults to AMQP library default]").create("p"));
    optionList.add(OptionBuilder.withLongOpt("vhost").hasArg().withDescription("name of virtual host on the AMQP broker [defaults to AMQP library default]").create("v"));
    optionList.add(OptionBuilder.withLongOpt("exchange").isRequired().hasArg().withDescription("name of the AMQP exchange [required - no default]").create("e"));
    optionList.add(OptionBuilder.withLongOpt("key").hasArg().withDescription("the routing key to use when sending messages [default: 'default.routing.key']").create("k"));
    optionList.add(OptionBuilder.withLongOpt("type").hasArg().withDescription("the type of exchange to create [default: 'topic']").create("t"));
    optionList.add(OptionBuilder.withLongOpt("durable").withDescription("if set, a durable exchange will be declared [default: not set]").create("d"));
    optionList.add(OptionBuilder.withLongOpt("autodelete").withDescription("if set, an auto-delete exchange will be declared [default: not set]").create("a"));
    optionList.add(OptionBuilder.withLongOpt("single").withDescription("if set, only a single message will be sent [default: not set]").create("s"));
    optionList.add(OptionBuilder.withLongOpt("start").hasArg().withDescription("time to use to start sending messages from [default: 2010-01-01T00:00:00]").create());
    optionList.add(OptionBuilder.withLongOpt("stop").hasArg().withDescription("time to use to send messages until (format: '2013-07-18T23:45:59') [default: current time]").create());
    optionList.add(OptionBuilder.withLongOpt("interval").hasArg().withDescription("the interval to add to the timestamp between messages in seconds [default: 10]").create());
    optionList.add(OptionBuilder.withLongOpt("delay").hasArg().withDescription("the delay between sending messages in milliseconds [default: 100]").create());
    // An extremely silly hack to maintain the above order in the help formatting.
    HelpFormatter formatter = new HelpFormatter();
    // Add a comparator to the HelpFormatter using the ArrayList above to sort by insertion order.
    formatter.setOptionComparator(new Comparator() {

        @Override
        public int compare(Object o1, Object o2) {
            // I know this isn't fast, but who cares! The list is short.
            return optionList.indexOf(o1) - optionList.indexOf(o2);
        }
    });
    // Now we can add all the options to an Options instance. This is dumb!
    Options options = new Options();
    for (Option option : optionList) {
        options.addOption(option);
    }
    CommandLine cmd = null;
    try {
        cmd = new BasicParser().parse(options, args);
    } catch (ParseException e) {
        formatter.printHelp("RabbitMQProducerMain", e.getMessage(), options, null);
        System.exit(1);
    }
    if (cmd.hasOption("h")) {
        formatter.printHelp("RabbitMQProducerMain", options);
        System.exit(2);
    }
    ConnectionFactory factory = new ConnectionFactory();
    if (cmd.hasOption("b")) {
        factory.setHost(cmd.getOptionValue("b"));
    }
    if (cmd.hasOption("u")) {
        factory.setUsername(cmd.getOptionValue("u"));
    }
    if (cmd.hasOption("p")) {
        factory.setPassword(cmd.getOptionValue("p"));
    }
    if (cmd.hasOption("v")) {
        factory.setVirtualHost(cmd.getOptionValue("v"));
    }
    if (cmd.hasOption("n")) {
        factory.setPort(Integer.parseInt(cmd.getOptionValue("n")));
    }
    String exchange = cmd.getOptionValue("e");
    String routingKey = "default.routing.key";
    if (cmd.hasOption("k")) {
        routingKey = cmd.getOptionValue("k");
    }
    boolean durable = cmd.hasOption("d");
    boolean autoDelete = cmd.hasOption("a");
    String type = cmd.getOptionValue("t", "topic");
    boolean single = cmd.hasOption("single");
    int interval = Integer.parseInt(cmd.getOptionValue("interval", "10"));
    int delay = Integer.parseInt(cmd.getOptionValue("delay", "100"));
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date stop = sdf.parse(cmd.getOptionValue("stop", sdf.format(new Date())));
    Random r = new Random();
    Calendar timer = Calendar.getInstance();
    timer.setTime(sdf.parse(cmd.getOptionValue("start", "2010-01-01T00:00:00")));
    String msg_template = "{\"utcdt\": \"%s\", \"wp\": %d, \"gender\": \"%s\", \"age\": %d}";
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(exchange, type, durable, autoDelete, null);
    do {
        int wp = (10 + r.nextInt(90)) * 100;
        String gender = r.nextBoolean() ? "male" : "female";
        int age = 20 + r.nextInt(70);
        String line = String.format(msg_template, sdf.format(timer.getTime()), wp, gender, age);
        channel.basicPublish(exchange, routingKey, null, line.getBytes());
        System.out.println("Sent message: " + line);
        timer.add(Calendar.SECOND, interval);
        Thread.sleep(delay);
    } while ((!single && stop.after(timer.getTime())));
    connection.close();
}
Also used : Options(org.apache.commons.cli.Options) Calendar(java.util.Calendar) Channel(com.rabbitmq.client.Channel) ArrayList(java.util.ArrayList) Connection(com.rabbitmq.client.Connection) Date(java.util.Date) Comparator(java.util.Comparator) HelpFormatter(org.apache.commons.cli.HelpFormatter) BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Random(java.util.Random) Option(org.apache.commons.cli.Option) ParseException(org.apache.commons.cli.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 90 with Option

use of org.apache.commons.cli.Option in project moco by dreamhead.

the class StartArgsParser method configOption.

protected Option configOption() {
    Option opt = new Option("c", true, "config");
    opt.setType(String.class);
    opt.setRequired(false);
    return opt;
}
Also used : Option(org.apache.commons.cli.Option)

Aggregations

Option (org.apache.commons.cli.Option)152 Options (org.apache.commons.cli.Options)105 CommandLine (org.apache.commons.cli.CommandLine)53 CommandLineParser (org.apache.commons.cli.CommandLineParser)52 ParseException (org.apache.commons.cli.ParseException)41 GnuParser (org.apache.commons.cli.GnuParser)39 HelpFormatter (org.apache.commons.cli.HelpFormatter)30 File (java.io.File)13 OptionGroup (org.apache.commons.cli.OptionGroup)13 FileInputStream (java.io.FileInputStream)10 IOException (java.io.IOException)10 HashMap (java.util.HashMap)9 DefaultParser (org.apache.commons.cli.DefaultParser)9 Properties (java.util.Properties)8 BasicParser (org.apache.commons.cli.BasicParser)6 ConsoleAppender (org.apache.log4j.ConsoleAppender)6 PatternLayout (org.apache.log4j.PatternLayout)6 ArrayList (java.util.ArrayList)5 PosixParser (org.apache.commons.cli.PosixParser)5 List (java.util.List)3