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求最大值最小值问题的更多相关文章

  1. html标签内部简单加js 一维数组求最大值 最小值两个值位置和数字金字塔图形

     html标签内部,简单加js <a href=""></a><!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  2. C++数组或vector求最大值最小值

    可以用max_element()及min_element()函数,二者返回的都是迭代器或指针. 头文件:#include<algorithm> 1.求数组的最大值或最小值 1)vector ...

  3. js求最大值最小值

    比较数组中数值的大小是比较常见的操作,比较大小的方法有多种,比如可以使用自带的sort()函数,代码如下: <html> <head> <meta charset=&qu ...

  4. Day_11【集合】扩展案例5_对list集合对象中的元素进行反转,求最大值最小值,求元素i在list集合中首次出现的索引,将oldvalue替换为newvalue

    分析以下需求,并用代码实现 定义MyArrays工具类,该工具类中有以下方法,方法描述如下: 1.public static void reverse(ArrayList<Integer> ...

  5. C++中Vector求最大值最小值

    vector<int> v: 最大值: int max = *max_element(v.begin(),v.end()); 最小值: int min = *min_element(v.b ...

  6. 【C++】Vector求最大值最小值

    最大值: int max = *max_element(v.begin(),v.end()); 最小值: int min = *min_element(v.begin(),v.end());

  7. 求最大值最小值的方法 时间复杂度O(n)

    #include<iostream> #include <iostream> #include <bitset> #include <ctime> us ...

  8. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  9. C语言:用指针求最大值和最小值

    用指针求数组最大值和最小值(10分) 题目内容: 用指针求含有十个元素的数组最大值和最小值 主函数参考 int main() { int a[10],i,maxnum,minnum; for(i=0; ...

随机推荐

  1. PyCharm2017破解版安装

    PyCharm2017破解版安装步骤: 1.右击软件压缩包选择解压到pycharm2017. 2.在解压文件夹里面找到pycharm-professional-171.3780.47,右击打开. 3. ...

  2. node.js初步总结

    一:先上一段代码 process.argv.forEach(function (val, index, array) {    console.log(index + ":" + ...

  3. Java synchronized关键字的理解

    转载自:http://blog.csdn.net/xiao__gui/article/details/8188833 在Java中,synchronized关键字是用来控制线程同步的,就是在多线程的环 ...

  4. 关于on commit preserve rows与on commit delete rows的测试

    1:先分别建立两张表 sql> CREATE GLOBAL TEMPORARY TABLE tmp_session on commit preserve rows as select * fro ...

  5. 编译hbase-1.2.3源代码

    目录 目录 1 1. 约定 1 2. 安装jdk 1 3. 安装maven 1 4. 网络配置 2 4.1. eclipse 3 4.2. maven 3 5. 从hbase官网下载源代码包: 4 6 ...

  6. _RecordsetPtr使用方法

    _variant_t vUsername,vID,vname; //变量声明 _RecordsetPtr m_pRecordset;     //记录集 CString strid; _Connect ...

  7. C++中的关键字用法--- 四种强制类型转换的总结

    四种强制类型转换的总结(const_cast.static_cast.dynamic_cast.reinterpreter_cast 1. C风格的强制类型转换(Type Cast)很简单,不管什么类 ...

  8. 配置好Nginx后,通过flume收集日志到hdfs(记得生成本地log时,不要生成一个文件,)

    生成本地log最好生成多个文件放在一个文件夹里,特别多的时候一个小时一个文件 配置好Nginx后,通过flume收集日志到hdfs 可参考flume的文件 用flume的案例二 执行的注意点 avro ...

  9. [leetcode] 16. Add Binary

    这个题目相对有点奇怪,题目如下: Given two binary strings, return their sum (also a binary string). For example, a = ...

  10. Jersey构建restful风格的WebSerivices(二)

    一. 总体说明 XML和JSON 是最为常用的数据交换格式.本例子演示如何将java对象,转成XML输出. 二.流程 1.在上文的例子中,创建一个包“com.waylau.rest.bean” 2.在 ...