IntWritable

Hadoop数据类型

咱们自顶而下来分析一下这些类

一、Writable接口的定义如下

package org.apache.hadoop.io;  
  
import java.io.DataOutput;  
import java.io.DataInput;  
import java.io.IOException;  
public interface Writable {  
/* 
    object将自身字段序列化后的的字节流写入输出流out中。 
参数: 
    out - 接收object序列化后的字节流的输出流. 
*/  
  void write(DataOutput out) throws IOException;  
    
  /* 
      将输入流in中的字节流反序列化然后写入object的字段 
  参数: 
      字节流的出处 
  */  
    void readFields(DataInput in) throws IOException;  
}

二、WritableComparable接口的定义如下

public interface WritableComparable<T> extends Writable, comparable<T> {  
}

三、comparable的方法,comparable是属于java.lang.*中的一个接口,它只有一个方法

int compareTo( T other);  
/* 
    比较此对象与指定对象other的顺序。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。 
 
    参数:o - 要比较的对象。 
 
    返回:负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。  
*/

四、IntWritable类定义如下

package org.apache.hadoop.io;  
  
import java.io.*;  
  
/** A WritableComparable for ints. */  
public class IntWritable implements WritableComparable {  
  private int value;  
  
  public IntWritable() {}  
  
  public IntWritable(int value) { set(value); }  
  
  /** Set the value of this IntWritable. */  
  public void set(int value) { this.value = value; }  
  
  /** Return the value of this IntWritable. */  
  public int get() { return value; }  
  
  public void readFields(DataInput in) throws IOException {  
    value = in.readInt();  
  }  
  
  public void write(DataOutput out) throws IOException {  
    out.writeInt(value);  
  }  
  
  /** Returns true iff o is a IntWritable with the same value. */  
  public boolean equals(Object o) {  
    if (!(o instanceof IntWritable))  
      return false;  
    IntWritable other = (IntWritable)o;  
    return this.value == other.value;  
  }  
  
  public int hashCode() {  
    return value;  
  }  
  
  /** Compares two IntWritables. */  
  public int compareTo(Object o) {  
    int thisValue = this.value;  
    int thatValue = ((IntWritable)o).value;  
    return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));  
  }  
  
//重写了toString方法
  public String toString() {  
    return Integer.toString(value);  
  }  
  
  /** A Comparator optimized for IntWritable. */   
  public static class Comparator extends WritableComparator {  
    public Comparator() {  
      super(IntWritable.class);  
    }  
  
    public int compare(byte[] b1, int s1, int l1,  
                       byte[] b2, int s2, int l2) {  
      int thisValue = readInt(b1, s1);  
      int thatValue = readInt(b2, s2);  
      return (thisValue<thatValue ? -1 : (thisValue==thatValue ? 0 : 1));  
    }  
  }  
  
  static {                                        // register this comparator  
    WritableComparator.define(IntWritable.class, new Comparator());  
  }  
}

转载于:https://my.oschina.net/u/3267050/blog/1601497

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值