1

How can I convert hadoop's Configuration conf to Map<String, String>?

I have a method that takes Map as an argument and I want to pass Configuration conf to it, so how to convert between the two ?

2 Answers 2

2

You can use the Iterator provided by the Configuration and build the Map.

Configuration configuration = new Configuration();
Map<String, String> map = new HashMap<>();
Iterator<Map.Entry<String,String>> iterator = configuration.iterator();
while (iterator.hasNext()) {
  Map.Entry<String, String> entry = iterator.next();
  map.put(entry.getKey(), entry.getValue());
}

You can also take a look this method, which take a regex and returns a Map<> of configuration.

Sign up to request clarification or add additional context in comments.

1 Comment

You can also do for (Map.Entry<String, String> entry : configuration) { map.put(entry.getKey(), entry.getValue()); }
1

why not do something like:

Map<String, String> config = new HashMap<String, String>();
for(String key: conf.getKeys()){
    config.put(key, conf.getString(key));
} 

That is, iterate over all the entries and add to Map.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.