mapreduce 实现数子排序
设计思路:
使用mapreduce的默认排序,按照key值进行排序的,如果key为封装int的IntWritable类型,那么MapReduce按照数字大小对key排序,如果key为封装为String的Text类型,那么MapReduce按照字典顺序对字符串排序。
首先map阶段将输入的数字作为key, 并记录相同key出现的次数,在reduce阶段将输入的key作为输出的value,如果相同值存在多个,循环便利输出。
源数据:file1
2
32
654
32
15
756
65223
file2
5956
22
650
92
file3
26
54
6
2
15
源代码
package com.duking.hadoop; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; public class DataSort { public static class Map extends
Mapper<Object, Text, IntWritable, IntWritable> { private static IntWritable data = new IntWritable(); // 实现map函数
public void map(Object key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); // 将输入的每一行数据转换为String类型 data.set(Integer.parseInt(line)); // 将String 转换为Integer context.write(data, new IntWritable(1)); // 将 date->key
// 统计key出现的次数自增为value
}
} // reduce将输入中的key复制到输出数据的key上,并直接输出 这是数据区重的思想
public static class Reduce extends
Reducer<IntWritable, IntWritable, IntWritable, IntWritable> { private static IntWritable linenum = new IntWritable(1);
private IntWritable result = new IntWritable(); // 实现reduce函数 public void reduce(IntWritable key, Iterable<IntWritable> values, //Iterable转为List
Context context) throws IOException, InterruptedException { for (IntWritable val : values) { context.write(linenum, key); linenum = new IntWritable(linenum.get() + 1);
}
} } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); conf.set("mapred.job.tracker", "192.168.60.129:9000"); // 指定带运行参数的目录为输入输出目录
String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs(); /*
* 指定工程下的input2为文件输入目录 output2为文件输出目录 String[] ioArgs = new String[] {
* "input2", "output2" };
*
* String[] otherArgs = new GenericOptionsParser(conf, ioArgs)
* .getRemainingArgs();
*/ if (otherArgs.length != 2) { // 判断路径参数是否为2个 System.err.println("Usage: Data Deduplication <in> <out>"); System.exit(2); } // set maprduce job name
Job job = new Job(conf, "Data sort"); job.setJarByClass(DataSort.class); // 设置Map、Combine和Reduce处理类 job.setMapperClass(Map.class); job.setCombinerClass(Reduce.class); job.setReducerClass(Reduce.class); // 设置输出类型 job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(IntWritable.class); // 设置输入和输出目录 FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
结果
1 2
2 2
3 6
4 15
5 15
6 22
7 26
8 32
9 32
10 54
11 92
12 650
13 654
14 756
15 5956
16 65223
mapreduce 实现数子排序的更多相关文章
- Hadoop学习笔记—11.MapReduce中的排序和分组
一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...
- Hadoop学习笔记: MapReduce二次排序
本文给出一个实现MapReduce二次排序的例子 package SortTest; import java.io.DataInput; import java.io.DataOutput; impo ...
- (转)MapReduce二次排序
一.概述 MapReduce框架对处理结果的输出会根据key值进行默认的排序,这个默认排序可以满足一部分需求,但是也是十分有限的.在我们实际的需求当中,往往有要对reduce输出结果进行二次排序的需求 ...
- 详细讲解MapReduce二次排序过程
我在15年处理大数据的时候还都是使用MapReduce, 随着时间的推移, 计算工具的发展, 内存越来越便宜, 计算方式也有了极大的改变. 到现在再做大数据开发的好多同学都是直接使用spark, hi ...
- mapreduce数据处理——统计排序
接上篇https://www.cnblogs.com/sengzhao666/p/11850849.html 2.数据处理: ·统计最受欢迎的视频/文章的Top10访问次数 (id) ·按照地市统计最 ...
- MapReduce二次排序
默认情况下,Map 输出的结果会对 Key 进行默认的排序,但是有时候需要对 Key 排序的同时再对 Value 进行排序,这时候就要用到二次排序了.下面让我们来介绍一下什么是二次排序. 二次排序原理 ...
- Hadoop MapReduce 二次排序原理及其应用
关于二次排序主要涉及到这么几个东西: 在0.20.0 以前使用的是 setPartitionerClass setOutputkeyComparatorClass setOutputValueGrou ...
- MapReduce中的排序
hadoop的计算模型就是map/reduce,每一个计算任务会被分割成很多互不依赖的map/reduce计算单元,将所有的计算单元执行完毕后整个计算任务就完成了.因为计算单元之间互不依 ...
- hadoop2.2.0 MapReduce求和并排序
javabean必须实现WritableComparable接口,并实现该接口的序列化,反序列话和比较方法 package com.my.hadoop.mapreduce.sort; import j ...
随机推荐
- 关于错误处理程序中【return】的用法
先让俺这位新人帮各位有幸游览到我博客文章的叔叔阿姨哥哥姐姐们解释一下什么是错误处理?即:当程序发生错误时,保证程序不会异常中断的机制. 那么为什么程序中会有错误处理呢?像我们通常无论是玩手机或者玩游戏 ...
- 1804 小C的多边形
1804 小C的多边形 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 小C偶然发现了一个奇妙的n个点的多边形.现在你需要给外圈的边标记上1~n-1,里圈的边也标记 ...
- UESTC 491 Tricks in Bits
Tricks in Bits Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit ...
- Centos7安装zookeeper
1.进入/opt cd /opt 2.下载 zookeeper-3.4.10.tar.gz: wget https://mirrors.tuna.tsinghua.edu.cn/apache/zook ...
- Git 命令行帮助
Git usage: git [--version] [--help] [-C <path>] [-c name=value] [--exec-path[=<path>]] [ ...
- Django 请求生命周期【图示】
Django 请求生命周期
- 设计一个算法,採用BFS方式输出图G中从顶点u到v的最短路径(不带权的无向连通图G採用邻接表存储)
思想:图G是不带权的无向连通图.一条边的长度计为1,因此,求带顶点u和顶点v的最短的路径即求顶点u和顶点v的边数最少的顶点序列.利用广度优先遍历算法,从u出发进行广度遍历,类似于从顶点u出发一层一层地 ...
- Selenium chrome配置不加载图片
from selenium import webdriver chrome_options = webdriver.ChromeOptions() prefs = {"profile.man ...
- JavaWeb—拦截器Interceptor
1.概念 java里的拦截器是动态拦截Action调用的对象,它提供了一种机制可以使开发者在一个Action执行的前后执行一段代码,也可以在一个Action执行前阻止其执行,同时也提供了一种可以提取A ...
- Vue-router2.0学习笔记(转)
转:https://segmentfault.com/a/1190000007825106 Vue.js的一大特色就是构建单页面应用十分方便,既然要方便构建单页面应用那么自然少不了路由,vue-rou ...