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代码

java 复制代码
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;
    }

    @Override
    public String toString() {
        return viewer + " " + length;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(viewer);
        out.writeInt(length);

    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.viewer = in.readInt();
        this.length = in.readInt();

    }

    @Override
    public 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);

    }
}
java 复制代码
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> {
        @Override
        protected 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>{
        @Override
        protected void reduce(PlayWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text name : values) {
                context.write(name,key);
            }
        }
    }
}

4.在hdfs查看结果


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

相关推荐
AI极客菌6 小时前
AI绘画工具中,为什么专业玩家爱用Stable Diffusion,普通玩家却喜欢Midjourney?
大数据·人工智能·ai·ai作画·stable diffusion·aigc·midjourney
腾视科技AI6 小时前
腾视科技大模型一体机解决方案:低成本私有化落地,重塑行业智能应用新格局
大数据·人工智能·科技·ai·边缘计算·算力·ai算力
金融支付架构实战指南7 小时前
支付系统 ES 实战案例:从索引创建到真实业务查询
大数据·elasticsearch·搜索引擎·支付
百胜软件@百胜软件9 小时前
从“数据孤岛”到“智利标杆”:百胜E3全渠道中台助力“名创优品”Newtree实现一体化智变
大数据·人工智能·零售数字化·数智中台·珠宝行业
lizhihai_999 小时前
股市学习心得-A股服务器/算力服务器龙头
大数据·运维·服务器·人工智能·科技·学习
AllData公司负责人10 小时前
大模型赋能AllData数据中台,系列升级|通过联合智谱大模型与BiSheng开源项目,建设企业大模型应用开发平台,支持知识库向量检索!
大数据·数据结构·数据库·算法·大模型·向量数据库·智谱ai
Antom全球收单10 小时前
面对多市场、多币种、多支付方式,Antom如何帮助企业搭建全球支付平台
大数据
数智化管理手记10 小时前
标准作业越推越虚?重塑认知、规避误区,破解精益落地形式主义
大数据·网络·精益工程
一只鹿鹿鹿10 小时前
网络安全评估方案
java·大数据·运维·物联网·web安全
人工智能培训11 小时前
打造行业知识图谱三步走
大数据·人工智能·机器学习·3d·知识图谱·agent