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; ...
随机推荐
- 17)maven-surefire-plugin
http://maven.apache.org/surefire/maven-surefire-plugin/ Goals Overview The Surefire Plugin has only ...
- faceswap安装说明
Installing Faceswap Installing Faceswap Prerequisites Hardware Requirements Supported operating syst ...
- css布局:定宽,自适应
css三栏布局:1.中自:float,absolute,margin三种方法.2.中固:margin,table两种方法. 两边定宽,中间自适应: float: #left{ float:left; ...
- 【PAT Advanced Level】1015. Reversible Primes (20)
转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...
- kafka不停止服务的情况下修改日志保留时间
kafka配置文件如下: broker.id=1 port=9092 host.name=ssy-kafka1 num.network.threads=4 num.io.threads=8 socke ...
- Java泛型与Restlet客户端
写一个与restlet服务器通信的客户端类,用于测试通信是否成功,并且进行交互.为了方便其他人使用,于是,写一个通用的方法封装起来,可是中途却放生了一些问题. 按照正常写法,顺序走下来是这样的: pu ...
- 开源项目之ASP.NET Core + Vue.js 的前后端分离的通用后台管理系统框架
年前看了这个开源项目感觉很不错,这个小项目对于传统的.net 开发人员,想做技术提升是一个很不错的参考案例. 开源项目演示地址:https://dnczeus.codedefault.com/logi ...
- C#中的split的基本用法
split的使用: 1.使用char()字符分隔:根据单个的char()类型的进行分隔 代码如下: string str="e2kdk2fjod2fiksf21"; ');//因为 ...
- C#treeView控件单击事件选中节点滞后问题解决方法
问题描述:在treeView的Click事件中,选中的节点SelectedNode并不是您刚才点击的节点,总是上一次选中的节点,节点选中滞后的问题. 解决方案:在treeView的MouseDown事 ...
- C#检测并安装https站点的数字证书,CefSharp和HttpWebRequest通过会话Cookie实现自动登录访问https站点
HttpUtil工具类: using System; using System.Collections.Generic; using System.IO; using System.Linq; usi ...