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. leetcode-easy-string-344 Reverse String

    mycode class Solution(object): def reverseString(self, s): """ :type s: List[str] :rt ...

  2. 安装源配置文件“/etc/apt/sources.list”问题

    安装docker过程中使用以下命令设置稳定存储库. $ sudo add-apt-repository \ "deb [arch=amd64] https://download.docker ...

  3. 算法 - DNA搜索 - Ako Corasick

    场景:从很长的字符串(输入字符串.DNA)中搜索大量固定字符串(字典.基因) 题目:Determining DNA Health | HackerRank 算法:Aho–Corasick algori ...

  4. BS架构和CS架构

    B:browser  浏览器 S:server       服务器 C:client        客户端 BS:浏览器和服务器的关系,通过浏览器来访问服务器.比如:新浪.百度.等等. 优点:只要有浏 ...

  5. python-静态方法和类方法及其使用场景

    静态方法和类方法 静态方法 我们在类中定义的方法都是对象方法,也就是说这些方法都是发送给对象的消息.实际上,我们写在类中的方法并不需要都是对象方法,例如我们定义一个"三角形"类,通 ...

  6. Python_List对象内置方法详解

    目录 目录 前言 软件环境 列表List 修改列表的元素 插入列表元素 extend 将序列中的元素迭代的附加到list中 insert 在指定的索引号中插入一个元素 删除列表元素 del 删除Lis ...

  7. resultMap的使用总结

    Mybatis:resultMap的使用总结   resultMap是Mybatis最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中. resultMap包含的元素 ...

  8. python3.6+RF环境搭建

    现在大家都在用python3了,利用这个机会正好把自己的练习重新整理一遍,本篇记录用python3.6重新搭建关键字驱动环境 目录 1.安装python3.6 2.安装wxPython 3.安装rob ...

  9. Pytorch笔记 (3) 科学计算1

    一.张量 标量 可以看作是  零维张量 向量 可以看作是  一维张量 矩阵 可以看作是  二维张量 继续扩展数据的维度,可以得到更高维度的张量 ————>  张量又称 多维数组 给定一个张量数据 ...

  10. jenkins自动化部署工具

    jenkins自动化测试 & 持续集成 知识点: 1.下载地址:jenkins.io download: