import scala.math.Ordered;
/**
* 自定义品类二次排序key
* @author dali
* 封装排序算法需要的几个字段:点击次数,下单次数,支付次数
* 实现Ordered接口要求的几个方法
*
* 其它key相比,如何判断大于,大于等于,小于,小于等于
*
* 依次使用三个数进行比较,如果某一个相等,那么就比较下一个
*/
public class CategorySortKey implements Ordered<CategorySortKey>{
private long clickCount;
private long orderCount;
private long payCount;
@Override
public boolean $greater(CategorySortKey other) {
if(clickCount > other.getClickCount()) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount > other.getClickCount()) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount == other.getClickCount() &&
payCount > other.getClickCount()) {
return true;
}
return false;
}
@Override
public boolean $greater$eq(CategorySortKey other) {
if($greater(other)) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount == other.getClickCount() &&
payCount == other.getClickCount()) {
return true;
}
return false;
}
@Override
public boolean $less(CategorySortKey other) {
if(clickCount < other.getClickCount()) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount < other.getClickCount()) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount == other.getClickCount() &&
payCount < other.getClickCount()) {
return true;
}
return false;
}
@Override
public boolean $less$eq(CategorySortKey other) {
if($less(other)) {
return true;
} else if(clickCount == other.getClickCount() &&
orderCount == other.getClickCount() &&
payCount == other.getClickCount()) {
return true;
}
return false;
}
@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) (orderCount - 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) (orderCount - 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;
}
}
/**
* 5. 将数据映射成 <CategorySortKey, info>格式的RDD,然后进行二次排序(降序)
*/
JavaPairRDD<CategorySortKey, String> sortKey2CountRDD = categoryid2countRDD
.mapToPair(tuple -> {
String countInfo = tuple._2;
long clickCount = Long.valueOf(StringUtils
.getFieldFromConcatString(countInfo, "\\|", Constants.FIELD_CLICK_COUNT));
long orderCount = Long.valueOf(StringUtils
.getFieldFromConcatString(countInfo, "\\|", Constants.FIELD_ORDER_COUNT));
long payCount = Long.valueOf(StringUtils
.getFieldFromConcatString(countInfo, "\\|", Constants.FIELD_PAY_COUNT));
CategorySortKey categorySortKey = new CategorySortKey(
clickCount,orderCount,payCount);
return new Tuple2<CategorySortKey, String>(categorySortKey,countInfo );
});
//降序
JavaPairRDD<CategorySortKey, String> sortedCategoryCountRDD = sortKey2CountRDD.sortByKey(false);