Database Reference
In-Depth Information
(an
IntWritable
representing the temperature, −1.1°C), before finally calling the
runTest()
method to execute the test. If the expected output values are not emitted by
the mapper, MRUnit will fail the test. Notice that the input key could be set to any value
because our mapper ignores it.
Proceeding in a test-driven fashion, we create a
Mapper
implementation that passes the
test (see
Example 6-6
)
. Because we will be evolving the classes in this chapter, each is put
in a different package indicating its version for ease of exposition. For example,
v1.MaxTemperatureMapper
is version 1 of
MaxTemperatureMapper
. In real-
ity, of course, you would evolve classes without repackaging them.
Example 6-6. First version of a Mapper that passes MaxTemperatureMapperTest
public class
MaxTemperatureMapper
extends
Mapper
<
LongWritable
,
Text
,
Text
,
IntWritable
> {
@Override
public
void
map
(
LongWritable key
,
Text value
,
Context context
)
throws
IOException
,
InterruptedException
{
String line
=
value
.
toString
();
String year
=
line
.
substring
(
15
,
19
);
int
airTemperature
=
Integer
.
parseInt
(
line
.
substring
(
87
,
92
));
context
.
write
(
new
Text
(
year
),
new
IntWritable
(
airTemperature
));
}
}
This is a very simple implementation that pulls the year and temperature fields from the
line and writes them to the
Context
. Let's add a test for missing values, which in the
raw data are represented by a temperature of
+9999
:
@Test
public
void
ignoresMissingTemperatureRecord
()
throws
IOException
,
InterruptedException
{
Text value
=
new
Text
(
"0043011990999991950051518004+68750+023550FM-12+0382"
+
// Year ^^^^
"99999V0203201N00261220001CN9999999N9+99991+99999999999"
);
// Temperature ^^^^^
new
MapDriver
<
LongWritable
,
Text
,
Text
,
IntWritable
>()
.
withMapper
(
new
MaxTemperatureMapper
())
.
withInput
(
new
LongWritable
(
0
),
value
)
.
runTest
();
}