分析年气象数据平均温度
Map类
package AvgTemperature_04 ;
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 java. io. IOException ;
public class MyMap extends
Mapper < LongWritable , Text , Text , IntWritable > {
private static final int MISSING = 9999 ;
@Override
protected void map ( LongWritable key, Text value, Context context) throws IOException , InterruptedException {
String line = value. toString ( ) ;
String year = line. substring ( 15 , 19 ) ;
int airTem;
if ( line. charAt ( 45 ) == '+' ) {
airTem = Integer . parseInt ( line. substring ( 46 , 50 ) ) ;
} else {
airTem = Integer . parseInt ( line. substring ( 45 , 50 ) ) ;
}
String quality = line. substring ( 50 , 51 ) ;
System . out. println ( "quality:" + quality) ;
if ( airTem != MISSING && quality. matches ( "[01459]]" ) ) {
context. write ( new Text ( year) , new IntWritable ( airTem) ) ;
}
}
}
Reduce类
package AvgTemperature_04 ;
import org. apache. hadoop. io. IntWritable ;
import org. apache. hadoop. io. Text ;
import org. apache. hadoop. mapreduce. Reducer ;
import java. io. IOException ;
public class MyReduce extends Reducer < Text , IntWritable , Text , IntWritable > {
protected void reduce ( Text key, Iterable < IntWritable > values, Context context) throws IOException , InterruptedException {
int sumValue = 0 ;
int count = 0 ;
for ( IntWritable value: values) {
sumValue += value. get ( ) ;
count++ ;
}
int avgValue = sumValue/ count;
context. write ( key, new IntWritable ( avgValue) ) ;
}
}
Job类
package AvgTemperature_04 ;
import org. apache. hadoop. conf. Configuration ;
import org. apache. hadoop. fs. Path ;
import org. apache. hadoop. io. IntWritable ;
import org. apache. hadoop. mapreduce. Job ;
import org. apache. hadoop. mapreduce. lib. input. FileInputFormat ;
import org. apache. hadoop. mapreduce. lib. output. FileOutputFormat ;
import org. apache. hadoop. io. Text ;
import java. io. IOException ;
public class TestJob {
public static void main ( String [ ] args) throws IOException , ClassNotFoundException , InterruptedException {
Configuration conf = new Configuration ( ) ;
Job job = Job . getInstance ( conf) ;
job. setJarByClass ( TestJob . class ) ;
job. setMapperClass ( MyMap . class ) ;
job. setReducerClass ( MyReduce . class ) ;
job. setOutputKeyClass ( Text . class ) ;
job. setOutputValueClass ( IntWritable . class ) ;
FileInputFormat . setInputPaths ( job, new Path ( "file:///simple/source.txt" ) ) ;
FileOutputFormat . setOutputPath ( job, new Path ( "file:///simple/output" ) ) ;
System . out. println ( job. waitForCompletion ( true ) ? 0 : 1 ) ; ;
}
}