Java Code Examples for org.apache.commons.cli.CommandLine#getOptionValues()

The following examples show how to use org.apache.commons.cli.CommandLine#getOptionValues() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestTajoCli.java    From tajo with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseConf() throws Exception {
  String[] args = new String[]{"--conf", "tajo.cli.print.pause=false",
    "--conf", "tajo.executor.join.inner.in-memory-table-num=256"};

  CommandLineParser parser = new PosixParser();
  CommandLine cmd = parser.parse(TajoCli.options, args);
  String[] confValues = cmd.getOptionValues("conf");

  assertNotNull(confValues);
  assertEquals(2, confValues.length);

  assertEquals("tajo.cli.print.pause=false", confValues[0]);
  assertEquals("tajo.executor.join.inner.in-memory-table-num=256", confValues[1]);

  TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration();
  try (TajoCli testCli = new TajoCli(tajoConf, args, null, System.in, System.out, err)) {
    assertEquals("false", testCli.getContext().get(SessionVars.CLI_PAGING_ENABLED));
    assertEquals("256", testCli.getContext().getConf().get("tajo.executor.join.inner.in-memory-table-num"));
  }
}
 
Example 2
Source File: LoggingTimestampConverterTool.java    From kieker with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean readPropertiesFromCommandLine(final CommandLine commandLine) {
	final String[] timestampsStr = commandLine
			.getOptionValues(LoggingTimestampConverterTool.FLAG_TIMESTAMPS_PARAMETER);
	if ((timestampsStr == null) || (timestampsStr.length == 0)) {
		LoggingTimestampConverterTool.LOGGER.error("No timestamp passed as argument.");
		return false;
	}

	this.timestampsLong = new long[timestampsStr.length];

	for (int curIdx = 0; curIdx < timestampsStr.length; curIdx++) {
		try {
			this.timestampsLong[curIdx] = Long.parseLong(timestampsStr[curIdx]);
		} catch (final NumberFormatException ex) {
			LoggingTimestampConverterTool.LOGGER.error("Failed to parse timestamp: {}", timestampsStr[curIdx], ex);
			return false;
		}
	}

	return true;
}
 
Example 3
Source File: ConvertCommand.java    From robot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Given an input state and command line arguments, save the ontology to a new format and return
 * the state unchanged.
 *
 * <p>Supported formats:
 *
 * <ul>
 *   <li>OBO .obo
 *   <li>RDFXML .owl
 *   <li>Turtle .ttl
 *   <li>OWLXML .owx
 *   <li>Manchester .omn
 *   <li>OWL Functional .ofn
 *   <li>OboGraphs JSON .json
 * </ul>
 *
 * @param state the state from the previous command, or null
 * @param args the command-line arguments
 * @return the input state unchanged, or a new state with the ontology
 * @throws Exception on any problem
 */
public CommandState execute(CommandState state, String[] args) throws Exception {
  CommandLine line = CommandLineHelper.getCommandLine(getUsage(), getOptions(), args);
  if (line == null) {
    return null;
  }

  if (state == null) {
    state = new CommandState();
  }

  IOHelper ioHelper = CommandLineHelper.getIOHelper(line);
  state = CommandLineHelper.updateInputOntology(ioHelper, state, line);
  OWLOntology ontology = state.getOntology();

  String[] outputs = line.getOptionValues("output");
  // Validate the output
  if (outputs == null || outputs.length == 0) {
    throw new IllegalArgumentException(missingOutputError);
  } else if (outputs.length > 1) {
    throw new IllegalArgumentException(multipleOutputsError);
  }

  CommandLineHelper.maybeSaveOutput(line, ontology);
  return state;
}
 
Example 4
Source File: BootstrapTools.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Parse the dynamic properties (passed on the command line).
 */
public static Configuration parseDynamicProperties(CommandLine cmd) {
	final Configuration config = new Configuration();

	String[] values = cmd.getOptionValues(DYNAMIC_PROPERTIES_OPT);
	if (values != null) {
		for (String value : values) {
			String[] pair = value.split("=", 2);
			if (pair.length == 1) {
				config.setString(pair[0], Boolean.TRUE.toString());
			}
			else if (pair.length == 2) {
				config.setString(pair[0], pair[1]);
			}
		}
	}

	return config;
}
 
