Search in sources :

Example 11 with CommandLine

use of org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine in project hbase by apache.

the class RegionSplitter method main.

/**
 * The main function for the RegionSplitter application. Common uses:
 * <p>
 * <ul>
 * <li>create a table named 'myTable' with 60 pre-split regions containing 2
 * column families 'test' &amp; 'rs', assuming the keys are hex-encoded ASCII:
 * <ul>
 * <li>bin/hbase org.apache.hadoop.hbase.util.RegionSplitter -c 60 -f test:rs
 * myTable HexStringSplit
 * </ul>
 * <li>create a table named 'myTable' with 50 pre-split regions,
 * assuming the keys are decimal-encoded ASCII:
 * <ul>
 * <li>bin/hbase org.apache.hadoop.hbase.util.RegionSplitter -c 50
 * myTable DecimalStringSplit
 * </ul>
 * <li>perform a rolling split of 'myTable' (i.e. 60 =&gt; 120 regions), # 2
 * outstanding splits at a time, assuming keys are uniformly distributed
 * bytes:
 * <ul>
 * <li>bin/hbase org.apache.hadoop.hbase.util.RegionSplitter -r -o 2 myTable
 * UniformSplit
 * </ul>
 * </ul>
 *
 * There are three SplitAlgorithms built into RegionSplitter, HexStringSplit,
 * DecimalStringSplit, and UniformSplit. These are different strategies for
 * choosing region boundaries. See their source code for details.
 *
 * @param args
 *          Usage: RegionSplitter &lt;TABLE&gt; &lt;SPLITALGORITHM&gt;
 *          &lt;-c &lt;# regions&gt; -f &lt;family:family:...&gt; | -r
 *          [-o &lt;# outstanding splits&gt;]&gt;
 *          [-D &lt;conf.param=value&gt;]
 * @throws IOException
 *           HBase IO problem
 * @throws InterruptedException
 *           user requested exit
 * @throws ParseException
 *           problem parsing user input
 */
@SuppressWarnings("static-access")
public static void main(String[] args) throws IOException, InterruptedException, ParseException {
    Configuration conf = HBaseConfiguration.create();
    // parse user input
    Options opt = new Options();
    opt.addOption(OptionBuilder.withArgName("property=value").hasArg().withDescription("Override HBase Configuration Settings").create("D"));
    opt.addOption(OptionBuilder.withArgName("region count").hasArg().withDescription("Create a new table with a pre-split number of regions").create("c"));
    opt.addOption(OptionBuilder.withArgName("family:family:...").hasArg().withDescription("Column Families to create with new table.  Required with -c").create("f"));
    opt.addOption("h", false, "Print this usage help");
    opt.addOption("r", false, "Perform a rolling split of an existing region");
    opt.addOption(OptionBuilder.withArgName("count").hasArg().withDescription("Max outstanding splits that have unfinished major compactions").create("o"));
    opt.addOption(null, "firstrow", true, "First Row in Table for Split Algorithm");
    opt.addOption(null, "lastrow", true, "Last Row in Table for Split Algorithm");
    opt.addOption(null, "risky", false, "Skip verification steps to complete quickly. " + "STRONGLY DISCOURAGED for production systems.  ");
    CommandLine cmd = new GnuParser().parse(opt, args);
    if (cmd.hasOption("D")) {
        for (String confOpt : cmd.getOptionValues("D")) {
            String[] kv = confOpt.split("=", 2);
            if (kv.length == 2) {
                conf.set(kv[0], kv[1]);
                LOG.debug("-D configuration override: " + kv[0] + "=" + kv[1]);
            } else {
                throw new ParseException("-D option format invalid: " + confOpt);
            }
        }
    }
    if (cmd.hasOption("risky")) {
        conf.setBoolean("split.verify", false);
    }
    boolean createTable = cmd.hasOption("c") && cmd.hasOption("f");
    boolean rollingSplit = cmd.hasOption("r");
    boolean oneOperOnly = createTable ^ rollingSplit;
    if (2 != cmd.getArgList().size() || !oneOperOnly || cmd.hasOption("h")) {
        new HelpFormatter().printHelp("bin/hbase regionsplitter <TABLE> <SPLITALGORITHM>\n" + "SPLITALGORITHM is the java class name of a class implementing " + "SplitAlgorithm, or one of the special strings HexStringSplit or " + "DecimalStringSplit or UniformSplit, which are built-in split algorithms. " + "HexStringSplit treats keys as hexadecimal ASCII, and " + "DecimalStringSplit treats keys as decimal ASCII, and " + "UniformSplit treats keys as arbitrary bytes.", opt);
        return;
    }
    TableName tableName = TableName.valueOf(cmd.getArgs()[0]);
    String splitClass = cmd.getArgs()[1];
    SplitAlgorithm splitAlgo = newSplitAlgoInstance(conf, splitClass);
    if (cmd.hasOption("firstrow")) {
        splitAlgo.setFirstRow(cmd.getOptionValue("firstrow"));
    }
    if (cmd.hasOption("lastrow")) {
        splitAlgo.setLastRow(cmd.getOptionValue("lastrow"));
    }
    if (createTable) {
        conf.set("split.count", cmd.getOptionValue("c"));
        createPresplitTable(tableName, splitAlgo, cmd.getOptionValue("f").split(":"), conf);
    }
    if (rollingSplit) {
        if (cmd.hasOption("o")) {
            conf.set("split.outstanding", cmd.getOptionValue("o"));
        }
        rollingSplit(tableName, splitAlgo, conf);
    }
}
Also used : HelpFormatter(org.apache.hbase.thirdparty.org.apache.commons.cli.HelpFormatter) Options(org.apache.hbase.thirdparty.org.apache.commons.cli.Options) TableName(org.apache.hadoop.hbase.TableName) CommandLine(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine) Configuration(org.apache.hadoop.conf.Configuration) HBaseConfiguration(org.apache.hadoop.hbase.HBaseConfiguration) GnuParser(org.apache.hbase.thirdparty.org.apache.commons.cli.GnuParser) ParseException(org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException)

