Hadoop入门(十五)Mapreduce的数据排序程序

"数据排序"是许多实际任务执行时要完成的第一项工作,比如学生成绩评比、数据建立索引等。这个实例和数据去重类似,都是先对原始数据进行初步处理,为进一步的数据操作打好基础

1 实例描述

对输入文件中数据进行排序。输入文件中的每行内容均为一个数字,即一个数据。要求在输出中每行有两个间隔的数字,其中,第一个代表原始数据在原始数据集中的位次,第二个代表原始数据。
样例输入如下所示: 
1)file1  

2
32
654
32
15
756
65223

2)file2  

5956
22
650
92

3)file3

26
54
6

期望输出:

1    2
2    6
3    15
4    22
5    26
6    32
7    32
8    54
9    92
10    650
11    654
12    756
13    5956
14    65223

 

2 问题分析

这个实例仅仅要求对输入数据进行排序

分析:
   MapReduce过程中就有排序,它的默认排序规则按照key值进行排序的,如果key为封装int的IntWritable类型,那么MapReduce按照数字大小对key排序,如果key为封装为String的Text类型,那么MapReduce按照字典顺序对字符串排序。
 
使用封装int的IntWritable型数据结构了。也就是在map中将读入的数据转化成IntWritable型,然后作为key值输出(value任意)。reduce拿到<key,value-list>之后,将输入的key作为value输出,并根据value-list中元素的个数决定输出的次数。输出的key(即代码中的linenum)是一个全局变量,它统计当前key的位次。需要注意的是这个程序中没有配置Combiner,也就是在MapReduce过程中不使用Combiner。这主要是因为使用map和reduce就已经能够完成任务了。

 

3.实现步骤

  1. 在map中将读入的数据转化成IntWritable型,然后作为key值输出(value任意)。 
  2. reduce拿到<key,value-list>之后,将输入的key作为value输出,并根据value-list中元素的个数决定输出的次数
  3. 输出的key是一个全局变量,它统计当前key的位次
     

 

4.关键代码

正序:

package com.mk.mapreduce;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;
import java.net.URI;public class Sort {public static class SortMapper extends Mapper<LongWritable, Text, IntWritable, IntWritable> {@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {IntWritable v = new IntWritable(Integer.parseInt(value.toString().trim()));context.write(v, new IntWritable(1));}}public static class SortReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {int count = 1;@Overrideprotected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {for (IntWritable v: values) {context.write(new IntWritable(count ++), key);}}}public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {String uri = "hdfs://192.168.150.128:9000";String input = "/sort/input";String output = "/sort/output";Configuration conf = new Configuration();if(System.getProperty("os.name").toLowerCase().contains("win"))conf.set("mapreduce.app-submission.cross-platform","true");FileSystem fileSystem = FileSystem.get(URI.create(uri), conf);Path path = new Path(output);fileSystem.delete(path,true);Job job = new Job(conf,"Sort");job.setJar("./out/artifacts/hadoop_test_jar/hadoop-test.jar");job.setJarByClass(Sort.class);job.setMapperClass(SortMapper.class);job.setReducerClass(SortReducer.class);job.setMapOutputKeyClass(IntWritable.class);job.setMapOutputValueClass(IntWritable.class);job.setOutputKeyClass(IntWritable.class);job.setOutputValueClass(IntWritable.class);FileInputFormat.addInputPaths(job, uri + input);FileOutputFormat.setOutputPath(job, new Path(uri + output));boolean ret = job.waitForCompletion(true);System.out.println(job.getJobName() + "-----" +ret);}
}

 

 

逆序:

