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;
    }

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

    }
}
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查看结果


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

相关推荐

  1. MapReduce

    2024-05-13 19:24:07       25 阅读
  2. MapReduce

    2024-05-13 19:24:07       24 阅读
  3. MapReduce

    2024-05-13 19:24:07       10 阅读
  4. <span style='color:red;'>MapReduce</span>

    MapReduce

    2024-05-13 19:24:07      9 阅读
  5. 排序---快速排序的4优化

    2024-05-13 19:24:07       6 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-13 19:24:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-13 19:24:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-13 19:24:07       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-13 19:24:07       20 阅读

热门阅读

  1. KAN网络

    KAN网络

    2024-05-13 19:24:07      11 阅读
  2. 微调大模型学习记录

    2024-05-13 19:24:07       15 阅读
  3. MFC--CCreateContext结构体

    2024-05-13 19:24:07       9 阅读
  4. 三种基本排序-冒泡,选择,二分

    2024-05-13 19:24:07       10 阅读
  5. MySQL中所有数据类型

    2024-05-13 19:24:07       9 阅读
  6. MongoDB聚合运算符:$topN

    2024-05-13 19:24:07       11 阅读
  7. stylus详解与引入

    2024-05-13 19:24:07       13 阅读
  8. 深度学习学习日记(5.6)

    2024-05-13 19:24:07       11 阅读
  9. 初级银行从业资格证知识点(十)

    2024-05-13 19:24:07       11 阅读
  10. 升级WSL Ubuntu内核从5.10到5.15

    2024-05-13 19:24:07       17 阅读
  11. Flink面试整理-Flink的配置管理包含哪些?

    2024-05-13 19:24:07       14 阅读
  12. Python Pandas 数据分析快速入门

    2024-05-13 19:24:07       12 阅读
  13. el-tree

    2024-05-13 19:24:07       24 阅读
  14. QT 文字转语言插件

    2024-05-13 19:24:07       15 阅读