博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hadoop排序组合键的使用情况
阅读量:4559 次
发布时间:2019-06-08

本文共 2477 字,大约阅读时间需要 8 分钟。

于hadoop当处理复杂的业务,需要使用组合键,与单纯的复杂的继承Writable接口,但继承WritableComparable<T>接口。事实上。WritableComparable<T>接口继承Writable和Comparable<T>接口,假设仅仅须要使用某一个类作为传值对象而不是作为key,继承Writable接口就可以。

上源代码:

public interface WritableComparable
extends Writable, Comparable
{}
public interface Writable {  void write(DataOutput out) throws IOException;  void readFields(DataInput in) throws IOException;}
public interface Comparable
{ public int compareTo(T o);}
下面是实现复合key的实例,亲測。可用

public class SortKey implements WritableComparable
{ private Text name; private IntWritable right; public SortKey() { set(new Text(), new IntWritable()); } public SortKey(Text name, IntWritable right) { set(name, right); } private void set(Text name,IntWritable right){ this.name = name; this.right = right; } /** * @return the name */ public Text getName() { return name; } /** * @param name the name to set */ public void setName(Text name) { this.name = name; } /** * @return the right */ public IntWritable getRight() { return right; } /** * @param right the right to set */ public void setRight(IntWritable right) { this.right = right; } @Override public void write(DataOutput out) throws IOException { name.write(out); right.write(out); } @Override public void readFields(DataInput in) throws IOException { name.readFields(in); right.readFields(in); } @Override public int compareTo(SortKey o) { int cmp = name.compareTo(o.name); if(cmp != 0){ return cmp; }else{ return right.compareTo(o.right); } }
	//到眼下为止,你仅仅能将其作为key来使用,可是假设你须要依照key的某一个值来排序,下面是重点
static{		WritableComparator.define(SortKey.class, new Comparator());	}		public static class Comparator extends WritableComparator{			    private static final Text.Comparator TEXT_COMPARATOR = new Text.Comparator();				protected Comparator() {			super(SortKey.class);		}		/* (non-Javadoc)		 * @see org.apache.hadoop.io.WritableComparator#compare(byte[], int, int, byte[], int, int)		 */		@Override		public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {			try{				int firstL1 = WritableUtils.decodeVIntSize(b1[s1]) + readVInt(b1, s1);				int firstL2 = WritableUtils.decodeVIntSize(b2[s2]) + readVInt(b2, s2);	            return TEXT_COMPARATOR.compare(b1, s1, firstL1, b2, s2, firstL2);			}catch(Exception e){				throw new IllegalArgumentException(e);			}		}		}}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

转载于:https://www.cnblogs.com/zfyouxi/p/4717690.html

你可能感兴趣的文章
JavaScript:综合案例---房贷计算器的实现
查看>>
MapXtreme开发(一)
查看>>
请问乘客最终买了几等座?
查看>>
获取表行数
查看>>
less与sass的区别点
查看>>
event.keycode值大全
查看>>
array and ram
查看>>
工作笔记——禁用浏览器的返回按钮
查看>>
免费获得盛大网盘EverBox125G容量方法
查看>>
如何用spidermonkey在python里调用javascript代码
查看>>
2016级算法第一次练习赛-A.群鸦的盛宴
查看>>
浅谈深度学习和本体间的关系
查看>>
js下载文件
查看>>
python 中的高级函数filter()
查看>>
vim配置
查看>>
python创建系统时间字符串
查看>>
服务器上产看报错的日志的方法
查看>>
软件安装
查看>>
黑盒测试实践—第四天
查看>>
luogu P4448 [AHOI2018初中组]球球的排列
查看>>