Hadoop的自定製數據類型
通常有兩個辦法,一種較爲簡單的是針對值,另一種更爲完整的是對於鍵和值都適應的方法:
一、實現Writable接口:java
/* DataInput and DataOutput 類是java.io的類 */
public interface Writable {
void readFields(DataInput in);
void write(DataOutput out);
}數據庫
下面是一個小例子:
public class Point3D implement Writable {
public float x, y, z;
public Point3D(float fx, float fy, float fz) {
this.x = fx;
this.y = fy;
this.z = fz;
}
public Point3D() {
this(0.0f, 0.0f, 0.0f);
}
public void readFields(DataInput in) throws IOException {
x = in.readFloat();
y = in.readFloat();
z = in.readFloat();
}
public void write(DataOutput out) throws IOException {
out.writeFloat(x);
out.writeFloat(y);
out.writeFloat(z);
}
public String toString() {
return Float.toString(x) + ", "
+ Float.toString(y) + ", "
+ Float.toString(z);
}
}ide
二、對於鍵來講,須要指定排序規則,對此,Java版Hadoop的辦法是實現WritableComparable這個泛型接口,WritableComparable,顧名思義了,一半是Writable,一半是Comparableoop
public interface WritableComparable<T> {
public void readFields(DataInput in);
public void write(DataOutput out);
public int compareTo(T other);
}this
這裏的compareTo方法是默認的key排序code
先給出下面的簡單例子,再作說明和擴展。
public class Point3D inplements WritableComparable {
public float x, y, z;
public Point3D(float fx, float fy, float fz) {
this.x = fx;
this.y = fy;
this.z = fz;
}
public Point3D() {
this(0.0f, 0.0f, 0.0f);
}
public void readFields(DataInput in) throws IOException {
x = in.readFloat();
y = in.readFloat();
z = in.readFloat();
}
public void write(DataOutput out) throws IOException {
out.writeFloat(x);
out.writeFloat(y);
out.writeFloat(z);
}
public String toString() {
return Float.toString(x) + ", "
+ Float.toString(y) + ", "
+ Float.toString(z);
}
public float distanceFromOrigin() {
return (float) Math.sqrt( x*x + y*y +z*z);
}
public int compareTo(Point3D other) {
return Float.compareTo(
distanceFromOrigin(),
other.distanceFromOrigin());
}
public boolean equals(Object o) {
if( !(o instanceof Point3D)) {
return false;
}
Point3D other = (Point3D) o;
return this.x == o.x
&& this.y == o.y
&& this.z == o.z;
}
/* 實現 hashCode() 方法很重要
* Hadoop的Partitioners會用到這個方法,後面再說
*/
public int hashCode() {
return Float.floatToIntBits(x)
^ Float.floatToIntBits(y)
^ Float.floatToIntBits(z);
}
}
若是要將對象寫入數據庫則還要繼承DBWritable接口對象
public interface WritableComparable<T> {
public void write(PreparedStatement statement) throwsSQLException;
public void readFields(ResultSet resultSet) throws SQLException;}排序
下面寫個例子繼承
public class LocationBean implements Writable, DBWritable {
private String mobilenetworkcode;
private String mobilecountrycode;
private Integer cellid;
private Integer locationareacode;
private Integer baiduareaid;
private Double lat;
private Double lng;
private Integer areaid;
@Override
public void write(PreparedStatement statement) throws SQLException {
int index = 1;
statement.setString(index++, this.getMobilenetworkcode());
statement.setString(index++, this.getMobilecountrycode());
statement.setInt(index++, this.getCellid());
statement.setInt(index++, this.getLocationareacode());
statement.setInt(index++, this.getBaiduareaid());
statement.setDouble(index++, this.getLat());
statement.setDouble(index++, this.getLng());
statement.setInt(index, this.getAreaid());
}
@Override
public void readFields(ResultSet resultSet) throws SQLException {
this.mobilenetworkcode = resultSet.getString(1);
this.mobilecountrycode = resultSet.getString(2);
this.cellid = resultSet.getInt(3);
this.locationareacode = resultSet.getInt(4);
this.baiduareaid = resultSet.getInt(5);
this.lat = resultSet.getDouble(6);
this.lng = resultSet.getDouble(7);
this.areaid = resultSet.getInt(8);
}
@Override
public void write(DataOutput out) throws IOException {
// TODO Auto-generated method stub
}
@Override
public void readFields(DataInput in) throws IOException {
}
public String getMobilenetworkcode() {
return mobilenetworkcode;
}
public void setMobilenetworkcode(String mobilenetworkcode) {
this.mobilenetworkcode = mobilenetworkcode;
}
public String getMobilecountrycode() {
return mobilecountrycode;
}
public void setMobilecountrycode(String mobilecountrycode) {
this.mobilecountrycode = mobilecountrycode;
}
public Integer getCellid() {
return cellid;
}
public void setCellid(Integer cellid) {
this.cellid = cellid;
}
public Integer getLocationareacode() {
return locationareacode;
}
public void setLocationareacode(Integer locationareacode) {
this.locationareacode = locationareacode;
}
public Integer getBaiduareaid() {
return baiduareaid;
}
public void setBaiduareaid(Integer baiduareaid) {
this.baiduareaid = baiduareaid;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
public Integer getAreaid() {
return areaid;
}
public void setAreaid(Integer areaid) {
this.areaid = areaid;
}
} 接口