Search in sources :

Example 1 with Options

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

the class ProcedureWALPrettyPrinter 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.
 */
@Override
public int run(final String[] args) throws IOException {
    // create options
    Options options = new Options();
    options.addOption("h", "help", false, "Output help message");
    options.addOption("f", "file", true, "File to print");
    final List<Path> files = new ArrayList<>();
    try {
        CommandLine cmd = new DefaultParser().parse(options, args);
        if (cmd.hasOption("f")) {
            files.add(new Path(cmd.getOptionValue("f")));
        }
        if (files.isEmpty() || cmd.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("ProcedureWALPrettyPrinter ", options, true);
            return (-1);
        }
    } catch (ParseException e) {
        LOG.error("Failed to parse commandLine arguments", e);
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("ProcedureWALPrettyPrinter ", options, true);
        return (-1);
    }
    // get configuration, file system, and process the given files
    for (Path file : files) {
        processFile(getConf(), file);
    }
    return (0);
}
Also used : Path(org.apache.hadoop.fs.Path) HelpFormatter(org.apache.hbase.thirdparty.org.apache.commons.cli.HelpFormatter) Options(org.apache.hbase.thirdparty.org.apache.commons.cli.Options) CommandLine(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine) ArrayList(java.util.ArrayList) ParseException(org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException) DefaultParser(org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser)

Example 2 with Options

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

the class HMasterCommandLine method run.

@Override
public int run(String[] args) throws Exception {
    boolean shutDownCluster = false;
    Options opt = new Options();
    opt.addOption("localRegionServers", true, "RegionServers to start in master process when running standalone");
    opt.addOption("masters", true, "Masters to start in this process");
    opt.addOption("minRegionServers", true, "Minimum RegionServers needed to host user tables");
    opt.addOption("backup", false, "Do not try to become HMaster until the primary fails");
    opt.addOption("shutDownCluster", false, "`hbase master stop --shutDownCluster` shuts down cluster");
    CommandLine cmd;
    try {
        cmd = new GnuParser().parse(opt, args);
    } catch (ParseException e) {
        LOG.error("Could not parse: ", e);
        usage(null);
        return 1;
    }
    if (cmd.hasOption("minRegionServers")) {
        String val = cmd.getOptionValue("minRegionServers");
        getConf().setInt("hbase.regions.server.count.min", Integer.parseInt(val));
        LOG.debug("minRegionServers set to " + val);
    }
    // minRegionServers used to be minServers.  Support it too.
    if (cmd.hasOption("minServers")) {
        String val = cmd.getOptionValue("minServers");
        getConf().setInt("hbase.regions.server.count.min", Integer.parseInt(val));
        LOG.debug("minServers set to " + val);
    }
    // check if we are the backup master - override the conf if so
    if (cmd.hasOption("backup")) {
        getConf().setBoolean(HConstants.MASTER_TYPE_BACKUP, true);
    }
    // master when we are in local/standalone mode. Useful testing)
    if (cmd.hasOption("localRegionServers")) {
        String val = cmd.getOptionValue("localRegionServers");
        getConf().setInt("hbase.regionservers", Integer.parseInt(val));
        LOG.debug("localRegionServers set to " + val);
    }
    // How many masters to startup inside this process; useful testing
    if (cmd.hasOption("masters")) {
        String val = cmd.getOptionValue("masters");
        getConf().setInt("hbase.masters", Integer.parseInt(val));
        LOG.debug("masters set to " + val);
    }
    // Checking whether to shut down cluster or not
    if (cmd.hasOption("shutDownCluster")) {
        shutDownCluster = true;
    }
    @SuppressWarnings("unchecked") List<String> remainingArgs = cmd.getArgList();
    if (remainingArgs.size() != 1) {
        usage(null);
        return 1;
    }
    String command = remainingArgs.get(0);
    if ("start".equals(command)) {
        return startMaster();
    } else if ("stop".equals(command)) {
        if (shutDownCluster) {
            return stopMaster();
        }
        System.err.println("To shutdown the master run " + "hbase-daemon.sh stop master or send a kill signal to " + "the HMaster pid, " + "and to stop HBase Cluster run \"stop-hbase.sh\" or \"hbase master stop --shutDownCluster\"");
        return 1;
    } else if ("clear".equals(command)) {
        return (ZNodeClearer.clear(getConf()) ? 0 : 1);
    } else {
        usage("Invalid command: " + command);
        return 1;
    }
}
Also used : Options(org.apache.hbase.thirdparty.org.apache.commons.cli.Options) CommandLine(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine) ServerCommandLine(org.apache.hadoop.hbase.util.ServerCommandLine) GnuParser(org.apache.hbase.thirdparty.org.apache.commons.cli.GnuParser) ParseException(org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException)

Example 3 with Options

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

the class HBTop method run.

@Override
public int run(String[] args) throws Exception {
    long initialRefreshDelay = 3 * 1000;
    Mode initialMode = Mode.REGION;
    List<Field> initialFields = null;
    Field initialSortField = null;
    Boolean initialAscendingSort = null;
    List<RecordFilter> initialFilters = null;
    long numberOfIterations = Long.MAX_VALUE;
    boolean batchMode = false;
    try {
        Options opts = getOptions();
        CommandLine commandLine = new DefaultParser().parse(opts, args);
        if (commandLine.hasOption("help")) {
            printUsage(opts);
            return 0;
        }
        if (commandLine.hasOption("mode")) {
            String mode = commandLine.getOptionValue("mode");
            switch(mode) {
                case "n":
                    initialMode = Mode.NAMESPACE;
                    break;
                case "t":
                    initialMode = Mode.TABLE;
                    break;
                case "r":
                    initialMode = Mode.REGION;
                    break;
                case "s":
                    initialMode = Mode.REGION_SERVER;
                    break;
                case "u":
                    initialMode = Mode.USER;
                    break;
                case "c":
                    initialMode = Mode.CLIENT;
                    break;
                default:
                    LOGGER.warn("Mode set invalid, using default");
                    break;
            }
        }
        if (commandLine.hasOption("outputFieldNames")) {
            initialMode.getFieldInfos().forEach(f -> System.out.println(f.getField().getHeader()));
            return 0;
        }
        if (commandLine.hasOption("delay")) {
            int delay = 0;
            try {
                delay = Integer.parseInt(commandLine.getOptionValue("delay"));
            } catch (NumberFormatException ignored) {
            }
            if (delay < 1) {
                LOGGER.warn("Delay set too low or invalid, using default");
            } else {
                initialRefreshDelay = delay * 1000L;
            }
        }
        if (commandLine.hasOption("numberOfIterations")) {
            try {
                numberOfIterations = Long.parseLong(commandLine.getOptionValue("numberOfIterations"));
            } catch (NumberFormatException ignored) {
                LOGGER.warn("The number of iterations set invalid, ignoring");
            }
        }
        if (commandLine.hasOption("sortField")) {
            String sortField = commandLine.getOptionValue("sortField");
            String field;
            boolean ascendingSort;
            if (sortField.startsWith("+")) {
                field = sortField.substring(1);
                ascendingSort = false;
            } else if (sortField.startsWith("-")) {
                field = sortField.substring(1);
                ascendingSort = true;
            } else {
                field = sortField;
                ascendingSort = false;
            }
            Optional<FieldInfo> fieldInfo = initialMode.getFieldInfos().stream().filter(f -> f.getField().getHeader().equals(field)).findFirst();
            if (fieldInfo.isPresent()) {
                initialSortField = fieldInfo.get().getField();
                initialAscendingSort = ascendingSort;
            } else {
                LOGGER.warn("The specified sort field " + field + " is not found, using default");
            }
        }
        if (commandLine.hasOption("fields")) {
            String[] fields = commandLine.getOptionValue("fields").split(",");
            initialFields = new ArrayList<>();
            for (String field : fields) {
                Optional<FieldInfo> fieldInfo = initialMode.getFieldInfos().stream().filter(f -> f.getField().getHeader().equals(field)).findFirst();
                if (fieldInfo.isPresent()) {
                    initialFields.add(fieldInfo.get().getField());
                } else {
                    LOGGER.warn("The specified field " + field + " is not found, ignoring");
                }
            }
        }
        if (commandLine.hasOption("filters")) {
            String[] filters = commandLine.getOptionValue("filters").split(",");
            List<Field> fields = initialMode.getFieldInfos().stream().map(FieldInfo::getField).collect(Collectors.toList());
            for (String filter : filters) {
                RecordFilter f = RecordFilter.parse(filter, fields, false);
                if (f != null) {
                    if (initialFilters == null) {
                        initialFilters = new ArrayList<>();
                    }
                    initialFilters.add(f);
                } else {
                    LOGGER.warn("The specified filter " + filter + " is invalid, ignoring");
                }
            }
        }
        if (commandLine.hasOption("batchMode")) {
            batchMode = true;
        }
    } catch (Exception e) {
        LOGGER.error("Unable to parse options", e);
        return 1;
    }
    try (Screen screen = new Screen(getConf(), initialRefreshDelay, initialMode, initialFields, initialSortField, initialAscendingSort, initialFilters, numberOfIterations, batchMode)) {
        screen.run();
    }
    return 0;
}
Also used : Logger(org.slf4j.Logger) Options(org.apache.hbase.thirdparty.org.apache.commons.cli.Options) ToolRunner(org.apache.hadoop.util.ToolRunner) LoggerFactory(org.slf4j.LoggerFactory) HBaseInterfaceAudience(org.apache.hadoop.hbase.HBaseInterfaceAudience) HelpFormatter(org.apache.hbase.thirdparty.org.apache.commons.cli.HelpFormatter) Collectors(java.util.stream.Collectors) Tool(org.apache.hadoop.util.Tool) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Screen(org.apache.hadoop.hbase.hbtop.screen.Screen) List(java.util.List) InterfaceAudience(org.apache.yetus.audience.InterfaceAudience) Field(org.apache.hadoop.hbase.hbtop.field.Field) FieldInfo(org.apache.hadoop.hbase.hbtop.field.FieldInfo) HBaseConfiguration(org.apache.hadoop.hbase.HBaseConfiguration) Mode(org.apache.hadoop.hbase.hbtop.mode.Mode) DefaultParser(org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser) Configured(org.apache.hadoop.conf.Configured) Configuration(org.apache.hadoop.conf.Configuration) Optional(java.util.Optional) CommandLine(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine) Options(org.apache.hbase.thirdparty.org.apache.commons.cli.Options) Screen(org.apache.hadoop.hbase.hbtop.screen.Screen) Mode(org.apache.hadoop.hbase.hbtop.mode.Mode) Field(org.apache.hadoop.hbase.hbtop.field.Field) CommandLine(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine) FieldInfo(org.apache.hadoop.hbase.hbtop.field.FieldInfo) DefaultParser(org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser)

