Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify and consolidate examples in Developing an MR App chapter.
- Loading branch information
Showing
21 changed files
with
113 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,29 @@ | ||
package v2; | ||
//== MaxTemperatureMapperV2 | ||
// cc MaxTemperatureMapperV2 A Mapper that uses a utility class to parse records | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.hadoop.io.IntWritable; | ||
import org.apache.hadoop.io.LongWritable; | ||
import org.apache.hadoop.io.Text; | ||
import org.apache.hadoop.mapreduce.Mapper; | ||
import v2.NcdcRecordParser; | ||
|
||
// vv MaxTemperatureMapperV2 | ||
public class MaxTemperatureMapper | ||
extends Mapper<LongWritable, Text, Text, IntWritable> { | ||
|
||
//vv MaxTemperatureMapperV2 | ||
extends Mapper<LongWritable, Text, Text, IntWritable> { | ||
|
||
/*[*/private NcdcRecordParser parser = new NcdcRecordParser();/*]*/ | ||
|
||
@Override | ||
public void map(LongWritable key, Text value, Context context) | ||
throws IOException, InterruptedException { | ||
|
||
String line = value.toString(); | ||
String year = line.substring(15, 19); | ||
/*[*/String temp = line.substring(87, 92); | ||
if (!missing(temp)) {/*]*/ | ||
int airTemperature = Integer.parseInt(temp); | ||
context.write(new Text(year), new IntWritable(airTemperature)); | ||
/*[*/}/*]*/ | ||
|
||
/*[*/parser.parse(value);/*]*/ | ||
if (/*[*/parser.isValidTemperature()/*]*/) { | ||
context.write(new Text(/*[*/parser.getYear()/*]*/), | ||
new IntWritable(/*[*/parser.getAirTemperature()/*]*/)); | ||
} | ||
} | ||
|
||
/*[*/private boolean missing(String temp) { | ||
return temp.equals("+9999"); | ||
}/*]*/ | ||
//^^ MaxTemperatureMapperV2 | ||
} | ||
// ^^ MaxTemperatureMapperV2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,34 @@ | ||
// == MaxTemperatureMapperV4 | ||
package v4; | ||
|
||
// cc MaxTemperatureMapperV4 Mapper for maximum temperature example | ||
import java.io.IOException; | ||
|
||
import org.apache.hadoop.io.IntWritable; | ||
import org.apache.hadoop.io.LongWritable; | ||
import org.apache.hadoop.io.Text; | ||
import org.apache.hadoop.mapreduce.Mapper; | ||
|
||
import v3.NcdcRecordParser; | ||
|
||
//vv MaxTemperatureMapperV4 | ||
// vv MaxTemperatureMapperV4 | ||
public class MaxTemperatureMapper | ||
extends Mapper<LongWritable, Text, Text, IntWritable> { | ||
|
||
/*[*/enum Temperature { | ||
OVER_100 | ||
}/*]*/ | ||
|
||
private NcdcRecordParser parser = new NcdcRecordParser(); | ||
enum Temperature { | ||
MALFORMED | ||
} | ||
|
||
private NcdcRecordParser parser = new NcdcRecordParser(); | ||
|
||
@Override | ||
public void map(LongWritable key, Text value, Context context) | ||
throws IOException, InterruptedException { | ||
|
||
parser.parse(value); | ||
if (parser.isValidTemperature()) { | ||
int airTemperature = parser.getAirTemperature(); | ||
/*[*/if (airTemperature > 1000) { | ||
System.err.println("Temperature over 100 degrees for input: " + value); | ||
context.setStatus("Detected possibly corrupt record: see logs."); | ||
context.getCounter(Temperature.OVER_100).increment(1); | ||
}/*]*/ | ||
context.write(new Text(parser.getYear()), new IntWritable(airTemperature)); | ||
} else if (parser.isMalformedTemperature()) { | ||
System.err.println("Ignoring possibly corrupt input: " + value); | ||
context.getCounter(Temperature.MALFORMED).increment(1); | ||
} | ||
} | ||
} | ||
//^^ MaxTemperatureMapperV4 | ||
|
||
// ^^ MaxTemperatureMapperV4 |
2 changes: 1 addition & 1 deletion
2
ch05/src/main/java/v5/NcdcRecordParser.java → ch05/src/main/java/v4/NcdcRecordParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package v5; | ||
package v4; | ||
import org.apache.hadoop.io.Text; | ||
|
||
public class NcdcRecordParser { | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.