github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/clients/hadoopfs/src/main/java/io/lakefs/FSConfiguration.java (about)

     1  package io.lakefs;
     2  
     3  import org.apache.hadoop.conf.Configuration;
     4  
     5  import java.util.Arrays;
     6  import java.util.Map;
     7  import java.util.stream.Collectors;
     8  
     9  public final class FSConfiguration {
    10  
    11      private static String formatFSConfigurationKey(String scheme, String key) {
    12          return "fs." + scheme + "." + key;
    13      }
    14  
    15      /**
    16       * lookup value from configuration based on scheme and key suffix.
    17       * first try to get "fs.[scheme].[key suffix]", if value not found use the default scheme
    18       * to build a key for lookup.
    19       *
    20       * @param conf      configuration object to get the value from
    21       * @param scheme    used to format the key for lookup
    22       * @param keySuffix key suffix to lookup
    23       * @return key value or null in case no value found
    24       */
    25      public static String get(Configuration conf, String scheme, String keySuffix) {
    26          String key = formatFSConfigurationKey(scheme, keySuffix);
    27          String value = conf.get(key);
    28          if (value == null && !scheme.equals(Constants.DEFAULT_SCHEME)) {
    29              key = formatFSConfigurationKey(Constants.DEFAULT_SCHEME, keySuffix);
    30              value = conf.get(key);
    31          }
    32          return value;
    33      }
    34  
    35      /**
    36       * lookup value from configuration based on scheme and key suffix, returns default in case of null value.
    37       *
    38       * @param conf         configuration object to get the value from
    39       * @param scheme       used to format key for lookup
    40       * @param keySuffix    key suffix to lookup
    41       * @param defaultValue default value returned in case of null
    42       * @return value found or default value
    43       */
    44      public static String get(Configuration conf, String scheme, String keySuffix, String defaultValue) {
    45          String value = get(conf, scheme, keySuffix);
    46          return (value == null) ? defaultValue : value;
    47      }
    48  
    49      public static int getInt(Configuration conf, String scheme, String keySuffix, int defaultValue) {
    50          String valueString = get(conf, scheme, keySuffix);
    51          return (valueString == null) ? defaultValue : Integer.parseInt(valueString);
    52      }
    53      /**
    54       * lookup a map in a configuration key.
    55       * The map is expected to be in the format of "key1:value1,key2:value2"
    56       *
    57       * @param conf         configuration object to get the value from
    58       * @param scheme       used to format key for lookup
    59       * @param keySuffix    key suffix to lookup
    60       * @return value found or default value
    61       */
    62      public static Map<String,String> getMap(Configuration conf, String scheme, String keySuffix) {
    63          String valueString = get(conf, scheme, keySuffix);
    64          if (valueString == null) {
    65              return null;
    66          }
    67          return Arrays.stream(valueString.split(","))
    68                  .map(entry -> entry.split(":"))
    69                  .collect(Collectors.toMap(
    70                          entry -> entry[0],
    71                          entry -> entry[1]
    72                  ));
    73      }
    74  }