org.apache.commons.lang3.text.StrTokenizer Java Examples

The following examples show how to use org.apache.commons.lang3.text.StrTokenizer. 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: SequencesImpl.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected Object executeScript(Sequence sequence, String sqlScript) {
    EntityManager em = persistence.getEntityManager(getDataStore(sequence));
    StrTokenizer tokenizer = new StrTokenizer(sqlScript, SequenceSupport.SQL_DELIMITER);
    Object value = null;
    Connection connection = em.getConnection();
    while (tokenizer.hasNext()) {
        String sql = tokenizer.nextToken();
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            try {
                if (statement.execute()) {
                    ResultSet rs = statement.getResultSet();
                    if (rs.next())
                        value = rs.getLong(1);
                }
            } finally {
                DbUtils.closeQuietly(statement);
            }
        } catch (SQLException e) {
            throw new IllegalStateException("Error executing SQL for getting next number", e);
        }
    }
    return value;
}
 
Example #2
Source File: StratosApplication.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
@Override
protected int executeCommand(String line) {
    String[] tokens = new StrTokenizer(line).getTokenArray();
    String action = tokens[0];
    String[] actionArgs = Arrays.copyOfRange(tokens, 1, tokens.length);
    if (logger.isDebugEnabled()) {
        logger.debug("Executing command action: {}, Tokens: {}", action, tokens.length);
    }
    Command<StratosCommandContext> command = commands.get(action);
    if (command == null) {
        System.out.println(action + ": command not found.");
        return CliConstants.COMMAND_FAILED;
    }
    try {
        return command.execute(context, actionArgs, new Option[0]);
    } catch (CommandException e) {
        if (logger.isErrorEnabled()) {
            logger.error("Error executing command: " + action, e);
        }
        return CliConstants.ERROR_CODE;
    }
}
 
Example #3
Source File: App.java    From Java-for-Data-Science with MIT License 5 votes vote down vote up
public static void apacheCommonsTokenizer(String text){
	StrTokenizer tokenizer = new StrTokenizer(text,",");
	while (tokenizer.hasNext()) {
		out.println(tokenizer.next());
	}

}
 
Example #4
Source File: PathUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Convert fs path to list of strings.
 * /a/b/c -> [a,b,c]
 * @param fsPath a string
 * @return list of path components
 */
public static List<String> toPathComponents(String fsPath) {
  if (fsPath == null ) {
    return EMPTY_SCHEMA_PATHS;
  }

  final StrTokenizer tokenizer = new StrTokenizer(fsPath, SLASH_CHAR, SqlUtils.QUOTE).setIgnoreEmptyTokens(true);
  return tokenizer.getTokenList();
}
 
Example #5
Source File: PathUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Convert fs path to list of strings.
 * /a/b/c -> [a,b,c]
 * @param fsPath a string
 * @return list of path components
 */
public static List<String> toPathComponents(String fsPath) {
  if (fsPath == null ) {
    return EMPTY_SCHEMA_PATHS;
  }

  final StrTokenizer tokenizer = new StrTokenizer(fsPath, SLASH_CHAR, SqlUtils.QUOTE).setIgnoreEmptyTokens(true);
  return tokenizer.getTokenList();
}
 
Example #6
Source File: SingleTaskLauncher.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
private void addJavaOptions() {
  if (sysConfig.hasPath(GobblinClusterConfigurationKeys.TASK_JVM_OPTIONS)) {
    final String taskJvmOptions = sysConfig.getString(GobblinClusterConfigurationKeys.TASK_JVM_OPTIONS);
    StrTokenizer tokenizer = new StrTokenizer(taskJvmOptions, ' ', '"');
    while(tokenizer.hasNext()) {
      this.cmd.add(tokenizer.next());
    }
  }
}
 
Example #7
Source File: ProcessExecutor.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
private List<String> processCommand(String commandLine) {
    List<String> command = new ArrayList<>(arguments);
    if(!isEmpty(commandLine)) {
        command.addAll(new StrTokenizer(commandLine, ',', '"').getTokenList());
    }

    return command;
}
 