Example 12 with CommandLine

use of org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine in project hbase by apache.

the class AbstractHBaseTool method isHelpCommand.

private boolean isHelpCommand(String[] args) throws ParseException {
    Options helpOption = new Options().addOption(HELP_OPTION);
    // this parses the command line but doesn't throw an exception on unknown options
    CommandLine cl = new DefaultParser().parse(helpOption, args, true);
    return cl.getOptions().length != 0;
}
Also used : Options(org.apache.hbase.thirdparty.org.apache.commons.cli.Options) CommandLine(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine) DefaultParser(org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser)

Example 13 with CommandLine

use of org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine in project hbase by apache.

the class AbstractHBaseTool method run.

@Override
public int run(String[] args) throws IOException {
    cmdLineArgs = args;
    Objects.requireNonNull(conf, "Tool configuration is not initialized");
    CommandLine cmd;
    List<String> argsList = new ArrayList<>(args.length);
    for (String arg : args) {
        argsList.add(arg);
    }
    // For backward compatibility of args which can't be parsed as Option. See javadoc for
    // processOldArgs(..)
    processOldArgs(argsList);
    try {
        addOptions();
        if (isHelpCommand(args)) {
            printUsage();
            return EXIT_SUCCESS;
        }
        String[] remainingArgs = new String[argsList.size()];
        argsList.toArray(remainingArgs);
        cmd = newParser().parse(options, remainingArgs);
    } catch (MissingOptionException e) {
        LOG.error(e.getMessage());
        LOG.error("Use -h or --help for usage instructions.");
        return EXIT_FAILURE;
    } catch (ParseException e) {
        LOG.error("Error when parsing command-line arguments", e);
        LOG.error("Use -h or --help for usage instructions.");
        return EXIT_FAILURE;
    }
    processOptions(cmd);
    int ret;
    try {
        ret = doWork();
    } catch (Exception e) {
        LOG.error("Error running command-line tool", e);
        return EXIT_FAILURE;
    }
    return ret;
}
Also used : CommandLine(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine) ArrayList(java.util.ArrayList) ParseException(org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException) MissingOptionException(org.apache.hbase.thirdparty.org.apache.commons.cli.MissingOptionException) IOException(java.io.IOException) MissingOptionException(org.apache.hbase.thirdparty.org.apache.commons.cli.MissingOptionException) ParseException(org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException)

Example 14 with CommandLine

use of org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine in project hbase by apache.

the class LoadTestTool method newParser.

