天天看點

hadoop自定義資料類型

Hadoop的基本資料類型是基于對Java的基本資料類型的封裝,如int對應IntWritable,Long對應LongWritable。

和Java中自定義資料類型一樣,某些時候我們也會在Hadoop中建立自定義資料類型。

Hadoop中自定義資料類型必須實作WritableComparable接口

舉例:

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;

public class LastOrder implements WritableComparable<LastOrder>{

	private int cust_id;
	private String cust_type;
	private String cust_email;
	
	public LastOrder(){
		
	}
	
	@Override
	public void readFields(DataInput in) throws IOException {
		
		this.cust_id = in.readInt();
		this.cust_type = in.readUTF();
		this.cust_email =in.readUTF();
	}

	@Override
	public void write(DataOutput out) throws IOException {
		
		out.writeInt(this.cust_id);
		out.writeUTF(this.cust_type);
		out.writeUTF(this.cust_email);
		
	}


	public int compareTo(LastOrder o) {
		
		return this.cust_id-o.cust_id;
	}

	public int hashCode(){
		return super.hashCode();
	}
	
	public boolean equals(LastOrder o){
		return super.equals(o);
	}
	
	
	public String toString(){
		StringBuffer  sb= new StringBuffer();
		sb.append(cust_id);
		sb.append("\001");
		sb.append(cust_type);
		sb.append("\001");
		sb.append(cust_email);
		
		return sb.toString();
	}
	
	public int getCust_id() {
		return cust_id;
	}

	public void setCust_id(int cust_id) {
		this.cust_id = cust_id;
	}

	public String getCust_email() {
		return cust_email;
	}

}
           

注意:方法readFields()和write()的字段順序必須一一對應,不然程式運作時會報錯。

繼續閱讀