Database Reference
In-Depth Information
A MapDriver can be used to check for zero, one, or more output records, according to
the number of times that withOutput() is called. In our application, since records with
missing temperatures should be filtered out, this test asserts that no output is produced for
this particular input value.
The new test fails since +9999 is not treated as a special case. Rather than putting more
logic into the mapper, it makes sense to factor out a parser class to encapsulate the parsing
logic; see Example 6-7 .
Example 6-7. A class for parsing weather records in NCDC format
public class NcdcRecordParser {
private static final int MISSING_TEMPERATURE = 9999 ;
private String year ;
private int airTemperature ;
private String quality ;
public void parse ( String record ) {
year = record . substring ( 15 , 19 );
String airTemperatureString ;
// Remove leading plus sign as parseInt doesn't like them (pre-Java
7)
if ( record . charAt ( 87 ) == '+' ) {
airTemperatureString = record . substring ( 88 , 92 );
} else {
airTemperatureString = record . substring ( 87 , 92 );
}
airTemperature = Integer . parseInt ( airTemperatureString );
quality = record . substring ( 92 , 93 );
}
public void parse ( Text record ) {
parse ( record . toString ());
}
public boolean isValidTemperature () {
return airTemperature != MISSING_TEMPERATURE &&
quality . matches ( "[01459]" );
}
public String getYear () {
return year ;
}
Search WWH ::




Custom Search