当前位置: 首页 > news >正文

MapReduce | 二次排序

1.需求

主播数据--按照观众人数降序排序,如果观众人数相同,按照直播时长降序

# 案例数据

用户id 观众人数 直播时长

团团 300 1000

小黑 200 2000

哦吼 400 7000

卢本伟 100 6000

八戒 250 5000

悟空 100 4000

唐僧 100 3000

# 期望结果

哦吼 400 7000

团团 300 1000

八戒 250 5000

小黑 200 2000

卢本伟 100 6000

悟空 100 4000

唐僧 100 3000

2.将数据上传到hdfs

3.Idea代码

package demo6;import org.apache.hadoop.io.WritableComparable;import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;public class PlayWritable implements WritableComparable<PlayWritable> {private int viewer;private int length;public PlayWritable() {}public PlayWritable(int viewer, int length) {this.viewer = viewer;this.length = length;}public int getViewer() {return viewer;}public void setViewer(int viewer) {this.viewer = viewer;}public int getLength() {return length;}public void setLength(int length) {this.length = length;}@Overridepublic String toString() {return viewer + " " + length;}@Overridepublic void write(DataOutput out) throws IOException {out.writeInt(viewer);out.writeInt(length);}@Overridepublic void readFields(DataInput in) throws IOException {this.viewer = in.readInt();this.length = in.readInt();}@Overridepublic int compareTo(PlayWritable o) {if (this.viewer != o.viewer){return this.viewer > o.viewer ? -1 : 1;}return this.length > o.length ? -1 : (this.length == o.length ? 0 : 1);}
}
package demo6;import demo5.DescIntWritable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.checkerframework.checker.units.qual.Length;import java.io.IOException;public class Sort3Job {public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {Configuration conf = new Configuration();conf.set("fs.defaultFS","hdfs://hadoop10:8020");Job job = Job.getInstance(conf);job.setJarByClass(Sort3Job.class);job.setInputFormatClass(TextInputFormat.class);job.setOutputFormatClass(TextOutputFormat.class);TextInputFormat.addInputPath(job,new Path("/mapreduce/demo6/sort3.txt"));TextOutputFormat.setOutputPath(job,new Path("/mapreduce/demo6/out"));job.setMapperClass(Sort3Mapper.class);job.setReducerClass(Sort3Reducer.class);//map输出的键与值类型job.setMapOutputKeyClass(PlayWritable.class);job.setMapOutputValueClass(Text.class);//reducer输出的键与值类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(PlayWritable.class);boolean b = job.waitForCompletion(true);System.out.println(b);}static class Sort3Mapper extends Mapper<LongWritable, Text, PlayWritable,Text> {@Overrideprotected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {String[] arr = value.toString().split("\t");context.write(new PlayWritable(Integer.parseInt(arr[1]),Integer.parseInt(arr[2])),new Text(arr[0]));}}static class Sort3Reducer extends Reducer<PlayWritable,Text,Text,PlayWritable>{@Overrideprotected void reduce(PlayWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {for (Text name : values) {context.write(name,key);}}}
}

4.在hdfs查看结果


请好好爱自己~ 想和你做朋友~

http://www.lryc.cn/news/347426.html

相关文章:

  • Java后端初始化项目(项目模板)
  • electron 多窗口 vuex/pinia 数据状态同步简易方案(利用 LocalStorage)
  • 自定义数据集图像分类实现
  • 【C++】手搓读写ini文件源码
  • undolog
  • 项目文档分享
  • 【深耕 Python】Quantum Computing 量子计算机(5)量子物理概念(二)
  • 手写Spring5【笔记】
  • 2024中国(重庆)机器人展览会8月举办
  • Apache 开源项目文档中心 (英文 + 中文)
  • 蓝桥杯 算法提高 ADV-1164 和谐宿舍 python AC
  • Dragonfly 拓扑的路由算法
  • android基础-服务
  • mysql 事物
  • Unity Shader中获取像素点深度信息
  • ROS——Action学习
  • 基于C语言中的类型转换,C++标准创造出了更加可视化的类型转换
  • 如何创建族表
  • 【UnityRPG游戏制作】Unity_RPG项目_PureMVC框架应用
  • 并行计算的一些知识点分享--并行系统,并行程序, 并发,并行,分布式
  • 设计模式:访问者模式
  • vivado Virtex-7 配置存储器器件
  • 检测服务器环境,实现快速部署。适用于CRMEB_PRO/多店
  • Spring Security初探
  • 【Java代码审计】敏感信息泄漏篇
  • Windows Server 2012 R2 新增D盘分区
  • transformer与beter
  • MySQL索引设计遵循一系列原则
  • windows窗口消息队列与消息过程处理函数
  • 【Chisel】chisel中怎么处理类似verilog的可变位宽和parameter