mapreduce中怎么实现二次排序,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
二次排序的原理是将key自定义为一个其他的Bean对象,该对象中存储两个变量,一个为正常需要排序的key,第二个为需要作为二次排序的key,并为这个对象提供比较方法
/**
* @ClassName IntPair
* @Description
* 定义IntPair对象,该对象实现WritableComparable接口,描述第一列和第二列数据,同时完成两列数据的相关操作
* ,这里是对二者进行比较
* @date 2014年11月10日 上午10:15:34
*
*/
public static class IntPair implements WritableComparable<IntPair> {
String first;
int second;
/**
* Set the left and right values.
*/
public void set(String left, int right) {
first = left;
second = right;
}
public String getFirst() {
return first;
}
public int getSecond() {
return second;
}
public int getFileName() {
return fileName;
}
public void setFileName(int fileName) {
this.fileName = fileName;
}
@Override
// 反序列化,从流中的二进制转换成IntPair
public void readFields(DataInput in) throws IOException {
first = in.readUTF();
second = in.readInt();
fileName = in.readInt();
}
@Override
// 序列化,将IntPair转化成使用流传送的二进制
public void write(DataOutput out) throws IOException {
out.writeUTF(first);
out.writeInt(second);
out.writeInt(fileName);
}
@Override
// key的比较
public int compareTo(IntPair o) {
if (!first.equals(o.first)) {
return o.first.compareTo(first);
} else if (second != o.second) {
return second > o.second ? 1 : -1;
} else {
return 0;
}
}
@Override
public boolean equals(Object right) {
if (right == null)
return false;
if (this == right)
return true;
if (right instanceof IntPair) {
IntPair r = (IntPair) right;
return r.first.equals(first) && r.second == second;
} else {
return false;
}
}
}
为了能让第一次排序的正常排序需要使用Partitioner和
/**
* 分区函数类。根据first确定Partition。
*/
public static class FirstPartitioner extends
Partitioner<IntPair, Text> {
@Override
public int getPartition(IntPair key, Text value, int numPartitions) {
return key.first.hashCode()%numPartitions;
}
}
/**
* 分组函数类。只要first相同就属于同一个组。
*/
// 第二种方法,继承WritableComparator
public static class GroupingComparator extends WritableComparator {
protected GroupingComparator() {
super(IntPair.class, true);
}
@SuppressWarnings("rawtypes")
@Override
// Compare two WritableComparables.
public int compare(WritableComparable w1, WritableComparable w2) {
IntPair ip1 = (IntPair) w1;
IntPair ip2 = (IntPair) w2;
String l = ip1.getFirst();
String r = ip2.getFirst();
return r.compareTo(l);
}
}
然后在main函数中的job中加入
job.setMapOutputKeyClass(IntPair.class);
job.setGroupingComparatorClass(GroupingComparator.class);
job.setPartitionerClass(FirstPartitioner.class);
关于mapreduce中怎么实现二次排序问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/ssrs2202/blog/493873