Search in sources :

Example 1 with CommandLine

use of org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine 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 CommandLine

use of org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine 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 CommandLine

use of org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine 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 CommandLine

use of org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine 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)

Example 5 with CommandLine

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

the class DataBlockEncodingTool method main.

/**
 * A command line interface to benchmarks. Parses command-line arguments and
 * runs the appropriate benchmarks.
 * @param args Should have length at least 1 and holds the file path to HFile.
 * @throws IOException If you specified the wrong file.
 */
public static void main(final String[] args) throws IOException {
    // set up user arguments
    Options options = new Options();
    options.addOption(OPT_HFILE_NAME, true, "HFile to analyse (REQUIRED)");
    options.getOption(OPT_HFILE_NAME).setArgName("FILENAME");
    options.addOption(OPT_KV_LIMIT, true, "Maximum number of KeyValues to process. A benchmark stops running " + "after iterating over this many KV pairs.");
    options.getOption(OPT_KV_LIMIT).setArgName("NUMBER");
    options.addOption(OPT_MEASURE_THROUGHPUT, false, "Measure read throughput");
    options.addOption(OPT_OMIT_CORRECTNESS_TEST, false, "Omit corectness tests.");
    options.addOption(OPT_COMPRESSION_ALGORITHM, true, "What kind of compression algorithm use for comparison.");
    options.addOption(OPT_BENCHMARK_N_TIMES, true, "Number of times to run each benchmark. Default value: " + DEFAULT_BENCHMARK_N_TIMES);
    options.addOption(OPT_BENCHMARK_N_OMIT, true, "Number of first runs of every benchmark to exclude from " + "statistics (" + DEFAULT_BENCHMARK_N_OMIT + " by default, so that " + "only the last " + (DEFAULT_BENCHMARK_N_TIMES - DEFAULT_BENCHMARK_N_OMIT) + " times are included in statistics.)");
    // parse arguments
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println("Could not parse arguments!");
        System.exit(-1);
        // avoid warning
        return;
    }
    int kvLimit = Integer.MAX_VALUE;
    if (cmd.hasOption(OPT_KV_LIMIT)) {
        kvLimit = Integer.parseInt(cmd.getOptionValue(OPT_KV_LIMIT));
        if (kvLimit <= 0) {
            LOG.error("KV_LIMIT should not less than 1.");
        }
    }
    // basic argument sanity checks
    if (!cmd.hasOption(OPT_HFILE_NAME)) {
        LOG.error("Please specify HFile name using the " + OPT_HFILE_NAME + " option");
        printUsage(options);
        System.exit(-1);
    }
    String pathName = cmd.getOptionValue(OPT_HFILE_NAME);
    String compressionName = DEFAULT_COMPRESSION.getName();
    if (cmd.hasOption(OPT_COMPRESSION_ALGORITHM)) {
        compressionName = cmd.getOptionValue(OPT_COMPRESSION_ALGORITHM).toLowerCase(Locale.ROOT);
    }
    boolean doBenchmark = cmd.hasOption(OPT_MEASURE_THROUGHPUT);
    boolean doVerify = !cmd.hasOption(OPT_OMIT_CORRECTNESS_TEST);
    if (cmd.hasOption(OPT_BENCHMARK_N_TIMES)) {
        benchmarkNTimes = Integer.valueOf(cmd.getOptionValue(OPT_BENCHMARK_N_TIMES));
    }
    if (cmd.hasOption(OPT_BENCHMARK_N_OMIT)) {
        benchmarkNOmit = Integer.valueOf(cmd.getOptionValue(OPT_BENCHMARK_N_OMIT));
    }
    if (benchmarkNTimes < benchmarkNOmit) {
        LOG.error("The number of times to run each benchmark (" + benchmarkNTimes + ") must be greater than the number of benchmark runs to exclude " + "from statistics (" + benchmarkNOmit + ")");
        System.exit(1);
    }
    LOG.info("Running benchmark " + benchmarkNTimes + " times. " + "Excluding the first " + benchmarkNOmit + " times from statistics.");
    final Configuration conf = HBaseConfiguration.create();
    testCodecs(conf, kvLimit, pathName, compressionName, doBenchmark, doVerify);
}
Also used : 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) CommandLineParser(org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLineParser) 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