【发布时间】:2014-04-02 06:48:46
【问题描述】:
给定一个名为 KeyLabelDistance 的类,我将其作为 Hadoop 中的键和值传递,我想对其执行二次排序,即我首先要根据键的递增值对键进行排序,然后按递减顺序对键进行排序距离。
为了做到这一点,我需要编写自己的 GroupingComparator。我的问题是,由于 setGroupingComparator() 方法仅将扩展 RawComparator 的类作为参数,我如何在分组比较器中执行此比较字节?我需要显式序列化和反序列化对象吗? 并且让 KeyLabelDistance 类实现 WritableComparable 如下是否会使对 SortComparator 的需求变得多余?
我从这个答案中得到了 SortComparator 和 GroupComparator 的使用:What are the differences between Sort Comparator and Group Comparator in Hadoop?
以下是KeyLabelDistance的实现:
public class KeyLabelDistance implements WritableComparable<KeyLabelDistance>
{
private int key;
private int label;
private double distance;
KeyLabelDistance()
{
key = 0;
label = 0;
distance = 0;
}
KeyLabelDistance(int key, int label, double distance)
{
this.key = key;
this.label = label;
this.distance = distance;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public int getLabel() {
return label;
}
public void setLabel(int label) {
this.label = label;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public int compareTo(KeyLabelDistance lhs, KeyLabelDistance rhs)
{
if(lhs == rhs)
return 0;
else
{
if(lhs.getKey() < rhs.getKey())
return -1;
else if(lhs.getKey() > rhs.getKey())
return 1;
else
{
//If the keys are equal, look at the distances -> since more is the "distance" more is the "similarity", the comparison is counterintuitive
if(lhs.getDistance() < rhs.getDistance() )
return 1;
else if(lhs.getDistance() > rhs.getDistance())
return -1;
else return 0;
}
}
}
}
组比较器的代码如下:
public class KeyLabelDistanceGroupingComparator extends WritableComparator{
public int compare (KeyLabelDistance lhs, KeyLabelDistance rhs)
{
if(lhs == rhs)
return 0;
else
{
if(lhs.getKey() < rhs.getKey())
return -1;
else if(lhs.getKey() > rhs.getKey())
return 1;
return 0;
}
}
}
任何帮助表示赞赏。在此先感谢。
【问题讨论】: