1.MyTopN  主程序

package com.littlepage.topn;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; import java.io.IOException; public class MyTopN {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf=new Configuration(true);
String[] other=new GenericOptionsParser(conf,args).getRemainingArgs();
//设定本地环境运行,不进行集群运行
conf.set("mapreduce.framework.name","local");
//设定异构平台
conf.set("mapreduce.app-submission.cross-platform","true");
Job job=Job.getInstance(conf);
job.setJarByClass(MyTopN.class);
job.setJobName("TopN");
//核心
//map task
//input,output
TextInputFormat.addInputPath(job,new Path(other[]));
Path outPath=new Path(other[]);
if(outPath.getFileSystem(conf).exists(outPath)){
outPath.getFileSystem(conf).delete(outPath,true);
}
//map
job.setMapperClass(TopNMapper.class);
job.setMapOutputKeyClass(TopNKey.class);
job.setMapOutputValueClass(IntWritable.class);
//partitioner
//只需要满足相同的key获得相同的分区号
job.setPartitionerClass(TopNPartitioner.class);
//sortComparator
job.setSortComparatorClass(TopNSortComparator.class);
//combine //reducetask
job.setReducerClass(TopNReducer.class);
//groupingComparator
job.setGroupingComparatorClass(TopNGroupingComparator.class);
//output
TextOutputFormat.setOutputPath(job,outPath);
job.waitForCompletion(true);
}
}

2.TopNKey

package com.littlepage.topn;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; /**
* 自定义类型必须实现接口:
* 序列化/反序列化 比较器
*/ public class TopNKey implements WritableComparable<TopNKey> { private int year;
private int month;
private int day;
private int template; public int getYear() {
return year;
} public void setYear(int year) {
this.year = year;
} public int getMonth() {
return month;
} public void setMonth(int month) {
this.month = month;
} public int getDay() {
return day;
} public void setDay(int day) {
this.day = day;
} public int getTemplate() {
return template;
} public void setTemplate(int template) {
this.template = template;
} @Override
public void write(DataOutput out) throws IOException {
out.writeInt(year);
out.writeInt(month);
out.writeInt(day);
out.writeInt(template);
} @Override
public void readFields(DataInput in) throws IOException {
this.year = in.readInt();
this.month = in.readInt();
this.day = in.readInt();
this.template = in.readInt();
} @Override
public int compareTo(TopNKey that) {
int c1 = Integer.compare(this.year,that.getYear());
if(c1==){
int c2 = Integer.compare(this.month,that.getMonth());
if(c2 == ){
return Integer.compare(this.day,that.getDay());
}
return c2;
}
return c1;
}
}

3.TopNMapper

package com.littlepage.topn;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.StringUtils; import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; public class TopNMapper extends Mapper<LongWritable, Text,TopNKey, IntWritable> {
TopNKey topNKey = new TopNKey();
IntWritable intWritable = new IntWritable(); @Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//开发习惯
//value: 2019-6-1 22:22:22 1 31
String[] strs = StringUtils.split(value.toString(), '\t');
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try{
Date date = sdf.parse(strs[]);
Calendar cal=Calendar.getInstance();
cal.setTime(date);
topNKey.setYear(cal.get(Calendar.YEAR));
topNKey.setMonth(cal.get(Calendar.MONTH)+);
topNKey.setDay(cal.get(Calendar.DAY_OF_MONTH));
int template=Integer.parseInt(strs[]);
topNKey.setTemplate(template);
intWritable.set(template);
context.write(topNKey,intWritable);
}catch (ParseException e){
e.printStackTrace();
}
}
}

4.TopNReducer

package com.littlepage.topn;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException;
import java.util.Iterator; public class TopNReducer extends Reducer<TopNKey, IntWritable, Text,IntWritable> {
Text rkey=new Text();
IntWritable rval=new IntWritable();
int flag=;
int day=;
@Override
protected void reduce(TopNKey key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
Iterator<IntWritable> iter = values.iterator();
while(iter.hasNext()){
IntWritable val=iter.next();
if(flag==){
rkey.set(key.getYear()+"-"+key.getMonth()+"-"+key.getDay());
rval.set(key.getTemplate());
context.write(rkey,rval);
flag++;
day=key.getDay();
}
if(flag!=&&day!=key.getDay()){
rkey.set(key.getYear()+"-"+key.getMonth()+"-"+key.getDay());
rval.set(key.getTemplate());
context.write(rkey,rval);
break;
}
}
}
}

5.TopNPartitioner 分区规划,来划分Map之后的结果是存在哪个dn进行处理

package com.littlepage.topn;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Partitioner; public class TopNPartitioner extends Partitioner<TopNKey,IntWritable> {
@Override
public int getPartition(TopNKey key, IntWritable value, int numPartitions) {
//1.不能太复杂
//2.缩小组的维度
return key.getYear()%numPartitions;//可能会产生数据倾斜
}

}

6.TopNSortComparator 排序比较器,在Map中精确到月,按温度递减

package com.littlepage.topn;

import org.apache.hadoop.io.RawComparator;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; public class TopNSortComparator extends WritableComparator {
public TopNSortComparator(){
super(TopNKey.class,true);
} @Override
public int compare(WritableComparable a, WritableComparable b) {
TopNKey k1=(TopNKey) a;
TopNKey k2=(TopNKey) b;
//年,月,温度,且温度倒序
int c1=Integer.compare(k1.getYear(),k2.getYear());
if(c1==){
int c2=Integer.compare(k1.getMonth(),k2.getMonth());
if(c2==){
return -Integer.compare(k1.getTemplate(),k2.getTemplate());
}
return c2;
}
return c1;
}
}

