Skip to content

Commit

Permalink
Get AvroSpecificMaxTemperature test passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhite committed Jan 15, 2012
1 parent ca7ee34 commit 2e2ca8b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
@@ -0,0 +1 @@
hadoop jar avro-examples.jar AvroSpecificMaxTemperature input/ncdc/sample.txt output
Binary file not shown.
33 changes: 27 additions & 6 deletions ch04-avro/src/main/java/AvroSpecificMaxTemperature.java
Expand Up @@ -18,6 +18,8 @@
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import specific.WeatherRecord;

public class AvroSpecificMaxTemperature extends Configured implements Tool {

public static class MaxTemperatureMapper
Expand Down Expand Up @@ -54,15 +56,33 @@ public void reduce(Integer key, Iterable<WeatherRecord> values,
}
collector.collect(max);
}
private WeatherRecord newWeatherRecord(WeatherRecord value) {
WeatherRecord record = new WeatherRecord();
record.year = value.year;
record.temperature = value.temperature;
record.stationId = value.stationId;
return record;
}

public static class MaxTemperatureCombiner extends
AvroReducer<Integer, WeatherRecord, Pair<Integer, WeatherRecord>> {

@Override
public void reduce(Integer key, Iterable<WeatherRecord> values,
AvroCollector<Pair<Integer, WeatherRecord>> collector,
Reporter reporter) throws IOException {
WeatherRecord max = null;
for (WeatherRecord value : values) {
if (max == null || value.temperature > max.temperature) {
max = newWeatherRecord(value);
}
}
collector.collect(new Pair<Integer, WeatherRecord>(key, max));
}
}

private static WeatherRecord newWeatherRecord(WeatherRecord value) {
WeatherRecord record = new WeatherRecord();
record.year = value.year;
record.temperature = value.temperature;
record.stationId = value.stationId;
return record;
}

@Override
public int run(String[] args) throws Exception {
if (args.length != 2) {
Expand All @@ -86,6 +106,7 @@ public int run(String[] args) throws Exception {
conf.setInputFormat(AvroUtf8InputFormat.class);

AvroJob.setMapperClass(conf, MaxTemperatureMapper.class);
AvroJob.setCombinerClass(conf, MaxTemperatureCombiner.class);
AvroJob.setReducerClass(conf, MaxTemperatureReducer.class);

JobClient.runJob(conf);
Expand Down
1 change: 1 addition & 0 deletions ch04-avro/src/main/resources/WeatherRecord.avsc
@@ -1,6 +1,7 @@
{
"type": "record",
"name": "WeatherRecord",
"namespace": "specific",
"doc": "A weather reading.",
"fields": [
{"name": "year", "type": "int"},
Expand Down

0 comments on commit 2e2ca8b

Please sign in to comment.