Java Code Examples for org.apache.hadoop.conf.Configuration.getValByRegex()

The following are Jave code examples for showing how to use getValByRegex() of the org.apache.hadoop.conf.Configuration class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: angel   File: ConfUtils.java   View Source Code Vote up 4 votes
private static void expandEnv(Configuration conf) {

    Map<String, String> kvs = conf.getValByRegex("angel.*");
    Pattern pattern = Pattern.compile("\\$\\{[\\p{Alnum}\\p{Punct}]+?\\}");

    for (Map.Entry<String, String> kv : kvs.entrySet()) {
      String value = kv.getValue();
      Matcher matcher = pattern.matcher(value);
      List<String> keys = new ArrayList<String>();

      while (matcher.find()) {
        String matchedStr = matcher.group();
        keys.add(matchedStr.substring(2, matchedStr.length() - 1));
      }

      int size = keys.size();
      for (int i = 0; i < size; i++) {
        String envValue = System.getenv(keys.get(i));
        if (envValue == null) {
          LOG.warn("env " + keys.get(i) + " is null, please check.");
          continue;
        }
        value = value.replaceAll("\\$\\{" + keys.get(i) + "\\}", envValue);
      }

      conf.set(kv.getKey(), value);
    }

    // Add default fs(local fs) for lib jars.
    String libJars = conf.get(AngelConf.ANGEL_JOB_LIBJARS);
    if (libJars != null) {
      StringBuilder sb = new StringBuilder();
      String[] jars = libJars.split(",");
      for (int i = 0; i < jars.length; i++) {
        if (new Path(jars[i]).isAbsoluteAndSchemeAuthorityNull()) {
          sb.append("file://").append(jars[i]);
          if (i != jars.length - 1) {
            sb.append(",");
          }
        } else {
          sb.append(jars[i]);
          if (i != jars.length - 1) {
            sb.append(",");
          }
        }
      }
      conf.set(AngelConf.ANGEL_JOB_LIBJARS, sb.toString());
      LOG.info("jars loaded: " + sb.toString());
    }
  }