package com.mk.mapreduce;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
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;import java.io.IOException;
import java.net.URI;public class Sort {public static class SortMapper extends Mapper<LongWritable, Text, IntWritable, IntWritable> {@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {IntWritable v = new IntWritable(Integer.parseInt(value.toString().trim()));context.write(v, new IntWritable(1));}}public static class SortReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {int count = 1;@Overrideprotected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {for (IntWritable v: values) {context.write(new IntWritable(count ++), key);}}}public static class SortComparator implements RawComparator<IntWritable> {@Overridepublic int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {return IntWritable.Comparator.compareBytes(b2, s2, l2, b1, s1, l1);}@Overridepublic int compare(IntWritable o1, IntWritable o2) {return o2.get() - o1.get();}}public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {String uri = "hdfs://192.168.150.128:9000";String input = "/sort/input";String output = "/sort/output";Configuration conf = new Configuration();if(System.getProperty("os.name").toLowerCase().contains("win"))conf.set("mapreduce.app-submission.cross-platform","true");FileSystem fileSystem = FileSystem.get(URI.create(uri), conf);Path path = new Path(output);fileSystem.delete(path,true);Job job = new Job(conf,"Sort");job.setJar("./out/artifacts/hadoop_test_jar/hadoop-test.jar");job.setJarByClass(Sort.class);job.setMapperClass(SortMapper.class);job.setReducerClass(SortReducer.class);job.setMapOutputKeyClass(IntWritable.class);job.setMapOutputValueClass(IntWritable.class);job.setOutputKeyClass(IntWritable.class);job.setOutputValueClass(IntWritable.class);FileInputFormat.addInputPaths(job, uri + input);FileOutputFormat.setOutputPath(job, new Path(uri + output));job.setSortComparatorClass(SortComparator.class);boolean ret = job.waitForCompletion(true);System.out.println(job.getJobName() + "-----" +ret);}
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/322988.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java我来了

引言 原本我也是个计算机的小白&#xff0c;从今年的二月份开始学习java&#xff0c;接触eclipse&#xff0c;终于算是开启了我的编程之旅。 在此之前我也不算是零基础&#xff0c;学校在上学期还未开设c语言课程的时候&#xff0c;我就自学了c语言&#xff0c;学的不深&#x…

jzoj4208-线段树什么的最讨厌了【dfs】

正题 题目大意 一个0∼n0\sim n0∼n的线段树包含l∼rl\sim rl∼r的区间&#xff0c;求最小的nnn 解题思路 dfsdfsdfs一下&#xff0c;从下面开始往上扩展区间&#xff0c;知道变成0∼x0\sim x0∼x的格式就就可以去最小值了。 记得剪枝 codecodecode #include<cstdio> …

Hadoop入门(十六)Mapreduce的单表关联程序

"单表关联"要求从给出的数据中寻找所关心的数据&#xff0c;它是对原始数据所包含信息的挖掘 1 实例描述 给出child-parent&#xff08;孩子——父母&#xff09;表&#xff0c;要求输出grandchild-grandparent&#xff08;孙子——祖父母&#xff09;表 样例输入&…

ASP.NET Core集成现有系统认证

我们现在大多数转向ASP.NET Core来使用开发的团队&#xff0c;应该都不是从0开始搭建系统&#xff0c;而是老的业务系统已经在运行&#xff0c;ASP.NET Core用来开发新模块。那么解决用户认证的问题&#xff0c;成为我们的第一个拦路虎。 认证与授权 什么是认证&#xff1f; …

Hadoop入门(十七)Mapreduce的多表关联程序

多表关联和单表关联类似&#xff0c;它也是通过对原始数据进行一定的处理&#xff0c;从其中挖掘出关心的信息 1 实例描述 输入是两个文件&#xff0c;一个代表工厂表&#xff0c;包含工厂名列和地址编号列&#xff1b;另一个代表地址表&#xff0c;包含地址名列和地址编号列…

jzoj4209-已经没有什么好害怕的了【差分】

正题 题目大意 ansians_iansi​表示包含字符iii的括号匹配子串个数 求∑i1n(ansi∗imod&ThinSpace;&ThinSpace;(1e97))\sum_{i1}^n(ans_i*i\mod (1e97))i1∑n​(ansi​∗imod(1e97)) 解题思路 计算出每个匹配括号的前一个括号位置和后一个括号位置。 一个差分数组 先…

使用Identity Server 4建立Authorization Server (4)

预备知识: 学习Identity Server 4的预备知识 第一部分: 使用Identity Server 4建立Authorization Server (1) 第二部分: 使用Identity Server 4建立Authorization Server (2) 第三部分: 使用Identity Server 4建立Authorization Server (3) 上一篇讲了使用OpenId Connect进行Au…

linux操作命令

uname -r 显示正在使用的内核版本、 docker exec -it mytomcat bash 进入tomcat界面 pwd 显示工作路径 ls 查看当前目录中的文件 docker docker run -d -p 8080:8080 tomcat 启动tomcat镜像 docker start id 编辑文件 touch 文件名 创建文件 mkdir dir1 创建一个叫做 ‘dir1…

Hadoop入门(十八)Mapreduce的倒排索引程序

一、简介 "倒排索引"是文档检索系统中最常用的数据结构&#xff0c;被广泛地应用于全文搜索引擎。它主要是用来存储某个单词&#xff08;或词组&#xff09;在一个文档或一组文档中的存储位置的映射&#xff0c;即提供了一种根据内容来查找文档的方式。由于不是根据…

.NET Core跨平台的奥秘[中篇]:复用之殇

在《.NET Core跨平台的奥秘[上篇]&#xff1a;历史的枷锁》中我们谈到&#xff1a;由于.NET是建立在CLI这一标准的规范之上&#xff0c;所以它天生就具有了“跨平台”的基因。在微软发布了第一个针对桌面和服务器平台的.NET Framework之后&#xff0c;它开始 “乐此不疲” 地对…

jozj4010-我才不是萝莉控呢【哈夫曼树】

正题 题目大意 从(n,1)(n,1)(n,1)到(1,1)(1,1)(1,1)&#xff0c;一个数组AAA&#xff0c;满足Ai≥Ai1A_i\geq A_i1Ai​≥Ai​1 每次有两个选择走到(x−1,y1)(x-1,y1)(x−1,y1)&#xff0c;或(x,⌊y/2⌋)(x,\lfloor y/2\rfloor)(x,⌊y/2⌋)。后者需要消耗∑ixnAi\sum_{ix}^nA_i…

java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized

在application.yml中 spring:datasource:username: rootpassword:url: jdbc:mysql://127.0.0.1:3306/testdriver-class-name: com.mysql.cj.jdbc.Driver结果报错 java.sql.SQLException: The server time zone value ‘&#xfffd;й&#xfffd;&#xfffd;&#xfffd;׼ʱ…

Hadoop入门(十一)Mapreduce的InputFomrat各种子类

一、TextInputFormat extends FileInputFomrat<LongWritable,Text> 是默认读取文件的切分器&#xff0c;其内的LineRecordReader:用来读取每一行的内容&#xff0c; LineRecordReader:内的 nextKeyValue(){}中&#xff0c;key的赋值在&#xff1a; initialize()方法内&…

欢乐纪中某B组赛【2019.1.21】

前言 成功翻车 成绩 RankRankRank是有算别人的 RankRankRankPersonPersonPersonScoreScoreScoreAAABBBCCC1414142017hzb2017hzb2017hzb8080803030300005050501414142017wyc2017wyc2017wyc8080800003030305050501414142017xxy2017xxy2017xxy8080803030300005050504444442017lw2…

极简版ASP.NET Core学习路径

拒绝承认这是一个七天速成教程&#xff0c;即使有这个效果&#xff0c;我也不愿意接受这个名字。嗯。 这个路径分为两块&#xff1a; 实践入门 理论延伸 有了ASP.NET以及C#的知识以及项目经验&#xff0c;我们几乎可以不再需要了解任何新的知识就开始操练&#xff0c;实践才是…

spring boot连接数据库

applicat.yml spring:datasource:username: rootpassword:url: jdbc:mysql://localhost:3306/test?useUnicodetrue&useJDBCCompliantTimezoneShifttrue&useLegacyDatetimeCodefalse&serverTimezoneUTCdriver-class-name: com.mysql.cj.jdbc.Drivertest文件夹下测…

依存句法分析的任务以及形式化定义

转载自 依存句法分析的任务以及形式化定义 依存句法分析的任务以及形式化定义 1、依存句法分析的形式化定义 在依存句法中&#xff0c;共同的基本假设是&#xff1a;句法结构本质上包含词和词对之间的关系。这种关系就是依存关系&#xff08;dependency relations&#xff…

jzoj3084-超级变变变【数学】

正题 题目大意 定义函数 f(x){x−1(x%21)x/2(x%20)f(x)\left\{\begin{matrix} &amp;x-1(x\%21)\\ &amp; x/2(x\%20) \end{matrix}\right.f(x){​x−1(x%21)x/2(x%20)​ 一次变化是将xf(x)xf(x)xf(x) 求A∼BA\sim BA∼B之间有多少个数可以变化到kkk 解题思路 其实就是…

使用Identity Server 4建立Authorization Server (5)

预备知识: 学习Identity Server 4的预备知识 第一部分: 使用Identity Server 4建立Authorization Server (1) 第二部分: 使用Identity Server 4建立Authorization Server (2) 第三部分: 使用Identity Server 4建立Authorization Server (3) 第四部分: 使用Identity Server 4建立…

idea如何安装lombok

https://github.com/mplushnikov/lombok-intellij-plugin/releases &#xff0c;Plugins -> Install plugin from disk… 选择下载的zip包安装&#xff0c;重启idea即可。 依赖包 <dependency><groupId>org.projectlombok</groupId><artifactId>lom…