Example #8
Source File: DirectWriterCommand.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private Writer getWriter(OptionManager options) throws IOException{
  final StoreQueryResultsPolicy storeQueryResultsPolicy = Optional
      .ofNullable(options.getOption(STORE_QUERY_RESULTS.getOptionName()))
      .map(o -> StoreQueryResultsPolicy.valueOf(o.getStringVal().toUpperCase(Locale.ROOT)))
      .orElse(StoreQueryResultsPolicy.NO);

  // Verify that query results are stored
  switch (storeQueryResultsPolicy) {
  case NO:
    return null;

  case DIRECT_PATH:
  case PATH_AND_ATTEMPT_ID:
    // supported cases
    break;

  default:
    logger.warn("Unknown query result store policy {}. Query results won't be saved", storeQueryResultsPolicy);
    return null;
  }

  final String storeTablePath = options.getOption(QUERY_RESULTS_STORE_TABLE.getOptionName()).getStringVal();
  final Quoting quoting = Optional.ofNullable(context.getSession().getInitialQuoting()).orElse(ParserConfig.QUOTING);
  final List<String> storeTable =
      new StrTokenizer(storeTablePath, '.', quoting.string.charAt(0))
          .setIgnoreEmptyTokens(true)
          .getTokenList();

  if (storeQueryResultsPolicy == StoreQueryResultsPolicy.PATH_AND_ATTEMPT_ID) {
    // QueryId is same as attempt id. Using its string form for the table name
    storeTable.add(QueryIdHelper.getQueryId(context.getQueryId()));
  }

  // Query results are stored in arrow format. If need arises, we can change
  // this to a configuration option.
  final Map<String, Object> storageOptions = ImmutableMap.<String, Object> of("type", ArrowFormatPlugin.ARROW_DEFAULT_NAME);

  final CreateTableEntry createTableEntry = context.getCatalog()
      .resolveCatalog(SystemUser.SYSTEM_USERNAME)
      .createNewTable(new NamespaceKey(storeTable), null, WriterOptions.DEFAULT, storageOptions);
  return createTableEntry.getWriter(new OpProps(0, SystemUser.SYSTEM_USERNAME, 0, Long.MAX_VALUE, 0, 0, false, 4095, RecordWriter.SCHEMA, false, 0.0d, false), null);
}
 
Example #9
Source File: SqlHandlerUtil.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 * When enabled, add a writer rel on top of the given rel to catch the output and write to configured store table.
 * @param inputRel
 * @return
 */
public static Rel storeQueryResultsIfNeeded(final SqlParser.Config config, final QueryContext context,
                                            final Rel inputRel) {
  final OptionManager options = context.getOptions();
  final StoreQueryResultsPolicy storeQueryResultsPolicy = Optional
      .ofNullable(options.getOption(STORE_QUERY_RESULTS.getOptionName()))
      .map(o -> StoreQueryResultsPolicy.valueOf(o.getStringVal().toUpperCase(Locale.ROOT)))
      .orElse(StoreQueryResultsPolicy.NO);

  switch (storeQueryResultsPolicy) {
  case NO:
    return inputRel;

  case DIRECT_PATH:
  case PATH_AND_ATTEMPT_ID:
    // supported cases
    break;

  default:
    logger.warn("Unknown query result store policy {}. Query results won't be saved", storeQueryResultsPolicy);
    return inputRel;
  }

  final String storeTablePath = options.getOption(QUERY_RESULTS_STORE_TABLE.getOptionName()).getStringVal();
  final List<String> storeTable =
      new StrTokenizer(storeTablePath, '.', config.quoting().string.charAt(0))
          .setIgnoreEmptyTokens(true)
          .getTokenList();

  if (storeQueryResultsPolicy == StoreQueryResultsPolicy.PATH_AND_ATTEMPT_ID) {
    // QueryId is same as attempt id. Using its string form for the table name
    storeTable.add(QueryIdHelper.getQueryId(context.getQueryId()));
  }

  // Query results are stored in arrow format. If need arises, we can change this to a configuration option.
  final Map<String, Object> storageOptions = ImmutableMap.<String, Object>of("type", ArrowFormatPlugin.ARROW_DEFAULT_NAME);

  WriterOptions writerOptions =
    options.getOption(PlannerSettings.ENABLE_OUTPUT_LIMITS)
    ? WriterOptions.DEFAULT.withRecordLimit(options.getOption(PlannerSettings.OUTPUT_LIMIT_SIZE))
    : WriterOptions.DEFAULT;

  // store table as system user.
  final CreateTableEntry createTableEntry = context.getCatalog()
      .resolveCatalog(SystemUser.SYSTEM_USERNAME)
      .createNewTable(new NamespaceKey(storeTable), null, writerOptions, storageOptions);

  final RelTraitSet traits = inputRel.getCluster().traitSet().plus(Rel.LOGICAL);
  return new WriterRel(inputRel.getCluster(), traits, inputRel, createTableEntry, inputRel.getRowType());
}
 
