-
Notifications
You must be signed in to change notification settings - Fork 10
/
MaxTempMapper.java
56 lines (45 loc) · 1.6 KB
/
MaxTempMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package hadoop;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import java.io.*;
import javax.servlet.*;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.RemoteIterator;
public class MaxTempMapper extends Mapper <LongWritable, Text, Text, IntWritable>
{
private static final int MISSING = 9999;
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException
{
String line = value.toString();
//this is all lines content
String date = line.substring(19,23);
//extract years
int airTemp = parseTemp(line);
String quality = line.substring(92,93);
boolean isRecord = airTemp != MISSING && quality.matches("[01459]");
if( isRecord ){
//output k2,v2 as year,temp
context.write(new Text(date), new IntWritable(airTemp));
}
//
}
private int parseTemp(String line) {
int airTemp;
if(line.charAt(87) == '+')
{
//parse positive
airTemp = Integer.parseInt(line.substring(88,92));
}
else {
airTemp = Integer.parseInt(line.substring(87,92));
}
return airTemp;
}