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; ...
随机推荐
- jdk10运行springboot项目出现:Type javax.xml.bind.JAXBContext not present
项目由openjdk8.0迁移到jdk10导致的 原因:java9模块化的概念使得JAXB默认没有加载: jaxb-api是存在jdk中的,只是默认没有加载而已,手动引入即可. 推荐方式: <! ...
- Windows could not set the offline local information.Error code:0X80000001解决方法
我的笔记本是联想Y460(白色) 昨天在重装系统的时候遇到如下错误:Windows could not set the offline local information.Error code:0X8 ...
- MFC 怎样获得某个窗口的句柄?
GetSafeHandle();this-> hWnd;GetDlgItem(hwnd,ID);//获取窗口ID所对应的HWND的子窗口句柄 在主窗口中,如果要用到父窗口的句柄,可以用 HWND ...
- C实现一个NTP客户端,可以从指定IP的NTP服务器获取时间戳
参考::https://blog.csdn.net/dosthing/article/details/81588219 下面的代码测试通过 头文件 #include <sys/types.h&g ...
- hdu 4995 离线处理+模拟
http://acm.hdu.edu.cn/showproblem.php?pid=4995 给定一维坐标下的n个点,以及每个点的权值,有m次查询,每次将查询的x点上的权值修改为离x最近的k个点权值的 ...
- Spring注解使用和与配置文件的关系
Spring注解使用和与配置文件的关系 1 注解概述与容器管理机制 Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repositor ...
- hibernate 一对多,由谁维护性能最优
举例如下 Customer类: public class Customer { private int id; private String name; private Set orders = ne ...
- 移动端与PC端的viewport
第一种解析: 设备像素,就是我们直觉上觉得"靠谱"的像素,这些像素为所使用的各种设备提供了正规的分辨率,并且其值可以通过(通常情况下)从screen.width/height属性中 ...
- ASP.NET MVC Ajax 伪造请求
1.前言 CSRF(Cross-site request forgery)跨站请求伪造,ASP.NET MVC 应用通过使用AJAX请求来提升用户体验,浏览器开发者工具可以一览众山小,就很容易伪造了请 ...
- 用idea做springboot开发,设置thymeleaf时候,新手容易忽略误区
最近小编因为工作原因需要完成工厂自动化改造,而思来想去觉得还是用Java开发,因为很久没有敲过代码,对java这块已经抛掉很多年.作为工厂自动开发或者大型企业级开发,个人认为java和C#会比较合适, ...