spark自定义二次排序

1.实现scala.math.Ordered接口

2.重写接口重的三个方法

3.三个方法中自定义你自己的比较大小的业务逻辑

4.我的案例

package com.lilei.spark.session;

import java.io.Serializable;

import scala.math.Ordered;

/**
 * 品类二次排序key
 *
 * 封装你要进行排序算法需要的几个字段:点击次数、下单次数和支付次数
 * 实现Ordered接口要求的几个方法
 *
 * 跟其他key相比,如何来判定大于、大于等于、小于、小于等于
 *
 * 依次使用三个次数进行比较,如果某一个相等,那么就比较下一个
 *
 * (自定义的二次排序key,必须要实现Serializable接口,表明是可以序列化的,负责会报错)
 *
 * @author Administrator
 *
 */
public class CategorySortKey implements Ordered<CategorySortKey>, Serializable {

    private static final long serialVersionUID = -6007890914324789180L;

    private long clickCount;
    private long orderCount;
    private long payCount;

    public CategorySortKey(long clickCount, long orderCount, long payCount) {
        this.clickCount = clickCount;
        this.orderCount = orderCount;
        this.payCount = payCount;
    }

    /**
     * 这个方法判断当前key比参数里面传进来的key要大
     * @param other
     * @return
     */
    @Override
    public boolean $greater(CategorySortKey other) {
        if(clickCount > other.getClickCount()) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount > other.getOrderCount()) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount == other.getOrderCount() &&
                payCount > other.getPayCount()) {
            return true;
        }
        return false;
    }

    /**
     * 判定当前key大于等于参数key
     * @param other
     * @return
     */
    @Override
    public boolean $greater$eq(CategorySortKey other) {
        if($greater(other)) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount == other.getOrderCount() &&
                payCount == other.getPayCount()) {
            return true;
        }
        return false;
    }

    /**
     * 当前key小于参数key
     * @param other
     * @return
     */
    @Override
    public boolean $less(CategorySortKey other) {
        if(clickCount < other.getClickCount()) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount < other.getOrderCount()) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount == other.getOrderCount() &&
                payCount < other.getPayCount()) {
            return true;
        }
        return false;
    }

    /**
     * 当前key小于等于参数key
     * @param other
     * @return
     */
    @Override
    public boolean $less$eq(CategorySortKey other) {
        if($less(other)) {
            return true;
        } else if(clickCount == other.getClickCount() &&
                orderCount == other.getOrderCount() &&
                payCount == other.getPayCount()) {
            return true;
        }
        return false;
    }

    /**
     *
     * @param other
     * @return
     */
    @Override
    public int compare(CategorySortKey other) {
        if(clickCount - other.getClickCount() != 0) {
            return (int) (clickCount - other.getClickCount());
        } else if(orderCount - other.getOrderCount() != 0) {
            return (int) (orderCount - other.getOrderCount());
        } else if(payCount - other.getPayCount() != 0) {
            return (int) (payCount - other.getPayCount());
        }
        return 0;
    }

    @Override
    public int compareTo(CategorySortKey other) {
        if(clickCount - other.getClickCount() != 0) {
            return (int) (clickCount - other.getClickCount());
        } else if(orderCount - other.getOrderCount() != 0) {
            return (int) (orderCount - other.getOrderCount());
        } else if(payCount - other.getPayCount() != 0) {
            return (int) (payCount - other.getPayCount());
        }
        return 0;
    }

    public long getClickCount() {
        return clickCount;
    }

    public void setClickCount(long clickCount) {
        this.clickCount = clickCount;
    }

    public long getOrderCount() {
        return orderCount;
    }

    public void setOrderCount(long orderCount) {
        this.orderCount = orderCount;
    }

    public long getPayCount() {
        return payCount;
    }

    public void setPayCount(long payCount) {
        this.payCount = payCount;
    }
}

猜你喜欢

转载自blog.csdn.net/u013164612/article/details/80324774