MapReduce求最大值最小值问题
import java.io.File;
import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobConf;
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 GetMinMaxKeyMapReduce { public static class GetMinMaxKeyMap extends Mapper<Object, Text, Text,Text> {
private Text min = new Text();
private Text max = new Text();
private Long i = new Long(0);
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] strs = value.toString().split("\t");
if (strs!=null && strs.length>5 &&strs[3].length() > 20 && strs[3].indexOf(" ") == -1 && strs[3].indexOf("=") == -1) {
if(i==0){
min= new Text(strs[3]);
max= new Text(strs[3]);
}
if(strs[3].compareTo(min.toString())<0){
min=new Text(strs[3]);
}
if(strs[3].compareTo(max.toString())>0){
max=new Text(strs[3]);
}
i++;
}
} @Override
protected void cleanup(Context context) throws IOException, InterruptedException {
context.write(new Text("min"), min);
context.write(new Text("max"), max);
}
} public static class GetMinMaxKeyReducer extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
String result ="";
for (Text value : values) {
if(result.equals("")){
result = value.toString();
}
if (("min").equals(key.toString())) {
if(value.toString().compareTo(result)<0){
result=value.toString();
}
} else if (("max").equals(key.toString())) {
if(value.toString().compareTo(result)>0){
result=value.toString();
}
} else {
System.err.println("未知reduce 输入key:" + key.toString());
}
}
context.write(key, new Text(result));
}
} public static void main(String[] args) throws Exception {
File jarFile = EJob.createTempJar("bin");
ClassLoader classLoader = EJob.getClassLoader();
Thread.currentThread().setContextClassLoader(classLoader); //Hadoop 运行环境
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "bfdbjc1:12001");; //任务参数设置
Job job = new Job(conf, "GetMinMaxKey"); job.setJarByClass(GetMinMaxKeyMapReduce.class);
job.setMapperClass(GetMinMaxKeyMap.class);
job.setReducerClass(GetMinMaxKeyReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path("hdfs://bfdbjc1:12000/user/work/tables2/raw_kafka/l_date=2013-09-15"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://bfdbjc1:12000/user/work/output/minmaxkey/")); //Eclipse 本地提交
((JobConf) job.getConfiguration()).setJar(jarFile.toString()); //等待任务运行完成
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
MapReduce求最大值最小值问题的更多相关文章
- html标签内部简单加js 一维数组求最大值 最小值两个值位置和数字金字塔图形
html标签内部,简单加js <a href=""></a><!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...
- C++数组或vector求最大值最小值
可以用max_element()及min_element()函数,二者返回的都是迭代器或指针. 头文件:#include<algorithm> 1.求数组的最大值或最小值 1)vector ...
- js求最大值最小值
比较数组中数值的大小是比较常见的操作,比较大小的方法有多种,比如可以使用自带的sort()函数,代码如下: <html> <head> <meta charset=&qu ...
- Day_11【集合】扩展案例5_对list集合对象中的元素进行反转,求最大值最小值,求元素i在list集合中首次出现的索引,将oldvalue替换为newvalue
分析以下需求,并用代码实现 定义MyArrays工具类,该工具类中有以下方法,方法描述如下: 1.public static void reverse(ArrayList<Integer> ...
- C++中Vector求最大值最小值
vector<int> v: 最大值: int max = *max_element(v.begin(),v.end()); 最小值: int min = *min_element(v.b ...
- 【C++】Vector求最大值最小值
最大值: int max = *max_element(v.begin(),v.end()); 最小值: int min = *min_element(v.begin(),v.end());
- 求最大值最小值的方法 时间复杂度O(n)
#include<iostream> #include <iostream> #include <bitset> #include <ctime> us ...
- POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53703 Accepted: 25237 ...
- C语言:用指针求最大值和最小值
用指针求数组最大值和最小值(10分) 题目内容: 用指针求含有十个元素的数组最大值和最小值 主函数参考 int main() { int a[10],i,maxnum,minnum; for(i=0; ...
随机推荐
- 解决以showModalDialog打开的页面在提交表单时弹出新窗口的问题
源代码如下: 父页面: window.showModalDialog("../readfile/readFile.jsp","","dialogWid ...
- android周期性任务
一般任务调度机制的实现方式主要有: Thread sleep.Timer.ScheduledExecutor.Handler和其他第三方开源库.android的AlarmManager 1. Time ...
- Android学习整理之Activity篇
一.Activity概念介绍 activity属于android的四大组件之一(其他的三个: Content provider,Broadcast receiver,Service),它可以理解为一个 ...
- ACL授权实例
上一篇关于ACL的文章中:位运算实现ACL授权与认证过程的原理解析,我们学习了通过位运算实现ACL授权与认证的原理核心,今天我们一起来看授权的实例. 实现的功能很简单:打开授权界面时,加载已授权信息. ...
- 一起学习MVC(4)Controllers的学习
控制器Controllers Controllers为控制器文档,AccountControllers内的方法对应View→Account下的cshtml文件. 我们看到Aco ...
- C# 动态生成Html地图文件
public void GPSModel(string x, string y, string ss)//动态地图文件 { if (x.Contains("-") &&am ...
- Python3.5 学习十
多进程: 多线程和多进程的区别: Python多线程不适合CPU操作密集型的任务,适合IO操作密集型的任务(IO操作不占用CPU) Python折中解决多线程不能真正同步运算的方案是:起多个进程,每个 ...
- jzoj3519
我們考慮將一個節點x的所有兒子的數都改成y 記lcm[x]表示一個點的所有子節點的lcm值 那麼我們會發現y*deg[x] 要被lcm[x]整除 那麼x就會比x所有子節點最小的數小,記為z 那麼x就是 ...
- C++类模板 template <class T>
C++在发展的后期增加了模板(template )的功能,提供了解决这类问题的途径.可以声明一个通用的类模板,它可以有一个或多个虚拟的类型参数. 比如: class Compare_int class ...
- PARSER_JS_PRECISION_RANGE_EXCEEDED 错误
{ [Error: parseLengthCodedNumber: JS precision range exceeded, number is >= 53 bit: "3037620 ...