mapreduce練習12 流量使用前10

@陌小軍發表於2020-10-07

1.FlowBean

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class FlowBean implements WritableComparable<FlowBean> {
    private long upFlow;
    private long downFlow;
    private long sumFlow;

    @Override
    public String toString() {
        return upFlow + "\t" + downFlow + "\t" + sumFlow;
    }

    public long getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }

    public long getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }

    public long getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(long sumFlow) {
        this.sumFlow = sumFlow;
    }

    @Override
    public int compareTo(FlowBean o) { //比較  逆序,按照降序排列
        return Long.compare(o.sumFlow, this.sumFlow);
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(upFlow);
        out.writeLong(downFlow);
        out.writeLong(sumFlow);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.upFlow = in.readLong();
        this.downFlow = in.readLong();
        this.sumFlow = in.readLong();
    }
}

2.FlowGroupingComparator

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

public class FlowGroupingComparator extends WritableComparator {
    protected FlowGroupingComparator() {
        super(FlowBean.class, true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        return 0;  //返回0,讓所有資料都相等
    }
}

3.FlowMapper

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class FlowMapper extends Mapper<LongWritable, Text, FlowBean, Text> {

    private FlowBean k = new FlowBean();
    private Text v = new Text();

    //ctrl + o 重寫方法
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] fields = value.toString().split("\t");  //按照/t進行切分
        v.set(fields[0]);
        k.setUpFlow(Long.parseLong(fields[1]));
        k.setDownFlow(Long.parseLong(fields[2]));
        k.setSumFlow(Long.parseLong(fields[3]));

        context.write(k, v);
    }
}

4.FlowReducer

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

public class FlowReducer extends Reducer<FlowBean, Text, Text, FlowBean> {//輸入k,v,輸出k,v

    @Override
    protected void reduce(FlowBean key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        //獲取迭代器
        Iterator<Text> iterator = values.iterator();
        //取前十
        for (int i = 0; i < 10; i++) {
            context.write(iterator.next(), key);
        }
    }
}

5.FlowDriver

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
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 FlowDriver {
    public static void main(String[] args) throws Exception, ClassNotFoundException, InterruptedException {
        // 1 獲取配置資訊,或者job物件例項
        String int_path = "hdfs://gjh:9000/1702240034/output/part-r-00000";
        String out_path = "hdfs://gjh:9000/1702240034/output_top10";
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI(int_path), conf);
        if (fs.exists(new Path(out_path))) {
            fs.delete(new Path(out_path), true);
        }
        Job job = Job.getInstance(conf);

        //指定本程式的jar包所在的本地路徑
        job.setJarByClass(FlowDriver.class);
        //指定本業務job要使用的mapper/Reducer業務類
        job.setMapperClass(FlowMapper.class);
        job.setReducerClass(FlowReducer.class);

        job.setGroupingComparatorClass(FlowGroupingComparator.class);
        //指定mapper輸出資料的kv型別
        job.setMapOutputKeyClass(FlowBean.class);
        job.setMapOutputValueClass(Text.class);
        //指定最終輸出的資料的kv型別
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);
        //指定job的輸入原始檔案所在目錄
        FileInputFormat.setInputPaths(job, new Path(int_path));
        FileOutputFormat.setOutputPath(job, new Path(out_path));
        //將job中配置的相關引數,以及job所用的java類所在的jar包,提交給yarn去執行
        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}

6.

在這裡插入圖片描述

相關文章