简单的MapReduce例子,从一批数据中获取每年的最高温度
2013-04-22 15:15
357 查看
样例数据只有年份和温度,用MapReduce来取每年的最高温度。
/* MaxTemperature.java */
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class MaxTemperature {
static class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
// 从每行的数据中分解出年和温度
String year = line.substring(0, 4);
int airTemperature = Integer.parseInt(line.substring(5, 8));
context.write(new Text(year), new IntWritable(airTemperature));
}
}
static class MaxTemperatureReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int maxValue = Integer.MIN_VALUE;
// key为年份, values为该年每天的温度,求出最大的温度值
for (IntWritable value : values) {
maxValue = Math.max(maxValue, value.get());
}
context.write(key, new IntWritable(maxValue));
}
}
public static void main(String[] args) throws Exception {
if(args.length !=2) {
System.err.println("Usage: MaxTemperature <input path> <output path>");
System.exit(-1);
}
Job job = new Job();
job.setJarByClass(MaxTemperature.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(MaxTemperatureMapper.class);
job.setReducerClass(MaxTemperatureReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true)?0:1);
}
}
打包在hadoop环境下运行,结果的输出会在output的目录下。
hadoop jar MaxTemperature.jar MaxTemperature sample.txt output
/* MaxTemperature.java */
import java.io.IOException;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class MaxTemperature {
static class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
// 从每行的数据中分解出年和温度
String year = line.substring(0, 4);
int airTemperature = Integer.parseInt(line.substring(5, 8));
context.write(new Text(year), new IntWritable(airTemperature));
}
}
static class MaxTemperatureReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int maxValue = Integer.MIN_VALUE;
// key为年份, values为该年每天的温度,求出最大的温度值
for (IntWritable value : values) {
maxValue = Math.max(maxValue, value.get());
}
context.write(key, new IntWritable(maxValue));
}
}
public static void main(String[] args) throws Exception {
if(args.length !=2) {
System.err.println("Usage: MaxTemperature <input path> <output path>");
System.exit(-1);
}
Job job = new Job();
job.setJarByClass(MaxTemperature.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(MaxTemperatureMapper.class);
job.setReducerClass(MaxTemperatureReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
System.exit(job.waitForCompletion(true)?0:1);
}
}
打包在hadoop环境下运行,结果的输出会在output的目录下。
hadoop jar MaxTemperature.jar MaxTemperature sample.txt output
相关文章推荐
- 简单的ajax数据传输和获取例子
- 1个比较简单的使用java反射机制获取前台数据进行数据封装的例子
- Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据
- ssh框架ajax获取后台list数据简单例子
- 简单的获取加速传感器数据的例子
- 【转载】Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据,作者:Jaiky_杰哥
- ashx获取处理数据的简单例子
- ashx获取处理数据的简单例子
- vue.js配合$.post从后台获取数据简单demo分享
- easyui-combobox 下拉列表的JSON获取数据例子
- 简单的异步获取网络数据
- Android 解析后台返回为Json数据的简单例子!!!
- Android 解析后台返回为Json数据的简单例子!!!
- Js中如何获取div盒子之下的元素(简单的例子)
- el获取简单数据和复杂数据
- 用SharedPreferences存储数据,一个最简单的例子
- Android 解析后台返回为Json数据的简单例子
- java获取redis中各种数据类型key对应的value代码简单封装
- c# Windows Forms Application中的DataGridView的数据指定列绑定 简单小例子
- C# Socket实现通过公共服务程序获取数据的例子