|
34 | 34 | import org.apache.hadoop.conf.Configurable; |
35 | 35 | import org.apache.hadoop.conf.Configuration; |
36 | 36 | import org.apache.hadoop.conf.Configured; |
| 37 | +import org.apache.hadoop.fs.Path; |
37 | 38 | import org.apache.hadoop.util.Tool; |
38 | 39 | import org.apache.hadoop.util.ToolRunner; |
39 | 40 | import org.apache.log4j.Level; |
@@ -78,7 +79,7 @@ public class Main extends Configured implements Tool { |
78 | 79 |
|
79 | 80 | @Parameter( |
80 | 81 | names = {"--config-file"}, |
81 | | - description = "Path to a properties configuration file containing key=value pairs.") |
| 82 | + description = "Path to a configuration file (properties or Hadoop XML format).") |
82 | 83 | private String configFilePath; |
83 | 84 |
|
84 | 85 | @VisibleForTesting |
@@ -182,11 +183,16 @@ public int run(String[] args) throws Exception { |
182 | 183 | Configuration merged = new Configuration(getConf()); |
183 | 184 |
|
184 | 185 | if (configFilePath != null) { |
185 | | - try (InputStream in = new FileInputStream(configFilePath)) { |
186 | | - Properties props = new Properties(); |
187 | | - props.load(in); |
188 | | - props.forEach((key, value) -> merged.set(key.toString(), value.toString())); |
189 | | - console.debug("Loaded configuration from file: {}", configFilePath); |
| 186 | + try { |
| 187 | + if (isXmlConfigFile(configFilePath)) { |
| 188 | + loadXmlConfiguration(merged, configFilePath); |
| 189 | + } else if (isPropertiesConfigFile(configFilePath)) { |
| 190 | + loadPropertiesConfiguration(merged, configFilePath); |
| 191 | + } else { |
| 192 | + throw new IllegalArgumentException( |
| 193 | + "Unsupported config file format. Only .xml and .properties files are supported: " |
| 194 | + + configFilePath); |
| 195 | + } |
190 | 196 | } catch (Exception e) { |
191 | 197 | throw new IllegalArgumentException( |
192 | 198 | "Failed to load config file '" + configFilePath + "': " + e.getMessage(), e); |
@@ -239,4 +245,27 @@ public static void main(String[] args) throws Exception { |
239 | 245 | int rc = ToolRunner.run(new Configuration(), new Main(console), args); |
240 | 246 | System.exit(rc); |
241 | 247 | } |
| 248 | + |
| 249 | + private boolean isXmlConfigFile(String filePath) { |
| 250 | + return filePath.toLowerCase().endsWith(".xml"); |
| 251 | + } |
| 252 | + |
| 253 | + private boolean isPropertiesConfigFile(String filePath) { |
| 254 | + String lowerPath = filePath.toLowerCase(); |
| 255 | + return lowerPath.endsWith(".properties"); |
| 256 | + } |
| 257 | + |
| 258 | + private void loadXmlConfiguration(Configuration config, String filePath) { |
| 259 | + config.addResource(new Path(filePath)); |
| 260 | + console.debug("Loaded XML configuration from file: {}", filePath); |
| 261 | + } |
| 262 | + |
| 263 | + private void loadPropertiesConfiguration(Configuration config, String filePath) throws Exception { |
| 264 | + try (InputStream in = new FileInputStream(filePath)) { |
| 265 | + Properties props = new Properties(); |
| 266 | + props.load(in); |
| 267 | + props.forEach((key, value) -> config.set(key.toString(), value.toString())); |
| 268 | + console.debug("Loaded properties configuration from file: {}", filePath); |
| 269 | + } |
| 270 | + } |
242 | 271 | } |
0 commit comments