溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Hadoop怎么自定制數(shù)據(jù)類型

發(fā)布時間:2021-12-09 14:43:03 來源:億速云 閱讀:160 作者:iii 欄目:云計算

這篇文章主要講解了“Hadoop怎么自定制數(shù)據(jù)類型”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Hadoop怎么自定制數(shù)據(jù)類型”吧!

一般有兩個辦法,一種較為簡單的是針對值,另外一種更為完整的是對于鍵和值都適應(yīng)的方法:
1、實現(xiàn)Writable接口:

/* 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);
 }
}

2、對于鍵來說,需要指定排序規(guī)則,對此,Java版Hadoop的辦法是實現(xiàn)WritableComparable這個泛型接口,WritableComparable,顧名思義了,一半是Writable,一半是Comparable

public interface WritableComparable<T> {
 public void readFields(DataInput in);
 public void write(DataOutput out);
 public int compareTo(T other);
}

這里的compareTo方法是默認(rèn)的key排序

先給出下面的簡單例子,再做說明和擴展。
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;
 }
 /* 實現(xiàn) hashCode() 方法很重要
  * Hadoop的Partitioners會用到這個方法,后面再說
  */
 public int hashCode() {
  return Float.floatToIntBits(x)
   ^ Float.floatToIntBits(y)
   ^ Float.floatToIntBits(z);
 }
}
如果要將對象寫入數(shù)據(jù)庫則還要繼承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; 
    } 
 

感謝各位的閱讀,以上就是“Hadoop怎么自定制數(shù)據(jù)類型”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Hadoop怎么自定制數(shù)據(jù)類型這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI