mapReduce案例之输出总流量在前10名的数据

FlowBean类

import org.apache.hadoop.io.WritableComparable;

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

class FlowBean implements WritableComparable<FlowBean> {
    private int sumFLow;
    private int upFLow;
    private int downFLow;

    @Override
    public String toString() {
        return upFLow +"\t" + downFLow +
                "\t" + sumFLow ;
    }

    public FlowBean(int sumFLow, int upFLow, int downFLow) {
        this.sumFLow = sumFLow;
        this.upFLow = upFLow;
        this.downFLow = downFLow;
    }

    public FlowBean() {
    }

    public int getUpFLow() {
        return upFLow;
    }

    public void setUpFLow(int upFLow) {
        this.upFLow = upFLow;
    }

    public int getDownFLow() {
        return downFLow;
    }

    public void setDownFLow(int downFLow) {
        this.downFLow = downFLow;
    }

    public int getSumFLow() {
        return sumFLow;
    }

    public void setSumFLow(int sumFLow) {
        this.sumFLow = sumFLow;
    }



    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(this.upFLow);
        dataOutput.writeInt(this.downFLow);
        dataOutput.writeInt(this.sumFLow);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        upFLow = dataInput.readInt();
        downFLow = dataInput.readInt();
        sumFLow = dataInput.readInt();
    }

    @Override
    public int compareTo(FlowBean o) {
        int result;
        result = Integer.compare(o.getSumFLow(), this.sumFLow);
        if(result == 0){
            result = this.downFLow > o.getDownFLow() ? -1 : 1;
        }
        return result;
    }
}

FlowTopNMapper类

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

import java.io.IOException;

public class FlowTopNMapper extends Mapper<LongWritable, Text,FlowBean,Text> {
    Text k = new Text();
    FlowBean flowBean = new FlowBean();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] splits = line.split("\t");
        k.set(splits[0]);
        flowBean.setUpFLow(Integer.parseInt(splits[1]));
        flowBean.setDownFLow(Integer.parseInt(splits[2]));
        flowBean.setSumFLow(Integer.parseInt(splits[3]));
        context.write(flowBean,k);
    }
}

FlowTopNReducer类

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

import java.io.IOException;

public class FlowTopNReducer extends Reducer<FlowBean, Text, Text, FlowBean> {
    int i = 0;

    @Override
    protected void reduce(FlowBean key, Iterable<Text> values, Context context) throws IOException, InterruptedException {

        if (i < 10) {
            for (Text v : values) {
                context.write(v, key);
            }
            i++;
        }
    }
}

FlowTopNDriver类

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;

public class FlowTopNDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Job job = Job.getInstance();

        job.setMapOutputKeyClass(FlowBean.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        job.setMapperClass(FlowTopNMapper.class);
        job.setReducerClass(FlowTopNReducer.class);

        FileInputFormat.setInputPaths(job,new Path("F:\\test\\top10\\top10.txt"));
        FileOutputFormat.setOutputPath(job,new Path("F:\\test\\top10\\top10out"));

        job.waitForCompletion(true);
    }
}

要分析的文件内容

13470253144	180	180	360
13509468723	7335	110349	117684
13560439638	918	4938	5856
13568436656	3597	25635	29232
13590439668	1116	954	2070
13630577991	6960	690	7650
13682846555	1938	2910	4848
13729199489	240	0	240
13736230513	2481	24681	27162
13768778790	120	120	240
13846544121	264	0	264
13956435636	132	1512	1644
13966251146	240	0	240
13975057813	11058	48243	59301
13992314666	3008	3720	6728
15043685818	3659	3538	7197
15910133277	3156	2936	6092
15959002129	1938	180	2118
18271575951	1527	2106	3633
18390173782	9531	2412	11943
84188413	4116	1432	5548
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MapReduce是一种用于处理大规模数据集的编程模型。在手机流量统计案例中,我们可以利用MapReduce来统计手机用户在不同时间段内的流量使用情况。 首先,我们需要将数据进行切分,将原始数据分为多个小块。接下来,我们使用Map函数,将每个小块的数据按照指定的键值对进行映射。键可以是时间段,值可以是流量数据。 然后,我们使用Reduce函数对映射后的数据进行归并和计算。Reduce函数可以对相同键的值进行合并操作,例如求和。这样我们就可以得到每个时间段的总流量。 为了更好地说明,我们以一天为时间段为例。假设我们有一个包含手机用户流量数据的文件,每一行表示一个用户在某个时间点的流量使用情况。 在Map阶段中,我们将文件每一行解析为键值对。键是时间段(例如早上、中午、下午、晚上等),值是流量数据。在这一阶段,我们可以使用正则表达式或其他方法来提取时间段和流量数据。 在Reduce阶段中,我们将相同时间段的流量数据进行合并计算。例如,对于早上这个时间段,我们将所有流量数据进行求和操作,得到这个时间段的总流量。 最后,我们可以将结果写入输出文件,或者保存在数据库中,便于进一步分析和应用。 通过MapReduce框架,我们可以高效地处理大规模的手机流量数据,提取有价值的信息。例如,我们可以分析不同时间段的流量使用情况,找出用户流量高峰时段,为运营商提供更精确的网络优化策略。此外,这种方法也可以用于其他大数据场景的数据处理和分析。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值