Example 5
Source File: CheckTelnetHandler.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
public TelnetResponse telnet(Channel channel, String[] args) {
    String respMessage = "";
    if (args == null || args.length != 3) {
        respMessage = help();
    } else {
        CommandLine cmd = getCommand(options, args);
        if (cmd.hasOption(HELP_SHORT)) {
            respMessage = help();
        } else {
            String[] nameAndId;
            if (cmd.hasOption("i")) {
                nameAndId = cmd.getOptionValues("i");
            } else {
                nameAndId = new String[2];
                System.arraycopy(cmd.getArgs(), 1, nameAndId, 0, 2);
            }
            if (nameAndId.length == 2 && nameAndId[0] != null && nameAndId[1] != null) {
                String className = ServiceManager.getClassName(Long.valueOf(nameAndId[1]));
                respMessage = nameAndId[0].equals(className) ? "1" : "0";
            }
        }

    }
    return new TelnetResponse(respMessage);
}
 
Example 6
Source File: CliOptionsParser.java    From flink with Apache License 2.0 6 votes vote down vote up
private static List<URL> checkUrls(CommandLine line, Option option) {
	if (line.hasOption(option.getOpt())) {
		final String[] urls = line.getOptionValues(option.getOpt());
		return Arrays.stream(urls)
			.distinct()
			.map((url) -> {
				try {
					return Path.fromLocalFile(new File(url).getAbsoluteFile()).toUri().toURL();
				} catch (Exception e) {
					throw new SqlClientException("Invalid path for option '" + option.getLongOpt() + "': " + url, e);
				}
			})
			.collect(Collectors.toList());
	}
	return null;
}
 
Example 7
Source File: KeywordOptimizer.java    From keyword-optimizer with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the match types specified from the command line to the {@link SeedGenerator}.
 *
 * @param cmdLine the parsed command line parameters
 * @return the match types for creating the seed keywords
 */
private static Set<KeywordMatchType> getMatchTypes(CommandLine cmdLine)
    throws KeywordOptimizerException {
  Set<KeywordMatchType> matchTypes = new HashSet<>();
  for (String matchType : cmdLine.getOptionValues("m")) {
    KeywordMatchType mt = KeywordMatchType.fromString(matchType);

    log("Using match type: " + mt);
    matchTypes.add(mt);
  }
  return matchTypes;
}
 
Example 8
Source File: ConfigManager.java    From serve with Apache License 2.0 5 votes vote down vote up
public Arguments(CommandLine cmd) {
    tsConfigFile = cmd.getOptionValue("ts-config-file");
    pythonExecutable = cmd.getOptionValue("python");
    modelStore = cmd.getOptionValue("model-store");
    models = cmd.getOptionValues("models");
    snapshotDisabled = cmd.hasOption("no-config-snapshot");
}
 
