39.top10热门品类之自定义二次排序key
来源:互联网 发布:手机如何注册淘宝会员 编辑:程序博客网 时间:2024/06/08 06:40
本文为《Spark大型电商项目实战》 系列文章之一,主要代码实现top10热门品类模块中的第四步:自定义二次排序key。
代码实现
第四步:自定义二次排序key
在包com.erik.sparkproject.spark
中新建二次排序key的类CategorySortKey.java
package com.erik.sparkproject.spark;import scala.math.Ordered;/** * 品类二次排序key * * 封装你要进行排序算法需要的几个字段;点击次数、下单次数和支付次数 * 实现Ordered接口要求的几个方法 * * 跟其他key相比,如何来判定大于、大于等于、小于、小于等于 * 依次使用三个次数进行比较,如果某一个相等,那么就比较下一个 * * 自定义的二次排序key,必须要实现serializable接口,表明是可以序列化 的,否则会报错 * @author Erik * */public class CategorySortKey implements Ordered<CategorySortKey>, Serializable{ private static final long serialVersionUID = -565992549650791884L; private long clickCount; private long orderCount; private long payCount; //使用右键-source-Generate Constructor using Fields进行构造方法 public CategorySortKey(long clickCount, long orderCount, long payCount) { super(); this.clickCount = clickCount; this.orderCount = orderCount; this.payCount = payCount; } 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; } 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; } 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; } 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; } 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; } 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; }}
《Spark 大型电商项目实战》源码:https://github.com/Erik-ly/SprakProject
本文为《Spark大型电商项目实战》系列文章之一,
更多文章:Spark大型电商项目实战:http://blog.csdn.net/u012318074/article/category/6744423
0 0
- 39.top10热门品类之自定义二次排序key
- 40.top10热门品类之进行二次排序
- 43.top10热门品类之使用Scala实现二次排序
- 42.top10热门品类之本地测试
- 41.top10热门品类之获取top10品类并写入MySQL
- 36.top10热门品类之获取session访问过的所有品类
- 37.top10热门品类之计算各品类点击、下单和支付的次数
- 38.top10热门品类之join品类与点击下单支付次数
- 35.top10热门品类之需求回顾以及实现思路分析
- MapReduce-自定义Key-二次排序
- 44.top10活跃session之开发准备以及top10品类RDD生成
- 45.top10活跃session之计算top10品类被各sessoin点击的次数
- 大数据IMF传奇 第19课 spark 二次排序 使用JAVA自定义key 进行二次排序
- MapReduce自定义二次排序
- Spark排序算法!! 使用java开发 自定义key值 进行二次排序 深入解析!
- Hadoop之MapReduce自定义二次排序流程实例详解
- Hadoop之MapReduce自定义二次排序流程实例详解
- Hadoop之MapReduce自定义二次排序流程实例详解
- poj 1556 线段相交 + dijstra
- For 循环编程题
- 用sax解析jsp页面时的问题
- android 7.0 app安装问题
- 数据表的创建
- 39.top10热门品类之自定义二次排序key
- (HTML5 )canvas 第八章
- Git仓库恢复
- POJ 2763 树链剖分+线段树维护区间和
- (转载)感知机模型
- linux之信号
- java基础2
- 前端学习资源汇总--持续更新
- NCL安装小记