Example #10
Source File: SqlUtils.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Parse the schema path into a list of schema entries.
 * @param schemaPath
 * @return
 */
public static List<String> parseSchemaPath(String schemaPath) {
  return new StrTokenizer(schemaPath, '.', SqlUtils.QUOTE)
      .setIgnoreEmptyTokens(true)
      .getTokenList();
}
 
Example #11
Source File: CommandCompleter.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {

    if (buffer.contains(CliConstants.RESOURCE_PATH_LONG_OPTION)) {
        return fileNameCompleter.complete(buffer, cursor, candidates);
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Buffer: {}, cursor: {}", buffer, cursor);
        logger.trace("Candidates {}", candidates);
    }
    if (StringUtils.isNotBlank(buffer)) {
        // User is typing a command
        StrTokenizer strTokenizer = new StrTokenizer(buffer);
        String action = strTokenizer.next();
        Collection<String> arguments = argumentMap.get(action);
        if (arguments != null) {
            if (logger.isTraceEnabled()) {
                logger.trace("Arguments found for {}, Tokens: {}", action, strTokenizer.getTokenList());
                logger.trace("Arguments for {}: {}", action, arguments);
            }
            List<String> args = new ArrayList<String>(arguments);
            List<Completer> completers = new ArrayList<Completer>();
            for (String token : strTokenizer.getTokenList()) {
                boolean argContains = arguments.contains(token);
                if (token.startsWith("-") && !argContains) {
                    continue;
                }
                if (argContains) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("Removing argument {}", token);
                    }
                    args.remove(token);
                }
                completers.add(new StringsCompleter(token));
            }
            completers.add(new StringsCompleter(args));
            Completer completer = new ArgumentCompleter(completers);
            return completer.complete(buffer, cursor, candidates);
        } else if (CliConstants.HELP_ACTION.equals(action)) {
            // For help action, we need to display available commands as arguments
            return helpCommandCompleter.complete(buffer, cursor, candidates);
        }
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Using Default Completer...");
    }
    return defaultCommandCompleter.complete(buffer, cursor, candidates);
}
 
Example #12
Source File: PathUtils.java    From dremio-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Parase a fully qualified dotted schema path to list of strings.
 * a.b.`c.json` -> [a,b,c.json]
 * a.b.`c-1`    -> [a,b,c-1]
 * a.b.c        -> [a,b,c]
 * @param path dotted schema path
 * @return list of path components.
 */
public static List<String> parseFullPath(final String path) {
  final StrTokenizer tokenizer = new StrTokenizer(path, PATH_DELIMITER, SqlUtils.QUOTE).setIgnoreEmptyTokens(true);
  return tokenizer.getTokenList();
}
 
Example #13
Source File: PathUtils.java    From dremio-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Parase a fully qualified dotted schema path to list of strings.
 * a.b.`c.json` -> [a,b,c.json]
 * a.b.`c-1`    -> [a,b,c-1]
 * a.b.c        -> [a,b,c]
 * @param path dotted schema path
 * @return list of path components.
 */
public static List<String> parseFullPath(final String path) {
  final StrTokenizer tokenizer = new StrTokenizer(path, PATH_DELIMITER, SqlUtils.QUOTE).setIgnoreEmptyTokens(true);
  return tokenizer.getTokenList();
}