7.TopNGroupingComparator 分组比较器,用于reduce的分组,每一个组是年月,进行reduce操作

package com.littlepage.topn;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator; public class TopNGroupingComparator extends WritableComparator {
public TopNGroupingComparator() {
super(TopNKey.class, true);
} @Override
public int compare(WritableComparable a, WritableComparable b) {
TopNKey k1 = (TopNKey) a;
TopNKey k2 = (TopNKey) b;
//年,月
int c1 = Integer.compare(k1.getYear(), k2.getYear());
if (c1 == ) {
return Integer.compare(k1.getMonth(), k2.getMonth());
}
return c1;
}
}

TopN案例是MapReduce的典型案例,需牢记

MapReduce TopN(自主复习)的更多相关文章

  1. MapReduce计数程序(自主复习)

    1.MyWordCount类 注意: 1.本机+测试,两个注释都放开 2.本机跑集群,要开异构平台为true 3.集群跑,把两个注释都注起来,然后在集群上面跑 package com.littlepa ...

  2. Before NOIP 2018

    目录 总结 刷题 2018 - 9 - 24 2018 - 9 - 25 2018 - 9 - 26 2018 - 9 - 27 2018 - 9 - 28 2018 - 9 - 29 2018 - ...

  3. C高级第一次PTA作业 要求三

    要求一.要求二 内容链接:http://www.cnblogs.com/X-JY/p/8550457.html 一.PTA作业中的知识点总结 1.6-1 计算两数的和与差(10 分) (1)*在程序中 ...

  4. mapreduce的cleanUp和setUp的特殊用法(TopN问题)和常规用法

    一:特殊用法 我们上来不讲普通用法,普通用法放到最后.我们来谈一谈特殊用法,了解这一用法,让你的mapreduce编程能力提高一个档次,毫不夸张!!!扯淡了,让我们进入正题: 我们知道reduce和m ...

  5. Hadoop基础-Map端链式编程之MapReduce统计TopN示例

    Hadoop基础-Map端链式编程之MapReduce统计TopN示例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.项目需求 对“temp.txt”中的数据进行分析,统计出各 ...

  6. Hadoop学习之路(二十)MapReduce求TopN

    前言 在Hadoop中,排序是MapReduce的灵魂,MapTask和ReduceTask均会对数据按Key排序,这个操作是MR框架的默认行为,不管你的业务逻辑上是否需要这一操作. 技术点 MapR ...

  7. 大数据学习——mapreduce学习topN问题

    求每一个订单中成交金额最大的那一笔  top1 数据 Order_0000001,Pdt_01,222.8 Order_0000001,Pdt_05,25.8 Order_0000002,Pdt_05 ...

  8. 大数据mapreduce全局排序top-N之python实现

    a.txt.b.txt文件如下: a.txt hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop ...

  9. hadoop mapreduce求解有序TopN(高效模式)

    1.在map阶段对数据先求解改分片的topN,到reduce阶段再合并求解一次,求解过程利用TreeMap的排序特性,不用自己写算法. 2.样板数据,类似如下 1 13682846555 192.16 ...

随机推荐

  1. PHP CI 框架简单使用(二)

    我们简单认识一下CI框架的MVC.示例代码如下 //CI控制器文件Home.php <?php defined('BASEPATH') OR exit('No direct script acc ...

  2. 清北学堂2019.8.10 & 清北学堂2019.8.11 & 清北学堂2019.8.12

    Day 5 杨思祺(YOUSIKI) 今天的难度逐渐上升,我也没做什么笔记 开始口胡正解 今天的主要内容是最小生成树,树上倍增和树链剖分 最小生成树 Prim 将所有点分为两个集合,已经和点 1 连通 ...

  3. EventChannel 原生向Flutter传递数据

    目的:原生页面主动向Flutter页面传递信息 1 flutter步骤 定义EventChannel static const EventChannel eventChannel = EventCha ...

  4. 回归_最小二乘法(python脚本实现)

     python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...

  5. CentOS 7.x关闭/开启防火墙出现Unit iptables.service failed to load: No such file or directory问题解决

    一直用CentOS 6.x,今天用CentOS7.3版本时,防火墙配置后执行service iptables start出现”Failed to restart iptables.service: U ...

  6. Linux监控命令之==>ps

    一.命令说明 ps 命令是最基本同时也是非常强大的进程查看命令.使用该命令可以确定有哪些进程正在运行和运行的状态.进程是否结束.进程有没有僵死.哪些进程占用了过多的资源等等.总之大部分信息都是可以通过 ...

  7. Function Expression

    One of the key characteristics of function declarations is function declaration hoisting, whereby fu ...

  8. 关机报 at-spi-bus-launcher

    查看是否有autostart文件夹,有则不建立 mkdir -v ~/.config/autostart 可直接修改/etc/xdg/autostart/at-spi-dbus-bus.desktop ...

  9. java:JavaScript3(innerHTML,post和get,单选框,多选框,下拉列表值得获取,JS中的数组,JS中的正则)

    1.innerHTML用户登录验证: <!DOCTYPE> <html> <head> <meta charset="UTF-8"> ...

  10. 【JAVA系列】Google爬虫如何抓取JavaScript的?

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[JAVA系列]Google爬虫如何抓取Java ...