public class Configuration extends Object implements Iterable<Map.Entry<String,String>>
Configurations are specified by resources. A resource contains a set of
name/value pairs as XML data. Each resource is named by a String
.
If named by a String
,
then the classpath is examined for a file with that name. If named by a
Path
, then the local filesystem is examined directly, without
referring to the classpath.
Unless explicitly turned off, Hadoop by default specifies two resources, loaded in-order from the classpath:
Configuration parameters may be declared final.
Once a resource declares a value final, no subsequently-loaded
resource can alter that value.
For example, one might define a final parameter with:
<property>
<name>dfs.client.buffer.dir</name>
<value>/tmp/hadoop/dfs/client</value>
<final>true</final>
</property>
Administrators typically define parameters as final in
core-site.xml for values that user applications may not alter.
Value strings are first processed for variable expansion. The available properties are:
System.getProperties()
.For example, if a configuration resource contains the following property
definitions:
<property>
<name>basedir</name>
<value>/user/${user.name}</value>
</property>
<property>
<name>tempdir</name>
<value>${basedir}/tmp</value>
</property>
When conf.get("tempdir") is called, then ${basedir}
will be resolved to another property in this Configuration, while
${user.name} would then ordinarily be resolved to the value
of the System property with that name.
Modifier and Type | Class and Description |
---|---|
static class |
Configuration.IntegerRanges
A class that represents a set of positive integer ranges.
|
Constructor and Description |
---|
Configuration()
A new configuration.
|
Configuration(Configuration other)
A new configuration with the same settings cloned from another.
|
Modifier and Type | Method and Description |
---|---|
void |
addResource(InputStream in)
Add a configuration resource.
|
void |
addResource(String name)
Add a configuration resource.
|
void |
addResource(URL url)
Add a configuration resource.
|
void |
clear()
Clears all keys from the configuration.
|
static Configuration |
create()
Creates an instance of configuration with default and site values.
|
static void |
dumpConfiguration(Configuration config,
Writer out)
Writes out all the parameters and their properties (final and resource) to
the given
Writer
The format of the output would be
{ "properties" : [ {key1,value1,key1.isFinal,key1.resource}, {key2,value2,
key2.isFinal,key2.resource}... |
String |
get(String name)
Get the value of the
name property, null if
no such property exists. |
String |
get(String name,
String defaultValue)
Get the value of the
name . |
boolean |
getBoolean(String name)
Get the value of the
name property as a boolean . |
boolean |
getBoolean(String name,
boolean defaultValue)
Get the value of the
name property as a boolean . |
Class<?> |
getClass(String name,
Class<?> defaultValue)
Get the value of the
name property as a Class . |
<U> Class<? extends U> |
getClass(String name,
Class<? extends U> defaultValue,
Class<U> xface)
Get the value of the
name property as a Class
implementing the interface specified by xface . |
Class<?> |
getClassByName(String name)
Load a class by name.
|
Class<?> |
getClassByNameOrNull(String name)
Load a class by name, returning null rather than throwing an exception
if it couldn't be loaded.
|
Class<?>[] |
getClasses(String name,
Class<?>... defaultValue)
Get the value of the
name property
as an array of Class . |
ClassLoader |
getClassLoader()
Get the
ClassLoader for this job. |
InputStream |
getConfResourceAsInputStream(String name)
Get an input stream attached to the configuration resource with the
given
name . |
Reader |
getConfResourceAsReader(String name)
Get a
Reader attached to the configuration resource with the
given name . |
<T extends Enum<T>> |
getEnum(String name,
Class<T> declaringClass)
Return value matching this enumerated type.
|
<T extends Enum<T>> |
getEnum(String name,
T defaultValue)
Return value matching this enumerated type.
|
File |
getFile(String dirsProp,
String path)
Get a local file name under a directory named in dirsProp with
the given path.
|
float |
getFloat(String name)
Get the value of the
name property as a float . |
float |
getFloat(String name,
float defaultValue)
Get the value of the
name property as a float . |
<U> List<U> |
getInstances(String name,
Class<U> xface)
Get the value of the
name property as a List
of objects implementing the interface specified by xface . |
int |
getInt(String name)
Get the value of the
name configuration property as an int . |
int |
getInt(String name,
int defaultValue)
Get the value of the
name property as an int . |
long |
getLong(String name)
Get the value of the
name configuration property as a long . |
long |
getLong(String name,
long defaultValue)
Get the value of the
name property as a long . |
long |
getLongBytes(String name)
Get the value of the
name property as a long or
human readable format. |
long |
getLongBytes(String name,
long defaultValue)
Get the value of the
name property as a long or
human readable format. |
Pattern |
getPattern(String name)
Get the value of the
name property as a Pattern . |
Pattern |
getPattern(String name,
Pattern defaultValue)
Get the value of the
name property as a Pattern . |
protected Properties |
getProps() |
Configuration.IntegerRanges |
getRange(String name)
Parse the given attribute as a set of integer ranges.
|
Configuration.IntegerRanges |
getRange(String name,
String defaultValue)
Parse the given attribute as a set of integer ranges.
|
String |
getRaw(String name)
Get the value of the
name property, without doing
variable expansion.If the key is
deprecated, it returns the value of the first key which replaces
the deprecated key and is not null. |
URL |
getResource(String name)
Get the
URL for the named resource. |
Collection<String> |
getStringCollection(String name)
Get the comma delimited values of the
name property as
a collection of String s. |
String[] |
getStrings(String name)
Get the comma delimited values of the
name property as
an array of String s. |
String[] |
getStrings(String name,
String... defaultValue)
Get the comma delimited values of the
name property as
an array of String s. |
String |
getTrimmed(String name)
Get the value of the
name property as a trimmed String ,
null if no such property exists. |
Collection<String> |
getTrimmedStringCollection(String name)
Get the comma delimited values of the
name property as
a collection of String s, trimmed of the leading and trailing whitespace. |
String[] |
getTrimmedStrings(String name)
Get the comma delimited values of the
name property as
an array of String s, trimmed of the leading and trailing whitespace. |
String[] |
getTrimmedStrings(String name,
String... defaultValue)
Get the comma delimited values of the
name property as
an array of String s, trimmed of the leading and trailing whitespace. |
Map<String,String> |
getValByRegex(String regex)
get keys matching the the regex.
|
static boolean |
isDeprecated(String key)
checks whether the given
key is deprecated. |
Iterator<Map.Entry<String,String>> |
iterator() |
static void |
main(String[] args)
For debugging.
|
void |
reloadConfiguration()
Reload configuration from previously added resources.
|
void |
set(String name,
String value)
Set the
value of the name property. |
void |
setBoolean(String name,
boolean value)
Set the value of the
name property to a boolean . |
void |
setBooleanIfUnset(String name,
boolean value)
Set the given property, if it is currently unset.
|
void |
setClass(String name,
Class<?> theClass,
Class<?> xface)
Set the value of the
name property to the name of a
theClass implementing the given interface xface . |
void |
setClassLoader(ClassLoader classLoader)
Set the class loader that will be used to load the various objects.
|
<T extends Enum<T>> |
setEnum(String name,
T value)
Set the value of the
name property to the given type. |
void |
setFloat(String name,
float value)
Set the value of the
name property to a float . |
void |
setIfUnset(String name,
String value)
Sets a property if it is currently unset.
|
void |
setInt(String name,
int value)
Set the value of the
name property to an int . |
void |
setLong(String name,
long value)
Set the value of the
name property to a long . |
void |
setPattern(String name,
Pattern pattern)
Set the given property to
Pattern . |
void |
setQuietMode(boolean quietmode)
Set the quietness-mode.
|
void |
setStrings(String name,
String... values)
Set the array of string values for the
name property as
as comma delimited values. |
int |
size()
Return the number of keys in the configuration.
|
String |
toString() |
void |
unset(String name)
Unset a previously set property.
|
void |
writeXml(OutputStream out)
Write out the non-default properties in this configuration to the given
OutputStream . |
void |
writeXml(Writer out)
Write out the non-default properties in this configuration to the given
Writer . |
public Configuration()
public Configuration(Configuration other)
other
- the configuration from which to clone settings.public static boolean isDeprecated(String key)
key
is deprecated.key
- the parameter which is to be checked for deprecationtrue
if the key is deprecated and
false
otherwise.public static Configuration create()
public void addResource(String name)
name
- resource to be added, the classpath is examined for a file
with that name.public void addResource(URL url)
url
- url of the resource to be added, the local filesystem is
examined directly to find the resource, without referring to
the classpath.public void addResource(InputStream in)
in
- InputStream to deserialize the object from.public void reloadConfiguration()
public String get(String name)
name
property, null
if
no such property exists. If the key is deprecated, it returns the value of
the first key which replaces the deprecated key and is not null
Values are processed for variable expansion
before being returned.name
- the property name.name
or its replacing property,
or null if no such property exists.public String getTrimmed(String name)
name
property as a trimmed String
,
null
if no such property exists.
If the key is deprecated, it returns the value of
the first key which replaces the deprecated key and is not null
Values are processed for variable expansion
before being returned.name
- the property name.name
or its replacing property,
or null if no such property exists.public String getRaw(String name)
name
property, without doing
variable expansion.If the key is
deprecated, it returns the value of the first key which replaces
the deprecated key and is not null.name
- the property name.name
property or
its replacing property and null if no such property exists.public void set(String name, String value)
value
of the name
property. If
name
is deprecated or there is a deprecated name associated to it,
it sets the value to both names.name
- property name.value
- property value.public void unset(String name)
public void setIfUnset(String name, String value)
name
- the property namevalue
- the new valuepublic String get(String name, String defaultValue)
name
. If the key is deprecated,
it returns the value of the first key which replaces the deprecated key
and is not null.
If no such property exists,
then defaultValue
is returned.name
- property name.defaultValue
- default value.defaultValue
if the property
doesn't exist.public int getInt(String name)
name
configuration property as an int
. If the property is missing
from the configuration or is not a valid int
, then an exception is thrown.name
- the configuration property nameint
NumberFormatException
- if the configured value is not a valid int
NullPointerException
- if the configuration property is not present in the loaded configpublic int getInt(String name, int defaultValue)
name
property as an int
.
If no such property exists, the provided default value is returned,
or if the specified value is not a valid int
,
then an error is thrown.name
- property name.defaultValue
- default value.int
,
or defaultValue
.NumberFormatException
- when the value is invalidpublic void setInt(String name, int value)
name
property to an int
.name
- property name.value
- int
value of the property.public long getLong(String name)
name
configuration property as a long
. If the config property does
does not exist or is not a valid long
, then an exception is thrown.name
- the configuration property namelong
NumberFormatException
- if the configured value is not a valid long
NullPointerException
- if the configuration property is not present in the loaded configpublic long getLong(String name, long defaultValue)
name
property as a long
.
If no such property exists, the provided default value is returned,
or if the specified value is not a valid long
,
then an error is thrown.name
- property name.defaultValue
- default value.long
,
or defaultValue
.NumberFormatException
- when the value is invalidpublic long getLongBytes(String name)
name
property as a long
or
human readable format. If no such property exists or if the specified value is not a valid
long
or human readable format, then an error is thrown. You
can use the following suffix (case insensitive): k(kilo), m(mega), g(giga),
t(tera), p(peta), e(exa)name
- property name.long
NumberFormatException
- when the value is invalidNullPointerException
- if the configuration property does not existpublic long getLongBytes(String name, long defaultValue)
name
property as a long
or
human readable format. If no such property exists, the provided default
value is returned, or if the specified value is not a valid
long
or human readable format, then an error is thrown. You
can use the following suffix (case insensitive): k(kilo), m(mega), g(giga),
t(tera), p(peta), e(exa)name
- property name.defaultValue
- default value.long
,
or defaultValue
.NumberFormatException
- when the value is invalidpublic void setLong(String name, long value)
name
property to a long
.name
- property name.value
- long
value of the property.public float getFloat(String name)
name
property as a float
.
If no such property exists or if the specified value is not a valid float
,
then an error is thrown.name
- property name.float
NumberFormatException
- when the value is invalidNullPointerException
- if the configuration property does not existpublic float getFloat(String name, float defaultValue)
name
property as a float
.
If no such property exists, the provided default value is returned,
or if the specified value is not a valid float
,
then an error is thrown.name
- property name.defaultValue
- default value.float
,
or defaultValue
.NumberFormatException
- when the value is invalidpublic void setFloat(String name, float value)
name
property to a float
.name
- property name.value
- property value.public boolean getBoolean(String name)
name
property as a boolean
.
If no such property is specified, or if the specified value is not a valid
boolean
, then an exception is thrown.name
- property name.boolean
NullPointerException
- if the configuration property does not existIllegalArgumentException
- if the configured value is not a valid boolean
public boolean getBoolean(String name, boolean defaultValue)
name
property as a boolean
.
If no such property is specified, or if the specified value is not a valid
boolean
, then defaultValue
is returned.name
- property name.defaultValue
- default value.boolean
,
or defaultValue
.public void setBoolean(String name, boolean value)
name
property to a boolean
.name
- property name.value
- boolean
value of the property.public void setBooleanIfUnset(String name, boolean value)
name
- property namevalue
- new valuepublic <T extends Enum<T>> void setEnum(String name, T value)
name
property to the given type. This
is equivalent to set(<name>, value.toString())
.name
- property namevalue
- new valuepublic <T extends Enum<T>> T getEnum(String name, Class<T> declaringClass)
name
- Property nameNullPointerException
- if the configuration property does not existIllegalArgumentException
- If mapping is illegal for the type
providedpublic <T extends Enum<T>> T getEnum(String name, T defaultValue)
name
- Property namedefaultValue
- Value returned if no mapping existsIllegalArgumentException
- If mapping is illegal for the type
providedpublic Pattern getPattern(String name)
name
property as a Pattern
.
If no such property is specified, or if the specified value is not a valid
Pattern
, then an exception is thrown.name
- property nameNullPointerException
- if the configuration property does not existPatternSyntaxException
- if the configured value is not a valid Pattern
public Pattern getPattern(String name, Pattern defaultValue)
name
property as a Pattern
.
If no such property is specified, or if the specified value is not a valid
Pattern
, then DefaultValue
is returned.name
- property namedefaultValue
- default valuepublic void setPattern(String name, Pattern pattern)
Pattern
.
If the pattern is passed as null, sets the empty pattern which results in
further calls to getPattern(...) returning the default value.name
- property namepattern
- new valuepublic Configuration.IntegerRanges getRange(String name)
name
- the attribute nameNullPointerException
- if the configuration property does not existpublic Configuration.IntegerRanges getRange(String name, String defaultValue)
name
- the attribute namedefaultValue
- the default value if it is not setpublic Collection<String> getStringCollection(String name)
name
property as
a collection of String
s.
If no such property is specified then empty collection is returned.
This is an optimized version of getStrings(String)
name
- property name.String
s.public String[] getStrings(String name)
name
property as
an array of String
s.
If no such property is specified then null
is returned.name
- property name.String
s,
or null
.public String[] getStrings(String name, String... defaultValue)
name
property as
an array of String
s.
If no such property is specified then default value is returned.name
- property name.defaultValue
- The default valueString
s,
or default value.public Collection<String> getTrimmedStringCollection(String name)
name
property as
a collection of String
s, trimmed of the leading and trailing whitespace.
If no such property is specified then empty Collection
is returned.name
- property name.String
s, or empty Collection
public String[] getTrimmedStrings(String name)
name
property as
an array of String
s, trimmed of the leading and trailing whitespace.
If no such property is specified then an empty array is returned.name
- property name.String
s,
or empty array.public String[] getTrimmedStrings(String name, String... defaultValue)
name
property as
an array of String
s, trimmed of the leading and trailing whitespace.
If no such property is specified then default value is returned.name
- property name.defaultValue
- The default valueString
s,
or default value.public void setStrings(String name, String... values)
name
property as
as comma delimited values.name
- property name.values
- The valuespublic Class<?> getClassByName(String name) throws ClassNotFoundException
name
- the class name.ClassNotFoundException
- if the class is not found.public Class<?> getClassByNameOrNull(String name)
name
- the class namepublic Class<?>[] getClasses(String name, Class<?>... defaultValue)
name
property
as an array of Class
.
The value of the property specifies a list of comma separated class names.
If no such property is specified, then defaultValue
is
returned.name
- the property name.defaultValue
- default value.Class[]
,
or defaultValue
.public Class<?> getClass(String name, Class<?> defaultValue)
name
property as a Class
.
If no such property is specified, then defaultValue
is
returned.name
- the class name.defaultValue
- default value.Class
,
or defaultValue
.public <U> Class<? extends U> getClass(String name, Class<? extends U> defaultValue, Class<U> xface)
name
property as a Class
implementing the interface specified by xface
.
If no such property is specified, then defaultValue
is
returned.
An exception is thrown if the returned class does not implement the named
interface.name
- the class name.defaultValue
- default value.xface
- the interface implemented by the named class.Class
,
or defaultValue
.public <U> List<U> getInstances(String name, Class<U> xface)
name
property as a List
of objects implementing the interface specified by xface
.
An exception is thrown if any of the classes does not exist, or if it does
not implement the named interface.name
- the property name.xface
- the interface implemented by the classes named by
name
.List
of objects implementing xface
.public void setClass(String name, Class<?> theClass, Class<?> xface)
name
property to the name of a
theClass
implementing the given interface xface
.
An exception is thrown if theClass
does not implement the
interface xface
.name
- property name.theClass
- property value.xface
- the interface implemented by the named class.public File getFile(String dirsProp, String path) throws IOException
dirsProp
- directory in which to locate the file.path
- file-path.IOException
public URL getResource(String name)
URL
for the named resource.name
- resource name.public InputStream getConfResourceAsInputStream(String name)
name
.name
- configuration resource name.public Reader getConfResourceAsReader(String name)
Reader
attached to the configuration resource with the
given name
.name
- configuration resource name.protected Properties getProps()
public int size()
public void clear()
public void writeXml(OutputStream out) throws IOException
OutputStream
.out
- the output stream to write to.IOException
public void writeXml(Writer out) throws IOException
Writer
.out
- the writer to write to.IOException
public static void dumpConfiguration(Configuration config, Writer out) throws IOException
Writer
The format of the output would be
{ "properties" : [ {key1,value1,key1.isFinal,key1.resource}, {key2,value2,
key2.isFinal,key2.resource}... ] }
It does not output the parameters of the configuration object which is
loaded from an input stream.out
- the Writer to write toIOException
public ClassLoader getClassLoader()
ClassLoader
for this job.public void setClassLoader(ClassLoader classLoader)
classLoader
- the new class loader.public void setQuietMode(boolean quietmode)
quietmode
- true
to set quiet-mode on, false
to turn it off.public static void main(String[] args) throws Exception
Exception
public Map<String,String> getValByRegex(String regex)
regex
- Copyright © 2013-2016 Cask Data, Inc. Licensed under the Apache License, Version 2.0.