Provides access to configuration parameters. Configurations are specified
by resources. A resource contains a set of name/value pairs.
Each resource is named by either a String or by a Path. If named by a
String, then the classpath is examined for a file with that name. If a
File, then the local filesystem is examined directly, without referring to
the CLASSPATH.
Configuration resources are of two types: default and
final. Default values are loaded first and final values are loaded last, and
thus override default values.
Hadoop's default resource is the String "hadoop-default.xml" and its
final resource is the String "hadoop-site.xml". Other tools built on Hadoop
may specify additional resources.
| Method from org.apache.hadoop.conf.Configuration Detail: |
public void addDefaultResource(String name) {
addResource(defaultResources, name);
}
|
public void addDefaultResource(URL url) {
addResource(defaultResources, url);
}
|
public void addDefaultResource(Path file) {
addResource(defaultResources, file);
}
|
public void addFinalResource(String name) {
addResource(finalResources, name);
}
|
public void addFinalResource(URL url) {
addResource(finalResources, url);
}
|
public void addFinalResource(Path file) {
addResource(finalResources, file);
}
|
public Iterator entries() {
return iterator();
} Deprecated! use - iterator() instead
|
public String get(String name) {
return substituteVars(getProps().getProperty(name));
}
Returns the value of the name property, or null if no
such property exists. |
public Object get(String name,
Object defaultValue) {
Object res = getObject(name);
if (res != null) return res;
else return defaultValue;
} Deprecated! A - side map of Configuration to Object should be used instead.
Returns the value of the name property. If no such property
exists, then defaultValue is returned. |
public String get(String name,
String defaultValue) {
return substituteVars(getProps().getProperty(name, defaultValue));
}
Returns the value of the name property. If no such property
exists, then defaultValue is returned. |
public boolean getBoolean(String name,
boolean defaultValue) {
String valueString = get(name);
if ("true".equals(valueString))
return true;
else if ("false".equals(valueString))
return false;
else return defaultValue;
}
Returns the value of the name property as an boolean. If no
such property is specified, or if the specified value is not a valid
boolean, then defaultValue is returned. Valid boolean values
are "true" and "false". |
public Class getClass(String name,
Class defaultValue) {
String valueString = get(name);
if (valueString == null)
return defaultValue;
try {
return getClassByName(valueString);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
Returns the value of the name property as a Class. If no
such property is specified, then defaultValue is returned. |
public Class getClass(String propertyName,
Class defaultValue,
Class xface) {
try {
Class< ? > theClass = getClass(propertyName, defaultValue);
if (theClass != null && !xface.isAssignableFrom(theClass))
throw new RuntimeException(theClass+" not "+xface.getName());
else if (theClass != null)
return theClass.asSubclass(xface);
else
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Returns the value of the name property as a Class. If no
such property is specified, then defaultValue is returned.
An error is thrown if the returned class does not implement the named
interface. |
public Class getClassByName(String name) throws ClassNotFoundException {
return Class.forName(name, true, classLoader);
}
|
public ClassLoader getClassLoader() {
return classLoader;
}
Get the class loader for this job. |
public InputStream getConfResourceAsInputStream(String name) {
try {
URL url= getResource(name);
if (url == null) {
LOG.info(name + " not found");
return null;
} else {
LOG.info("found resource " + name + " at " + url);
}
return url.openStream();
} catch (Exception e) {
return null;
}
}
Returns an input stream attached to the configuration resource with the
given name. |
public Reader getConfResourceAsReader(String name) {
try {
URL url= getResource(name);
if (url == null) {
LOG.info(name + " not found");
return null;
} else {
LOG.info("found resource " + name + " at " + url);
}
return new InputStreamReader(url.openStream());
} catch (Exception e) {
return null;
}
}
Returns a reader attached to the configuration resource with the
given name. |
public File getFile(String dirsProp,
String path) throws IOException {
String[] dirs = getStrings(dirsProp);
int hashCode = path.hashCode();
for (int i = 0; i < dirs.length; i++) { // try each local dir
int index = (hashCode+i & Integer.MAX_VALUE) % dirs.length;
File file = new File(dirs[index], path);
File dir = file.getParentFile();
if (dir.exists() || dir.mkdirs()) {
return file;
}
}
throw new IOException("No valid local directories in property: "+dirsProp);
}
Returns a local file name under a directory named in dirsProp with
the given path. If dirsProp contains multiple directories,
then one is chosen based on path's hash code. If the selected
directory does not exist, an attempt is made to create it. |
public float getFloat(String name,
float defaultValue) {
String valueString = get(name);
if (valueString == null)
return defaultValue;
try {
return Float.parseFloat(valueString);
} catch (NumberFormatException e) {
return defaultValue;
}
}
Returns the value of the name property as a float. If no
such property is specified, or if the specified value is not a valid
float, then defaultValue is returned. |
public int getInt(String name,
int defaultValue) {
String valueString = get(name);
if (valueString == null)
return defaultValue;
try {
return Integer.parseInt(valueString);
} catch (NumberFormatException e) {
return defaultValue;
}
}
Returns the value of the name property as an integer. If no
such property is specified, or if the specified value is not a valid
integer, then defaultValue is returned. |
public Path getLocalPath(String dirsProp,
String path) throws IOException {
String[] dirs = getStrings(dirsProp);
int hashCode = path.hashCode();
FileSystem fs = FileSystem.getLocal(this);
for (int i = 0; i < dirs.length; i++) { // try each local dir
int index = (hashCode+i & Integer.MAX_VALUE) % dirs.length;
Path file = new Path(dirs[index], path);
Path dir = file.getParent();
if (fs.mkdirs(dir) || fs.exists(dir)) {
return file;
}
}
LOG.warn("Could not make " + path +
" in local directories from " + dirsProp);
for(int i=0; i < dirs.length; i++) {
int index = (hashCode+i & Integer.MAX_VALUE) % dirs.length;
LOG.warn(dirsProp + "[" + index + "]=" + dirs[index]);
}
throw new IOException("No valid local directories in property: "+dirsProp);
}
Returns a local file under a directory named in dirsProp with
the given path. If dirsProp contains multiple directories,
then one is chosen based on path's hash code. If the selected
directory does not exist, an attempt is made to create it. |
public long getLong(String name,
long defaultValue) {
String valueString = get(name);
if (valueString == null)
return defaultValue;
try {
return Long.parseLong(valueString);
} catch (NumberFormatException e) {
return defaultValue;
}
}
Returns the value of the name property as a long. If no
such property is specified, or if the specified value is not a valid
long, then defaultValue is returned. |
public Object getObject(String name) {
return getProps().get(name);
} Deprecated! A - side map of Configuration to Object should be used instead.
Returns the value of the name property, or null if no such
property exists. |
public String getRaw(String name) {
return getProps().getProperty(name);
}
Get the value of the name property, without doing variable
expansion. |
public URL getResource(String name) {
return classLoader.getResource(name);
}
Returns the URL for the named resource. |
public String[] getStrings(String name) {
String valueString = get(name);
return StringUtils.getStrings(valueString);
}
Returns the value of the name property as an array of
strings. If no such property is specified, then null
is returned. Values are comma delimited. |
public Iterator iterator() {
// Get a copy of just the string to string pairs. After the old object
// methods that allow non-strings to be put into configurations are removed,
// we could replace properties with a Map< String,String > and get rid of this
// code.
Map< String,String > result = new HashMap< String,String >();
for(Map.Entry< Object,Object > item: getProps().entrySet()) {
if (item.getKey() instanceof String &&
item.getValue() instanceof String) {
result.put((String) item.getKey(), (String) item.getValue());
}
}
return result.entrySet().iterator();
}
Go through the list of String key-value pairs in the configuration. |
public static void main(String[] args) throws Exception {
new Configuration().write(System.out);
}
For debugging. List non-default properties to the terminal and exit. |
public void set(String name,
Object value) {
getOverlay().setProperty(name, value.toString());
getProps().setProperty(name, value.toString());
} Deprecated!
Sets the value of the name property. |
public void set(String name,
String value) {
getOverlay().setProperty(name, value);
getProps().setProperty(name, value);
}
Sets the value of the name property. |
public void setBoolean(String name,
boolean value) {
set(name, Boolean.toString(value));
}
Sets the value of the name property to an integer. |
public void setClass(String propertyName,
Class theClass,
Class xface) {
if (!xface.isAssignableFrom(theClass))
throw new RuntimeException(theClass+" not "+xface.getName());
set(propertyName, theClass.getName());
}
Sets the value of the name property to the name of a class.
First checks that the class implements the named interface. |
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
Set the class loader that will be used to load the various objects. |
public void setInt(String name,
int value) {
set(name, Integer.toString(value));
}
Sets the value of the name property to an integer. |
public void setLong(String name,
long value) {
set(name, Long.toString(value));
}
Sets the value of the name property to a long. |
public void setObject(String name,
Object value) {
getProps().put(name, value);
} Deprecated!
Sets the value of the name property. |
public synchronized void setQuietMode(boolean value) {
quietmode = value;
}
Make this class quiet. Error and informational
messages might not be logged. |
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Configuration: ");
sb.append("defaults: ");
toString(defaultResources, sb);
sb.append("final: ");
toString(finalResources, sb);
return sb.toString();
}
|
public void write(OutputStream out) throws IOException {
Properties properties = getProps();
try {
Document doc =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element conf = doc.createElement("configuration");
doc.appendChild(conf);
conf.appendChild(doc.createTextNode("\n"));
for (Enumeration e = properties.keys(); e.hasMoreElements();) {
String name = (String)e.nextElement();
Object object = properties.get(name);
String value = null;
if (object instanceof String) {
value = (String) object;
}else {
continue;
}
Element propNode = doc.createElement("property");
conf.appendChild(propNode);
Element nameNode = doc.createElement("name");
nameNode.appendChild(doc.createTextNode(name));
propNode.appendChild(nameNode);
Element valueNode = doc.createElement("value");
valueNode.appendChild(doc.createTextNode(value));
propNode.appendChild(valueNode);
conf.appendChild(doc.createTextNode("\n"));
}
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.transform(source, result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Writes non-default properties in this configuration. |