hadoop案例(二)--统计每一个手机号全年的总话费

统计每一个手机号全年的总话费(注意,虚拟网包月费属于赠送费,不计入在内)。
数据准备:
在这里插入图片描述
流程:
(1)读取一行数据,切分字段;
(2)抽取手机号、套餐基本费、语音通信费、短信彩信费、流量费;
(3)以手机号为key,bean对象为value输出,即context.write(手机号,bean)。
Reduce阶段:
(1)累加套餐基本费、语音通信费、短信彩信费、流量费得到总花费;
(2)实现自定义的bean来封装流量信息,并将bean作为map输出的key来传输;
(3) MR程序在处理数据的过程中会对数据排序(map输出的kv对传输到reduce之前,会排序),排序的依据是map输出的key。

程序设计:
(1)编写流量统计的bean对象

package com.csdn.TelFee;

import org.apache.hadoop.io.Writable;

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

public class Phone implements Writable {
    private int baseFee;//基础费用
    private int  viceFee;//语音费用
    private int msgFee;//短信费用
    private int floeFee;//流量费用
    private int sumFee;//共计费用

    public Phone() {
    }

    public Phone(int baseFee, int viceFee, int msgFee, int floeFee) {
        this.baseFee = baseFee;
        this.viceFee = viceFee;
        this.msgFee = msgFee;
        this.floeFee = floeFee;
        this.sumFee = baseFee+viceFee+msgFee+floeFee;
    }

    public int getBaseFee() {
        return baseFee;
    }

    public void setBaseFee(int baseFee) {
        this.baseFee = baseFee;
    }

    public int getViceFee() {
        return viceFee;
    }

    public void setViceFee(int viceFee) {
        this.viceFee = viceFee;
    }

    public int getMsgFee() {
        return msgFee;
    }

    public void setMsgFee(int msgFee) {
        this.msgFee = msgFee;
    }

    public int getFloeFee() {
        return floeFee;
    }

    public void setFloeFee(int floeFee) {
        this.floeFee = floeFee;
    }

    public int getSumFee() {
        return sumFee;
    }

    public void setSumFee(int sumFee) {
        this.sumFee = sumFee;
    }

    public void write(DataOutput out) throws IOException {
        out.writeInt(baseFee);
        out.writeInt(viceFee);
        out.writeInt(msgFee);
        out.writeInt(floeFee);
        out.writeInt(sumFee);
    }

    public void readFields(DataInput in) throws IOException {
        baseFee=in.readInt();
        viceFee=in.readInt();
        msgFee=in.readInt();
        floeFee=in.readInt();
        sumFee=in.readInt();
    }

    @Override
    public String toString() {
        return baseFee+"\t"+viceFee+"\t"+msgFee+"\t"+floeFee+"\t"+sumFee;
    }
}

(2)编写mapper

package com.csdn.TelFee;

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

import java.io.IOException;

public class myMapper extends Mapper<LongWritable, Text,Text,Phone> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        A	13939119984	3	5	7	8	20	201901
        String[] allArr = line.split("\t");
        String num=allArr[1];
        Phone phone =new Phone(
                Integer.parseInt(allArr[2]),//基础费
                Integer.parseInt(allArr[3]),//语音费
                Integer.parseInt(allArr[4]),//短信
                Integer.parseInt(allArr[5]));//流量
        context.write(new Text(num),phone);
    }
}

(3)编写reducer

package com.csdn.TelFee;

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

import java.io.IOException;

public class myReducer extends Reducer<Text,Phone,Text,Phone> {
    @Override
    protected void reduce(Text key, Iterable<Phone> values, Context context) throws IOException, InterruptedException {
        int baseSum=0;
        int viceSum=0;
        int msgSum=0;
        int flowSum=0;
        for (Phone value : values) {
            baseSum+=value.getBaseFee();
            viceSum+=value.getViceFee();
            msgSum+=value.getMsgFee();
            flowSum+=value.getFloeFee();
        }
        Phone phone = new Phone(baseSum,viceSum,msgSum,flowSum);
        context.write(key,phone);
    }
}

(4)编写驱动

package com.csdn.TelFee;

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;

public class myDriver {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(myDriver.class);
        job.setMapperClass(myMapper.class);
        job.setReducerClass(myReducer.class);

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

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

        FileInputFormat.setInputPaths(job,new Path("F:\\int\\phoneFee.txt"));
        FileSystem fs = FileSystem.get(conf);
        Path outPath= new Path("F:\\out\\free");
        boolean b = fs.exists(outPath);
        if (b) fs.delete(outPath,true);
        FileOutputFormat.setOutputPath(job,outPath);
        job.submit();
    }
}

数据处理后的结果:

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值