-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMRtest.java
More file actions
78 lines (68 loc) · 2.94 KB
/
MRtest.java
File metadata and controls
78 lines (68 loc) · 2.94 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class MRtest{
private static Logger logger = Logger.getLogger(MRtest.class);
public static class TempMap extends Mapper<LongWritable,Text,Text,IntWritable>{//<keyin,valuein,keyout,valueout>
public void map(LongWritable key, Text value, Context context) throws
IOException, InterruptedException{
String line = value.toString();
String year = line.substring(0,4);
int temperature = Integer.parseInt(line.substring(8));
context.write(new Text(year), new IntWritable(temperature));
}
}
public static class TempReduce extends Reducer<Text, IntWritable,Text,IntWritable>{
public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException,InterruptedException{
int maxValue = Integer.MIN_VALUE;
StringBuffer sb = new StringBuffer();
for(IntWritable value : values){
maxValue = Math.max(maxValue, value.get());
sb.append(value).append(",");
}
context.write(key, new IntWritable(maxValue));
}
}
public static void main(String[] args) throws Exception{
String dst = "hdfs://namenode:9000/user/input";
String dstout = "hdfs://namenode:9000/user/output";
Configuration conf = new Configuration();
Job job = new Job(conf,"MRtest");
FileInputFormat.addInputPath(job, new Path(dst));
FileOutputFormat.setOutputPath(job, new Path(dstout));
job.setJarByClass(MRtest.class);
job.setMapperClass(TempMap.class);
job.setReducerClass(TempReduce.class);
job.setCombinerClass(TempReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.waitForCompletion(true);
SimpleLayout layout = new SimpleLayout();
FileAppender appender = null;
try{
appender = new FileAppender(layout,"output.txt",false);
}catch(Exception e){}
logger.addAppender(appender);
logger.setLevel((Level)Level.WARN);//选择不同的日志级别,不同级别影响输出
// 记录debug级别的信息
logger.debug("This is debug message.");
// 记录info级别的信息
logger.info("This is info message.");
// 记录error级别的信息
logger.error("This is error message.");
System.out.println("Finished");
}
}