@Override
protected CommandLineParser newParser() {
    // Validate in parse() to get helpful error messages instead of exploding in processOptions()
    return new DefaultParser() {

        @Override
        public CommandLine parse(Options opts, String[] args, Properties props, boolean stop) throws ParseException {
            CommandLine cl = super.parse(opts, args, props, stop);
            boolean isReadWriteUpdate = cmd.hasOption(OPT_READ) || cmd.hasOption(OPT_WRITE) || cmd.hasOption(OPT_UPDATE);
            boolean isInitOnly = cmd.hasOption(OPT_INIT_ONLY);
            if (!isInitOnly && !isReadWriteUpdate) {
                throw new MissingOptionException("Must specify either -" + OPT_INIT_ONLY + " or at least one of -" + OPT_READ + ", -" + OPT_WRITE + ", -" + OPT_UPDATE);
            }
            if (isInitOnly && isReadWriteUpdate) {
                throw new AlreadySelectedException(OPT_INIT_ONLY + " cannot be specified with any of -" + OPT_READ + ", -" + OPT_WRITE + ", -" + OPT_UPDATE);
            }
            if (isReadWriteUpdate && !cmd.hasOption(OPT_NUM_KEYS)) {
                throw new MissingOptionException(OPT_NUM_KEYS + " must be specified in read/write mode.");
            }
            return cl;
        }
    };
}
Also used : Options(org.apache.hbase.thirdparty.org.apache.commons.cli.Options) CommandLine(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine) AlreadySelectedException(org.apache.hbase.thirdparty.org.apache.commons.cli.AlreadySelectedException) Properties(java.util.Properties) MissingOptionException(org.apache.hbase.thirdparty.org.apache.commons.cli.MissingOptionException) DefaultParser(org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser)

Example 15 with CommandLine

use of org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine in project hbase by apache.

the class RESTServer method parseCommandLine.

private static void parseCommandLine(String[] args, Configuration conf) {
    Options options = new Options();
    options.addOption("p", "port", true, "Port to bind to [default: " + DEFAULT_LISTEN_PORT + "]");
    options.addOption("ro", "readonly", false, "Respond only to GET HTTP " + "method requests [default: false]");
    options.addOption("i", "infoport", true, "Port for WEB UI");
    CommandLine commandLine = null;
    try {
        commandLine = new PosixParser().parse(options, args);
    } catch (ParseException e) {
        LOG.error("Could not parse: ", e);
        printUsageAndExit(options, -1);
    }
    // check for user-defined port setting, if so override the conf
    if (commandLine != null && commandLine.hasOption("port")) {
        String val = commandLine.getOptionValue("port");
        conf.setInt("hbase.rest.port", Integer.parseInt(val));
        if (LOG.isDebugEnabled()) {
            LOG.debug("port set to " + val);
        }
    }
    // check if server should only process GET requests, if so override the conf
    if (commandLine != null && commandLine.hasOption("readonly")) {
        conf.setBoolean("hbase.rest.readonly", true);
        if (LOG.isDebugEnabled()) {
            LOG.debug("readonly set to true");
        }
    }
    // check for user-defined info server port setting, if so override the conf
    if (commandLine != null && commandLine.hasOption("infoport")) {
        String val = commandLine.getOptionValue("infoport");
        conf.setInt("hbase.rest.info.port", Integer.parseInt(val));
        if (LOG.isDebugEnabled()) {
            LOG.debug("WEB UI port set to " + val);
        }
    }
    if (commandLine != null && commandLine.hasOption("skipLogin")) {
        conf.setBoolean(SKIP_LOGIN_KEY, true);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Skipping Kerberos login for REST server");
        }
    }
    List<String> remainingArgs = commandLine != null ? commandLine.getArgList() : new ArrayList<>();
    if (remainingArgs.size() != 1) {
        printUsageAndExit(options, 1);
    }
    String command = remainingArgs.get(0);
    if ("start".equals(command)) {
    // continue and start container
    } else if ("stop".equals(command)) {
        System.exit(1);
    } else {
        printUsageAndExit(options, 1);
    }
}
Also used : Options(org.apache.hbase.thirdparty.org.apache.commons.cli.Options) CommandLine(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine) PosixParser(org.apache.hbase.thirdparty.org.apache.commons.cli.PosixParser) ParseException(org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException)

Aggregations

CommandLine (org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine)21 Options (org.apache.hbase.thirdparty.org.apache.commons.cli.Options)14 ParseException (org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException)12 CommandLineParser (org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLineParser)9 DefaultParser (org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser)9 Configuration (org.apache.hadoop.conf.Configuration)8 HBaseConfiguration (org.apache.hadoop.hbase.HBaseConfiguration)8 HelpFormatter (org.apache.hbase.thirdparty.org.apache.commons.cli.HelpFormatter)8 PosixParser (org.apache.hbase.thirdparty.org.apache.commons.cli.PosixParser)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Path (org.apache.hadoop.fs.Path)4 GnuParser (org.apache.hbase.thirdparty.org.apache.commons.cli.GnuParser)4 List (java.util.List)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 TableName (org.apache.hadoop.hbase.TableName)2 MissingOptionException (org.apache.hbase.thirdparty.org.apache.commons.cli.MissingOptionException)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1