Database Reference
In-Depth Information
Configuration conf
=
new
Configuration
();
conf
.
addResource
(
"configuration-1.xml"
);
assertThat
(
conf
.
get
(
"color"
),
is
(
"yellow"
));
assertThat
(
conf
.
getInt
(
"size"
,
0
),
is
(
10
));
assertThat
(
conf
.
get
(
"breadth"
,
"wide"
),
is
(
"wide"
));
There are a couple of things to note: type information is not stored in the XML file; in-
stead, properties can be interpreted as a given type when they are read. Also, the
get()
methods allow you to specify a default value, which is used if the property is not defined
in the XML file, as in the case of
breadth
here.
Combining Resources
Things get interesting when more than one resource is used to define a
Configura-
tion
. This is used in Hadoop to separate out the default properties for the system,
defined internally in a file called
core-default.xml
, from the site-specific overrides in
core-
Example 6-2. A second configuration file, configuration-2.xml
<?xml version="1.0"?>
<configuration>
<property>
<name>
size
</name>
<value>
12
</value>
</property>
<property>
<name>
weight
</name>
<value>
light
</value>
</property>
</configuration>
Resources are added to a
Configuration
in order:
Configuration conf
=
new
Configuration
();
conf
.
addResource
(
"configuration-1.xml"
);
conf
.
addResource
(
"configuration-2.xml"
);
Properties defined in resources that are added later override the earlier definitions. So the
size
property takes its value from the second configuration file,
configuration-2.xml
:
assertThat
(
conf
.
getInt
(
"size"
,
0
),
is
(
12
));