hadoop復(fù)合鍵排序使用方法
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-11-06 12:03:41 閱讀次數(shù):2142次
在hadoop中處理復(fù)雜業(yè)務(wù)時(shí),需要用到復(fù)合鍵,復(fù)合不同于單純的繼承Writable接口,而是繼承了WritableComparable<T>接口,而實(shí)際上,WritableComparable<T>接口繼承了Writable和Comparable<T>接口,如果只需要使用某1個(gè)類(lèi)作為傳值對(duì)象而不是作為key,繼承Writable接口便可。
上源碼:
public interface WritableComparable<T> extends Writable, Comparable<T> {
}
public interface Writable {
void write(DataOutput out) throws IOException;
void readFields(DataInput in) throws IOException;
}
public interface Comparable<T> {
public int compareTo(T o);
}
以下是實(shí)現(xiàn)復(fù)合key的實(shí)例,親測(cè),可用
public class SortKey implements WritableComparable<SortKey>{
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);
}
}
<span style="white-space:pre"> </span>//到目前為止,你只能將其作為key來(lái)使用,但是如果你需要依照key的某1個(gè)值來(lái)排序,以下是重點(diǎn)
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);
}
}
}
}
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)