Example 4 with Options

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

the class HBTop method getOptions.

private Options getOptions() {
    Options opts = new Options();
    opts.addOption("h", "help", false, "Print usage; for help while the tool is running press 'h'");
    opts.addOption("d", "delay", true, "The refresh delay (in seconds); default is 3 seconds");
    opts.addOption("m", "mode", true, "The mode; n (Namespace)|t (Table)|r (Region)|s (RegionServer)|u (User)" + "|c (Client), default is r");
    opts.addOption("n", "numberOfIterations", true, "The number of iterations");
    opts.addOption("s", "sortField", true, "The initial sort field. You can prepend a `+' or `-' to the field name to also override" + " the sort direction. A leading `+' will force sorting high to low, whereas a `-' will" + " ensure a low to high ordering");
    opts.addOption("O", "outputFieldNames", false, "Print each of the available field names on a separate line, then quit");
    opts.addOption("f", "fields", true, "Show only the given fields. Specify comma separated fields to show multiple fields");
    opts.addOption("i", "filters", true, "The initial filters. Specify comma separated filters to set multiple filters");
    opts.addOption("b", "batchMode", false, "Starts hbtop in Batch mode, which could be useful for sending output from hbtop to other" + " programs or to a file. In this mode, hbtop will not accept input and runs until the" + " iterations limit you've set with the `-n' command-line option or until killed");
    return opts;
}
Also used : Options(org.apache.hbase.thirdparty.org.apache.commons.cli.Options)