Example 9
Source File: ConfigWrapper.java    From ecs-sync with Apache License 2.0 5 votes vote down vote up
public C parse(CommandLine commandLine, String prefix) {
    try {
        C object = getTargetClass().newInstance();
        BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(object);

        for (String name : propertyNames()) {
            ConfigPropertyWrapper propertyWrapper = getPropertyWrapper(name);
            if (!propertyWrapper.isCliOption()) continue;

            org.apache.commons.cli.Option option = propertyWrapper.getCliOption(prefix);

            if (commandLine.hasOption(option.getLongOpt())) {

                Object value = commandLine.getOptionValue(option.getLongOpt());
                if (propertyWrapper.getDescriptor().getPropertyType().isArray())
                    value = commandLine.getOptionValues(option.getLongOpt());

                if (Boolean.class == propertyWrapper.getDescriptor().getPropertyType()
                        || "boolean".equals(propertyWrapper.getDescriptor().getPropertyType().getName()))
                    value = Boolean.toString(!propertyWrapper.isCliInverted());

                beanWrapper.setPropertyValue(name, value);
            }
        }

        return object;
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: PythonProgramOptions.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected String[] extractProgramArgs(CommandLine line) {
	String[] args;
	if (isPythonEntryPoint(line)) {
		String[] rawArgs = line.hasOption(ARGS_OPTION.getOpt()) ?
			line.getOptionValues(ARGS_OPTION.getOpt()) :
			line.getArgs();
		// copy python related parameters to program args and place them in front of user parameters
		List<String> pyArgList = new ArrayList<>();
		Set<Option> pyOptions = new HashSet<>();
		pyOptions.add(PY_OPTION);
		pyOptions.add(PYMODULE_OPTION);
		for (Option option : line.getOptions()) {
			if (pyOptions.contains(option)) {
				pyArgList.add("--" + option.getLongOpt());
				pyArgList.add(option.getValue());
			}
		}
		String[] newArgs = pyArgList.toArray(new String[rawArgs.length + pyArgList.size()]);
		System.arraycopy(rawArgs, 0, newArgs, pyArgList.size(), rawArgs.length);
		args = newArgs;
	} else {
		args = super.extractProgramArgs(line);
	}

	return args;
}
 
Example 11
Source File: TlsToolkitStandaloneCommandLine.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private Supplier<String> parsePasswordSupplier(CommandLine commandLine, String option, Supplier<String> defaultSupplier) {
    if (commandLine.hasOption(option)) {
        String[] values = commandLine.getOptionValues(option);
        if (values.length == 1) {
            return PasswordUtil.passwordSupplier(values[0]);
        } else {
            return PasswordUtil.passwordSupplier("Provided " + option + " exhausted, please don't specify " + option
                    + ", specify one value to be used for all NiFi instances, or specify one value for each NiFi instance.", values);
        }
    } else {
        return defaultSupplier;
    }
}
 
Example 12
Source File: OptionsParser.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public OptionsParser(String... args) {
	Options options = new Options();

	options.addOption("plugins", true, "Directory from which to load a plugin bundle, can be used multiple times for different plugin bundles");
	options.addOption("home", true, "The home directory to use, default is [cwd]/home");
	
	CommandLineParser parser = new DefaultParser();
	pluginDirectories = null;
	try {
		CommandLine cmd = parser.parse(options, args);
		if (cmd.hasOption("plugins")) {
			String[] plugins = cmd.getOptionValues("plugins");
			pluginDirectories = new Path[plugins.length];
			for (int i=0; i<plugins.length; i++) {
				pluginDirectories[i] = Paths.get(plugins[i]);
				if (!Files.isDirectory(pluginDirectories[i])) {
					throw new RuntimeException("plugins parameter must point to a directory (" + pluginDirectories[i] + ")");
				}
			}
		}
		if (cmd.hasOption("home")) {
			home = Paths.get(cmd.getOptionValue("home"));
		}
	} catch (ParseException e) {
		e.printStackTrace();
	}
}
 
Example 13
Source File: CliFrontend.java    From stratosphere with Apache License 2.0 4 votes vote down vote up
/**
 * @param line
 * 
 * @return Either a PackagedProgram (upon success), or null;
 */
protected PackagedProgram buildProgram(CommandLine line) {
	String[] programArgs = line.hasOption(ARGS_OPTION.getOpt()) ?
			line.getOptionValues(ARGS_OPTION.getOpt()) :
			line.getArgs();

	// take the jar file from the option, or as the first trailing parameter (if available)
	String jarFilePath = null;
	if (line.hasOption(JAR_OPTION.getOpt())) {
		jarFilePath = line.getOptionValue(JAR_OPTION.getOpt());
	}
	else if (programArgs.length > 0) {
		jarFilePath = programArgs[0];
		programArgs = Arrays.copyOfRange(programArgs, 1, programArgs.length);
	}
	else {
		System.out.println("Error: Jar file is not set.");
		return null;
	}
	
	File jarFile = new File(jarFilePath);
	
	// Check if JAR file exists
	if (!jarFile.exists()) {
		System.out.println("Error: Jar file does not exist.");
		return null;
	}
	else if (!jarFile.isFile()) {
		System.out.println("Error: Jar file is not a file.");
		return null;
	}
	
	// Get assembler class
	String entryPointClass = line.hasOption(CLASS_OPTION.getOpt()) ?
			line.getOptionValue(CLASS_OPTION.getOpt()) :
			null;
			
	try {
		return entryPointClass == null ? 
				new PackagedProgram(jarFile, programArgs) :
				new PackagedProgram(jarFile, entryPointClass, programArgs);
	} catch (ProgramInvocationException e) {
		handleError(e);
		return null;
	}
}
 
Example 14
Source File: GenericOptionsParser.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * Modify configuration according user-specified generic options
 * @param conf Configuration to be modified
 * @param line User-specified generic options
 */
private void processGeneralOptions(Configuration conf,
    CommandLine line) throws IOException {
  if (line.hasOption("fs")) {
    FileSystem.setDefaultUri(conf, line.getOptionValue("fs"));
  }

  if (line.hasOption("jt")) {
    String optionValue = line.getOptionValue("jt");
    if (optionValue.equalsIgnoreCase("local")) {
      conf.set("mapreduce.framework.name", optionValue);
    }

    conf.set("yarn.resourcemanager.address", optionValue, 
        "from -jt command line option");
  }
  if (line.hasOption("conf")) {
    String[] values = line.getOptionValues("conf");
    for(String value : values) {
      conf.addResource(new Path(value));
    }
  }

  if (line.hasOption('D')) {
    String[] property = line.getOptionValues('D');
    for(String prop : property) {
      String[] keyval = prop.split("=", 2);
      if (keyval.length == 2) {
        conf.set(keyval[0], keyval[1], "from command line");
      }
    }
  }

  if (line.hasOption("libjars")) {
    conf.set("tmpjars", 
             validateFiles(line.getOptionValue("libjars"), conf),
             "from -libjars command line option");
    //setting libjars in client classpath
    URL[] libjars = getLibJars(conf);
    if(libjars!=null && libjars.length>0) {
      conf.setClassLoader(new URLClassLoader(libjars, conf.getClassLoader()));
      Thread.currentThread().setContextClassLoader(
          new URLClassLoader(libjars, 
              Thread.currentThread().getContextClassLoader()));
    }
  }
  if (line.hasOption("files")) {
    conf.set("tmpfiles", 
             validateFiles(line.getOptionValue("files"), conf),
             "from -files command line option");
  }
  if (line.hasOption("archives")) {
    conf.set("tmparchives", 
              validateFiles(line.getOptionValue("archives"), conf),
              "from -archives command line option");
  }
  conf.setBoolean("mapreduce.client.genericoptionsparser.used", true);
  
  // tokensFile
  if(line.hasOption("tokenCacheFile")) {
    String fileName = line.getOptionValue("tokenCacheFile");
    // check if the local file exists
    FileSystem localFs = FileSystem.getLocal(conf);
    Path p = localFs.makeQualified(new Path(fileName));
    if (!localFs.exists(p)) {
        throw new FileNotFoundException("File "+fileName+" does not exist.");
    }
    if(LOG.isDebugEnabled()) {
      LOG.debug("setting conf tokensFile: " + fileName);
    }
    UserGroupInformation.getCurrentUser().addCredentials(
        Credentials.readTokenStorageFile(p, conf));
    conf.set("mapreduce.job.credentials.binary", p.toString(),
             "from -tokenCacheFile command line option");

  }
}
 
Example 15
Source File: TlsToolkitStandaloneCommandLine.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected CommandLine doParse(String... args) throws CommandLineParseException {
    CommandLine commandLine = super.doParse(args);
    String outputDirectory = commandLine.getOptionValue(OUTPUT_DIRECTORY_ARG, DEFAULT_OUTPUT_DIRECTORY);
    baseDir = new File(outputDirectory);

    dnPrefix = commandLine.getOptionValue(NIFI_DN_PREFIX_ARG, TlsConfig.DEFAULT_DN_PREFIX);
    dnSuffix = commandLine.getOptionValue(NIFI_DN_SUFFIX_ARG, TlsConfig.DEFAULT_DN_SUFFIX);
    domainAlternativeNames = commandLine.getOptionValue(SUBJECT_ALTERNATIVE_NAMES);

    Stream<String> globalOrderExpressions = null;
    if (commandLine.hasOption(GLOBAL_PORT_SEQUENCE_ARG)) {
        globalOrderExpressions = Arrays.stream(commandLine.getOptionValues(GLOBAL_PORT_SEQUENCE_ARG)).flatMap(s -> Arrays.stream(s.split(","))).map(String::trim);
    }

    if (commandLine.hasOption(HOSTNAMES_ARG)) {
        instanceDefinitions = Collections.unmodifiableList(
                InstanceDefinition.createDefinitions(globalOrderExpressions,
                        Arrays.stream(commandLine.getOptionValues(HOSTNAMES_ARG)).flatMap(s -> Arrays.stream(s.split(",")).map(String::trim)),
                        parsePasswordSupplier(commandLine, KEY_STORE_PASSWORD_ARG, passwordUtil.passwordSupplier()),
                        parsePasswordSupplier(commandLine, KEY_PASSWORD_ARG, commandLine.hasOption(DIFFERENT_KEY_AND_KEYSTORE_PASSWORDS_ARG) ? passwordUtil.passwordSupplier() : null),
                        parsePasswordSupplier(commandLine, TRUST_STORE_PASSWORD_ARG, passwordUtil.passwordSupplier())));
    } else {
        instanceDefinitions = Collections.emptyList();
    }

    String[] clientDnValues = commandLine.getOptionValues(CLIENT_CERT_DN_ARG);
    if (clientDnValues != null) {
        clientDns = Collections.unmodifiableList(Arrays.stream(clientDnValues).collect(Collectors.toList()));
    } else {
        clientDns = Collections.emptyList();
    }

    clientPasswords = Collections.unmodifiableList(getPasswords(CLIENT_CERT_PASSWORD_ARG, commandLine, clientDns.size(), CLIENT_CERT_DN_ARG));
    clientPasswordsGenerated = commandLine.getOptionValues(CLIENT_CERT_PASSWORD_ARG) == null;
    overwrite = commandLine.hasOption(OVERWRITE_ARG);

    String nifiPropertiesFile = commandLine.getOptionValue(NIFI_PROPERTIES_FILE_ARG, "");
    try {
        if (StringUtils.isEmpty(nifiPropertiesFile)) {
            logger.info("No " + NIFI_PROPERTIES_FILE_ARG + " specified, using embedded one.");
            niFiPropertiesWriterFactory = new NiFiPropertiesWriterFactory();
        } else {
            logger.info("Using " + nifiPropertiesFile + " as template.");
            niFiPropertiesWriterFactory = new NiFiPropertiesWriterFactory(new FileInputStream(nifiPropertiesFile));
        }
    } catch (IOException e) {
        printUsageAndThrow("Unable to read nifi.properties from " + (StringUtils.isEmpty(nifiPropertiesFile) ? "classpath" : nifiPropertiesFile), ExitCode.ERROR_READING_NIFI_PROPERTIES);
    }
    return commandLine;
}
 
Example 16
Source File: Main.java    From swift-t with Apache License 2.0 4 votes vote down vote up
private static Args processArgs(String[] args) {
  Options opts = initOptions();

  CommandLine cmd = null;
  try {
    CommandLineParser parser = new GnuParser();
    cmd = parser.parse(opts, args);
  } catch (ParseException ex) {
    // Use Apache CLI-provided messages
    System.err.println(ex.getMessage());
    usage(opts);
    System.exit(1);
    return null;
  }

  if (cmd.hasOption(INCLUDE_FLAG)) {
    for (String dir: cmd.getOptionValues(INCLUDE_FLAG)) {
      Settings.addModulePath(dir);
    }
  }

  Properties swiftProgramArgs = cmd.getOptionProperties(SWIFT_PROG_ARG_FLAG);

  String preprocMacros[];
  if (cmd.hasOption(PREPROC_MACRO_FLAG)) {
    preprocMacros = cmd.getOptionValues(PREPROC_MACRO_FLAG);
  } else {
    preprocMacros = new String[0];
  }

  String[] remainingArgs = cmd.getArgs();
  if (remainingArgs.length < 1 || remainingArgs.length > 2) {
    System.out.println("Expected input file and optional output file, but got "
            + remainingArgs.length + " arguments");
    usage(opts);
    System.exit(ExitCode.ERROR_COMMAND.code());
  }

  String input = remainingArgs[0];
  String output = null;
  if (remainingArgs.length == 2) {
    output = remainingArgs[1];
  }
  Args result = new Args(input, output, swiftProgramArgs,
      Arrays.asList(preprocMacros));
  recordArgValues(result);
  return result;
}
 
Example 17
Source File: ArgParser.java    From fcrepo-import-export with Apache License 2.0 4 votes vote down vote up
/**
 * This method parses the command-line args
 *
 * @param cmd command line options
 * @return Config
 */
private Config parseConfigurationArgs(final CommandLine cmd) {
    final Config config = new Config();

    // Inspect Mode option
    final String mode = cmd.getOptionValue('m');
    if (!mode.equalsIgnoreCase("import") && !mode.equalsIgnoreCase("export")) {
        printHelp("Invalid 'mode' option: " + mode);
    }

    config.setMode(mode);
    config.setResource(cmd.getOptionValue('r'));
    config.setBaseDirectory(cmd.getOptionValue('d'));
    config.setIncludeBinaries(cmd.hasOption('b'));
    config.setRetrieveExternal(cmd.hasOption('x'));
    config.setRetrieveInbound(cmd.hasOption('i'));
    config.setOverwriteTombstones(cmd.hasOption('t'));
    config.setLegacy(cmd.hasOption("L"));
    config.setIncludeVersions(cmd.hasOption('V'));

    if (cmd.getOptionValue('R') != null) {
        config.setRepositoryRoot(cmd.getOptionValue('R'));
    }

    if (cmd.getOptionValue('w') != null) {
        config.setWriteConfig(cmd.getOptionValue('w'));
    }
    final String rdfLanguage = cmd.getOptionValue('l');
    if (rdfLanguage != null) {
        config.setRdfLanguage(rdfLanguage);
    }
    if (cmd.getOptionValues('M') != null) {
        if (cmd.getOptionValues('M').length != 2) {
            throw new IllegalArgumentException("The map should contain the export and import baseURLs, "
                    + "separated by a comma");
        }
        config.setMap(cmd.getOptionValues('M'));
    }
    if (cmd.getOptionValues('p') != null) {
        config.setPredicates(cmd.getOptionValues('p'));
    }

    config.setBagProfile(cmd.getOptionValue('g'));
    config.setBagConfigPath(cmd.getOptionValue('G'));
    config.setBagSerialization(cmd.getOptionValue('s'));

    config.setAuditLog(cmd.hasOption('a'));

    return config;
}
 
Example 18
Source File: OptionsParser.java    From big-c with Apache License 2.0 4 votes vote down vote up
private static String[] getVals(CommandLine command, String option) {
  return command.getOptionValues(option);
}
 
Example 19
Source File: NodeCLI.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public int run(String[] args) throws Exception {

  Options opts = new Options();
  opts.addOption(HELP_CMD, false, "Displays help for all commands.");
  opts.addOption(STATUS_CMD, true, "Prints the status report of the node.");
  opts.addOption(LIST_CMD, false, "List all running nodes. " +
      "Supports optional use of -states to filter nodes " +
      "based on node state, all -all to list all nodes.");
  Option nodeStateOpt = new Option(NODE_STATE_CMD, true,
      "Works with -list to filter nodes based on input comma-separated list of node states.");
  nodeStateOpt.setValueSeparator(',');
  nodeStateOpt.setArgs(Option.UNLIMITED_VALUES);
  nodeStateOpt.setArgName("States");
  opts.addOption(nodeStateOpt);
  Option allOpt = new Option(NODE_ALL, false,
      "Works with -list to list all nodes.");
  opts.addOption(allOpt);
  opts.getOption(STATUS_CMD).setArgName("NodeId");

  int exitCode = -1;
  CommandLine cliParser = null;
  try {
    cliParser = new GnuParser().parse(opts, args);
  } catch (MissingArgumentException ex) {
    sysout.println("Missing argument for options");
    printUsage(opts);
    return exitCode;
  }

  if (cliParser.hasOption("status")) {
    if (args.length != 2) {
      printUsage(opts);
      return exitCode;
    }
    printNodeStatus(cliParser.getOptionValue("status"));
  } else if (cliParser.hasOption("list")) {
    Set<NodeState> nodeStates = new HashSet<NodeState>();
    if (cliParser.hasOption(NODE_ALL)) {
      for (NodeState state : NodeState.values()) {
        nodeStates.add(state);
      }
    } else if (cliParser.hasOption(NODE_STATE_CMD)) {
      String[] types = cliParser.getOptionValues(NODE_STATE_CMD);
      if (types != null) {
        for (String type : types) {
          if (!type.trim().isEmpty()) {
            nodeStates.add(NodeState.valueOf(
                org.apache.hadoop.util.StringUtils.toUpperCase(type.trim())));
          }
        }
      }
    } else {
      nodeStates.add(NodeState.RUNNING);
    }
    listClusterNodes(nodeStates);
  } else if (cliParser.hasOption(HELP_CMD)) {
    printUsage(opts);
    return 0;
  } else {
    syserr.println("Invalid Command Usage : ");
    printUsage(opts);
  }
  return 0;
}
 
Example 20
Source File: SSTableExport.java    From aegisthus with Apache License 2.0 4 votes vote down vote up
@Override
public int run(String[] args) throws Exception {
    Job job = Job.getInstance(getConf());

    job.setJarByClass(SSTableExport.class);
    CommandLine cl = getOptions(args);
    if (cl == null) {
        return 1;
    }

    // Check all of the paths and load the sstable version from the input filenames
    List<Path> paths = Lists.newArrayList();
    if (cl.hasOption(Feature.CMD_ARG_INPUT_FILE)) {
        for (String input : cl.getOptionValues(Feature.CMD_ARG_INPUT_FILE)) {
            checkVersionFromFilename(input);
            paths.add(new Path(input));
        }
    }
    if (cl.hasOption(Feature.CMD_ARG_INPUT_DIR)) {
        paths.addAll(getDataFiles(job.getConfiguration(), cl.getOptionValue(Feature.CMD_ARG_INPUT_DIR)));
    }

    String avroSchemaString = getAvroSchema(cl.getOptionValue(Feature.CMD_ARG_AVRO_SCHEMA_FILE), job.getConfiguration());
    Schema avroSchema = new Schema.Parser().parse(avroSchemaString);

    // At this point we have the version of sstable that we can use for this run
    job.getConfiguration().set(Aegisthus.Feature.CONF_SSTABLE_VERSION, version.toString());

    if (job.getConfiguration().get(Aegisthus.Feature.CONF_CQL_SCHEMA) != null) {
        setConfigurationFromCql(job.getConfiguration());
    }

    job.setInputFormatClass(AegisthusInputFormat.class);
    job.setMapperClass(CQLMapper.class);
    job.setOutputFormatClass(AvroKeyOutputFormat.class);
    AvroJob.setOutputKeySchema(job, avroSchema);

    // Map-only job
    job.setNumReduceTasks(0);

    TextInputFormat.setInputPaths(job, paths.toArray(new Path[paths.size()]));
    FileOutputFormat.setOutputPath(job, new Path(cl.getOptionValue(Feature.CMD_ARG_OUTPUT_DIR)));

    job.submit();
    System.out.println(job.getJobID());
    System.out.println(job.getTrackingURL());
    boolean success = job.waitForCompletion(true);
    return success ? 0 : 1;
}