_mapreduce中的自定义排序

UserOrderSort.java

package com.igeekhome.mapreduce.model;

import org.apache.hadoop.io.WritableComparable;

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

public class UserOrderSort implements WritableComparable<UserOrderSort> {

    //订单编号
    private Integer orderId;
    //用户名
    private String userName;
    //用户性别
    private String sex;
    //订购商品名称
    private String goodsName;
    //商品的单价
    private Integer price;
    //订购商品数量
    private Integer saleCount;
    //订购总价
    private Integer totalPrice;


    //重写比较排序方法
    //按照订单总金额。进行降序排列

    @Override
    public int compareTo(UserOrderSort userOrderSort) {
        //获取要比较的订单总额
        Integer inputTotalPrice = userOrderSort.getTotalPrice();

        //定义返回值
        int returnCode=-1;
        if(this.totalPrice<inputTotalPrice){
            returnCode= -1;

        }else if(this.totalPrice>inputTotalPrice){
            returnCode=1;
        }
        else{
            returnCode=0;
        }
        return returnCode;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(orderId);
        dataOutput.writeUTF(userName);
        dataOutput.writeUTF(sex);
        dataOutput.writeUTF(goodsName);
        dataOutput.writeInt(price);
        dataOutput.writeInt(saleCount);
        dataOutput.writeInt(totalPrice);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        //在反序列化时,属性的顺序要与序列化一致
        this.orderId = dataInput.readInt();
        this.userName= dataInput.readUTF();
        this.sex= dataInput.readUTF();
        this.goodsName= dataInput.readUTF();
        this.price = dataInput.readInt();
        this.saleCount = dataInput.readInt();
        this.totalPrice = dataInput.readInt();
    }

    @Override
    public String toString() {
        return this.totalPrice.toString();
    }
    public UserOrderSort() {
    }

    public Integer getOrderId() {
        return orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Integer getSaleCount() {
        return saleCount;
    }

    public void setSaleCount(Integer saleCount) {
        this.saleCount = saleCount;
    }

    public Integer getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Integer totalPrice) {
        this.totalPrice = totalPrice;
    }
    public void setTotalPrice() {
        this.totalPrice = this.price*this.saleCount;
    }
}

UserOrderSortDriver.java 

package com.igeekhome.mapreduce.ordersort;


import com.igeekhome.mapreduce.model.UserOrderSort;
import com.igeekhome.mapreduce.order.SexPartitioner;
import org.apache.hadoop.conf.Configuration;
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 UserOrderSortDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //1.获取配置信息对象和job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);


        //2.关联driver类
        job.setJarByClass(UserOrderSortDriver.class);

        //3.设置mapper和reduce的类
        job.setMapperClass(UserOrderSortMapper.class);
        job.setReducerClass(UserOrderSortReducer.class);

        //4.设置mapper输出的kv类型
        job.setMapOutputKeyClass(UserOrderSort.class);
        job.setMapOutputValueClass(Text.class);


        //5设置最终输出的kv类型(reducer输出的kv类型)
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(UserOrderSort.class);


        //6.设置文件的输入路径和计算结果的输出路径
        Path filePath1 = new Path("D:\\bigdata\\sale_details.txt");

        FileInputFormat.setInputPaths(job,filePath1);

        //设置计算结果的输出路径(不存在)
        Path outputPath = new Path("D:\\bigdata\\order_sort_output");
        FileOutputFormat.setOutputPath(job,outputPath);


//        //设置采用自定义分区
//        job.setPartitionerClass(SexPartitioner.class);
//        //根据最终结果文件的个数设置对应的reduce task任务的个数
//        job.setNumReduceTasks(3);

        //7.提交任务,进行计算
        boolean result=job.waitForCompletion(true);

        System.out.println(result?"执行成功":"执行失败");
    }
}

UserOrderSortMapper.java

package com.igeekhome.mapreduce.ordersort;

import com.igeekhome.mapreduce.model.UserOrderSort;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;


/*
 * LongWritable:代表输入的key,值为每行数据的偏移量
 * UserOrderSort:代表输出的key,值为进行排序的用户订单对象
 * */
public class UserOrderSortMapper extends Mapper<LongWritable, Text, UserOrderSort,Text>{


    //新建UserOrderSort对象作为输出的key
    private UserOrderSort keyOut=new UserOrderSort();
    //新建Text对象作为输出的value
    private Text valueOut=new Text();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //获取一行数据
        String line = value.toString();

        //根据文本间的分隔符对于单词进行拆分
        String[] orderData = line.split(",");

        //根据下标提取数据
        String orderId = orderData[0];
        String userName = orderData[1];
        String sex = orderData[2];
        String goodsName = orderData[3];
        String price = orderData[4];
        String saleCount = orderData[5];

        //为输出的key赋值
        keyOut.setOrderId(Integer.parseInt(orderId));
        keyOut.setUserName(userName);
        keyOut.setSex(sex);
        keyOut.setGoodsName(goodsName);
        keyOut.setPrice(Integer.parseInt(price));
        keyOut.setSaleCount(Integer.parseInt(saleCount));

        keyOut.setTotalPrice();
        //为输出的value赋值
        valueOut.set(userName);
        //map阶段进行输出
        context.write(keyOut,valueOut);
    }
}

 UserOrderSortReducer.java

package com.igeekhome.mapreduce.ordersort;

import com.igeekhome.mapreduce.model.UserOrderSort;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * 第一个UserOrderSort:代表输入的key值进行排序的用户订单对象
 * 第一个Text:代表输入的value值为用户名
 *
 * 第二个UserOrderSort:代表输出的value值进行排序的用户订单对象
 *  第二个Text:代表输出的key值为用户名
 *
 * */
public class UserOrderSortReducer extends Reducer<UserOrderSort, Text,Text,UserOrderSort> {

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

        for (Text userName : values) {
            context.write(userName,userOrderSort);
        }
    }
}

 运行截图

sale_details.txt

order_sort_output

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值