org.apache.hadoop.conf
Class Configuration

java.lang.Object
  extended by org.apache.hadoop.conf.Configuration
Direct Known Subclasses:
JobConf

public class Configuration
extends Object

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.

The values returned by most get* methods are based on String representations. This String is processed for variable expansion. The available variables are the System properties and the other properties defined in this Configuration.

The only get* method that is not processed for variable expansion is getObject(String) (as it cannot assume that the returned values are String). You can use getObject to obtain the raw value of a String property without variable expansion: if (String)conf.getObject("my.jdk") is "JDK ${java.version}" then conf.get("my.jdk") is "JDK 1.5.0"

Example XML config using variables:
<name>basedir</name><value>/user/${user.name}</value>
<name>tempdir</name><value>${basedir}/tmp</value>
When conf.get("tempdir") is called:
${basedir} is resolved to another property in this Configuration. Then ${user.name} is resolved to a System property.


Constructor Summary
Configuration()
          A new configuration.
Configuration(Configuration other)
          A new configuration with the same settings cloned from another.
 
Method Summary
 void addDefaultResource(Path file)
          Add a default resource.
 void addDefaultResource(String name)
          Add a default resource.
 void addDefaultResource(URL url)
          Add a default resource.
 void addFinalResource(Path file)
          Add a final resource.
 void addFinalResource(String name)
          Add a final resource.
 void addFinalResource(URL url)
          Add a final resource.
 Iterator entries()
           
 String get(String name)
          Returns the value of the name property, or null if no such property exists.
 Object get(String name, Object defaultValue)
          Returns the value of the name property.
 String get(String name, String defaultValue)
          Returns the value of the name property.
 boolean getBoolean(String name, boolean defaultValue)
          Returns the value of the name property as an boolean.
 Class<?> getClass(String name, Class<?> defaultValue)
          Returns the value of the name property as a Class.
<U> Class<? extends U>
getClass(String propertyName, Class<? extends U> defaultValue, Class<U> xface)
          Returns the value of the name property as a Class.
 Class<?> getClassByName(String name)
          Load a class by name.
 ClassLoader getClassLoader()
          Get the class loader for this job.
 InputStream getConfResourceAsInputStream(String name)
          Returns an input stream attached to the configuration resource with the given name.
 Reader getConfResourceAsReader(String name)
          Returns a reader attached to the configuration resource with the given name.
 File getFile(String dirsProp, String path)
          Returns a local file name under a directory named in dirsProp with the given path.
 float getFloat(String name, float defaultValue)
          Returns the value of the name property as a float.
 int getInt(String name, int defaultValue)
          Returns the value of the name property as an integer.
 Path getLocalPath(String dirsProp, String path)
          Returns a local file under a directory named in dirsProp with the given path.
 long getLong(String name, long defaultValue)
          Returns the value of the name property as a long.
 Object getObject(String name)
          Returns the value of the name property, or null if no such property exists.
 URL getResource(String name)
          Returns the URL for the named resource.
 String[] getStrings(String name)
          Returns the value of the name property as an array of strings.
static void main(String[] args)
          For debugging.
 void set(String name, Object value)
          Sets the value of the name property.
 void setBoolean(String name, boolean value)
          Sets the value of the name property to an integer.
 void setClass(String propertyName, Class<?> theClass, Class<?> xface)
          Sets the value of the name property to the name of a class.
 void setClassLoader(ClassLoader classLoader)
          Set the class loader that will be used to load the various objects.
 void setInt(String name, int value)
          Sets the value of the name property to an integer.
 void setLong(String name, long value)
          Sets the value of the name property to a long.
 void setObject(String name, Object value)
          Sets the value of the name property.
 void setQuietMode(boolean value)
          Make this class quiet.
 String toString()
           
 void write(OutputStream out)
          Writes non-default properties in this configuration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Configuration

public Configuration()
A new configuration.


Configuration

public Configuration(Configuration other)
A new configuration with the same settings cloned from another.

Method Detail

addDefaultResource

public void addDefaultResource(String name)
Add a default resource.


addDefaultResource

public void addDefaultResource(URL url)
Add a default resource.


addDefaultResource

public void addDefaultResource(Path file)
Add a default resource.


addFinalResource

public void addFinalResource(String name)
Add a final resource.


addFinalResource

public void addFinalResource(URL url)
Add a final resource.


addFinalResource

public void addFinalResource(Path file)
Add a final resource.


getObject

public Object getObject(String name)
Returns the value of the name property, or null if no such property exists.


setObject

public void setObject(String name,
                      Object value)
Sets the value of the name property.


get

public Object get(String name,
                  Object defaultValue)
Returns the value of the name property. If no such property exists, then defaultValue is returned.


get

public String get(String name)
Returns the value of the name property, or null if no such property exists.


set

public void set(String name,
                Object value)
Sets the value of the name property.


get

public String get(String name,
                  String defaultValue)
Returns the value of the name property. If no such property exists, then defaultValue is returned.


getInt

public int getInt(String name,
                  int 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.


setInt

public void setInt(String name,
                   int value)
Sets the value of the name property to an integer.


getLong

public long getLong(String name,
                    long 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.


setLong

public void setLong(String name,
                    long value)
Sets the value of the name property to a long.


getFloat

public float getFloat(String name,
                      float 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.


getBoolean

public boolean getBoolean(String name,
                          boolean 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".


setBoolean

public void setBoolean(String name,
                       boolean value)
Sets the value of the name property to an integer.


getStrings

public String[] getStrings(String name)
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.


getClassByName

public Class<?> getClassByName(String name)
                        throws ClassNotFoundException
Load a class by name.

Parameters:
name - the class name
Returns:
the class object
Throws:
ClassNotFoundException - if the class is not found

getClass

public Class<?> getClass(String name,
                         Class<?> defaultValue)
Returns the value of the name property as a Class. If no such property is specified, then defaultValue is returned.


getClass

public <U> Class<? extends U> getClass(String propertyName,
                                       Class<? extends U> defaultValue,
                                       Class<U> xface)
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.


setClass

public void setClass(String propertyName,
                     Class<?> theClass,
                     Class<?> xface)
Sets the value of the name property to the name of a class. First checks that the class implements the named interface.


getLocalPath

public Path getLocalPath(String dirsProp,
                         String path)
                  throws IOException
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.

Throws:
IOException

getFile

public File getFile(String dirsProp,
                    String path)
             throws IOException
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.

Throws:
IOException

getResource

public URL getResource(String name)
Returns the URL for the named resource.


getConfResourceAsInputStream

public InputStream getConfResourceAsInputStream(String name)
Returns an input stream attached to the configuration resource with the given name.


getConfResourceAsReader

public Reader getConfResourceAsReader(String name)
Returns a reader attached to the configuration resource with the given name.


entries

public Iterator entries()
Returns:
Iterator< Map.Entry<String,String> >

write

public void write(OutputStream out)
           throws IOException
Writes non-default properties in this configuration.

Throws:
IOException

getClassLoader

public ClassLoader getClassLoader()
Get the class loader for this job.

Returns:
the correct class loader

setClassLoader

public void setClassLoader(ClassLoader classLoader)
Set the class loader that will be used to load the various objects.

Parameters:
classLoader - the new class loader

toString

public String toString()
Overrides:
toString in class Object

setQuietMode

public void setQuietMode(boolean value)
Make this class quiet. Error and informational messages might not be logged.


main

public static void main(String[] args)
                 throws Exception
For debugging. List non-default properties to the terminal and exit.

Throws:
Exception


Copyright © 2006 The Apache Software Foundation