In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
mapreduce中怎么实现二次排序,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
二次排序的原理是将key自定义为一个其他的Bean对象,该对象中存储两个变量,一个为正常需要排序的key,第二个为需要作为二次排序的key,并为这个对象提供比较方法
/** * @ClassName IntPair * @Description * 定义IntPair对象,该对象实现WritableComparable接口,描述第一列和第二列数据,同时完成两列数据的相关操作 * ,这里是对二者进行比较 * @date 2014年11月10日 上午10:15:34 * */ public static class IntPair implements WritableComparable { 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 { @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中怎么实现二次排序问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.