Example 5 with Options

use of org.apache.hbase.thirdparty.org.apache.commons.cli.Options 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("t", "tables", true, "Table names (comma separated) to filter by; eg: test1,test2,test3 ");
    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("k", "outputOnlyRowKey", false, "Print only row keys");
    options.addOption("w", "row", true, "Row to filter by. Pass row name.");
    options.addOption("f", "rowPrefix", true, "Row prefix to filter by.");
    options.addOption("g", "goto", true, "Position to seek to in the file");
    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("k")) {
            printer.setOutputOnlyRowKey();
        }
        if (cmd.hasOption("t")) {
            printer.setTableFilter(cmd.getOptionValue("t"));
        }
        if (cmd.hasOption("r")) {
            printer.setRegionFilter(cmd.getOptionValue("r"));
        }
        if (cmd.hasOption("s")) {
            printer.setSequenceFilter(Long.parseLong(cmd.getOptionValue("s")));
        }
        if (cmd.hasOption("w")) {
            if (cmd.hasOption("f")) {
                throw new ParseException("Row and Row-prefix cannot be supplied together");
            }
            printer.setRowFilter(cmd.getOptionValue("w"));
        }
        if (cmd.hasOption("f")) {
            if (cmd.hasOption("w")) {
                throw new ParseException("Row and Row-prefix cannot be supplied together");
            }
            printer.setRowPrefixFilter(cmd.getOptionValue("f"));
        }
        if (cmd.hasOption("g")) {
            printer.setPosition(Long.parseLong(cmd.getOptionValue("g")));
        }
    } catch (ParseException e) {
        LOG.error("Failed to parse commandLine arguments", e);
        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();
    CommonFSUtils.setFsDefault(conf, CommonFSUtils.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.hbase.thirdparty.org.apache.commons.cli.HelpFormatter) Path(org.apache.hadoop.fs.Path) Options(org.apache.hbase.thirdparty.org.apache.commons.cli.Options) CommandLine(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine) Configuration(org.apache.hadoop.conf.Configuration) HBaseConfiguration(org.apache.hadoop.hbase.HBaseConfiguration) PosixParser(org.apache.hbase.thirdparty.org.apache.commons.cli.PosixParser) FileSystem(org.apache.hadoop.fs.FileSystem) CommandLineParser(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLineParser) ParseException(org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException)

Aggregations

Options (org.apache.hbase.thirdparty.org.apache.commons.cli.Options)19 CommandLine (org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine)14 ParseException (org.apache.hbase.thirdparty.org.apache.commons.cli.ParseException)10 DefaultParser (org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser)8 Configuration (org.apache.hadoop.conf.Configuration)7 HBaseConfiguration (org.apache.hadoop.hbase.HBaseConfiguration)7 CommandLineParser (org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLineParser)6 HelpFormatter (org.apache.hbase.thirdparty.org.apache.commons.cli.HelpFormatter)4 ArrayList (java.util.ArrayList)3 GnuParser (org.apache.hbase.thirdparty.org.apache.commons.cli.GnuParser)3 PosixParser (org.apache.hbase.thirdparty.org.apache.commons.cli.PosixParser)3 List (java.util.List)2 Path (org.apache.hadoop.fs.Path)2 TableName (org.apache.hadoop.hbase.TableName)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1