溫馨提示×

溫馨提示×

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

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

hadoop-common中WritableUtils的示例代碼

發(fā)布時(shí)間:2021-12-09 15:22:39 來源:億速云 閱讀:145 作者:小新 欄目:云計(jì)算

這篇文章將為大家詳細(xì)講解有關(guān) hadoop-common中WritableUtils的示例代碼,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

hadoop將java的基本類型進(jìn)行封裝,對(duì)整型進(jìn)行編碼時(shí),分為固定長度格式、可變長度格式??勺冮L度格式使用一種比較靈活的編碼方式,對(duì)與較小的數(shù)(尤其是負(fù)數(shù))可以節(jié)省空間存儲(chǔ)。

VIntWritable
public class VIntWritable implements WritableComparable<VIntWritable> {
  private int value;//getter //setter
  @Override
  public void readFields(DataInput in) throws IOException {
    value = WritableUtils.readVInt(in);
  }  @Override
  public void write(DataOutput out) throws IOException {
    WritableUtils.writeVInt(out, value);
  }

}
WritableUtils.writeVLong
  public static void writeVInt(DataOutput stream, int i) throws IOException {
    writeVLong(stream, i);
  }  public static void writeVLong(DataOutput stream, long i) throws IOException {if (i >= -112 && i <= 127) {
      stream.writeByte((byte)i);      return;
    }int len = -112;if (i < 0) {
      i ^= -1L; // take one's complement'  len = -120;
    }long tmp = i;while (tmp != 0) {
      tmp = tmp >> 8;
      len--;
    }

    stream.writeByte((byte)len);

    len = (len < -120) ? -(len + 120) : -(len + 112);for (int idx = len; idx != 0; idx--) {      int shiftbits = (idx - 1) * 8;      long mask = 0xFFL << shiftbits;
      System.out.println(((i & mask) >> shiftbits));
      stream.writeByte((byte)((i & mask) >> shiftbits));
    }
  }
  1. 如果i在 [-112 ~ 127] 之間,直接轉(zhuǎn)換為byte類型存儲(chǔ)。

  2. 如果i小于-112時(shí),將其轉(zhuǎn)換成正數(shù)(異或-1L),將標(biāo)識(shí)量len 設(shè)置-120;否則len為-112

  3. 移位要存儲(chǔ)的數(shù)據(jù),同時(shí)len進(jìn)行自減(len即做了標(biāo)示量,又統(tǒng)計(jì)了移位次數(shù))。

  4. 將標(biāo)識(shí)量寫到輸出流。

  5. 重置len,將len設(shè)置為移位個(gè)數(shù)。

  6. 進(jìn)行循環(huán),將數(shù)據(jù)每8位寫到輸出流(大端模式),具體分析for循環(huán)。

WritableUtils.readVLong
  public static long readVLong(DataInput stream) throws IOException {byte firstByte = stream.readByte();int len = decodeVIntSize(firstByte);if (len == 1) {      return firstByte;
    }long i = 0;for (int idx = 0; idx < len-1; idx++) {      byte b = stream.readByte();
      i = i << 8;
      i = i | (b & 0xFF);
    }return (isNegativeVInt(firstByte) ? (i ^ -1L) : i);
  }  public static int decodeVIntSize(byte value) {if (value >= -112) {      return 1;
    } else if (value < -120) {      return -119 - value;
    }return -111 - value;
  } 
  public static boolean isNegativeVInt(byte value) {return value < -120 || (value >= -112 && value < 0);
  }
  1. 讀取一個(gè)byte類型

  2. 判斷讀出數(shù)據(jù)如果大于-112,說明不是標(biāo)志量,可以直接返回原始數(shù)據(jù),如果小于-120或者在[-112 ~ -120]之間,說明為標(biāo)識(shí)量,需要判斷返回移動(dòng)位數(shù)。

  3. 通過得到移動(dòng)的位數(shù),每次移動(dòng)8位,異或移位。還原數(shù)據(jù)。

  4. 判斷表示量,如果在小于-120 或者在[0 ~ -112]之間,證明是負(fù)數(shù),將得到的數(shù)據(jù)進(jìn)行異或-1L得到最終值。

關(guān)于“ hadoop-common中WritableUtils的示例代碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

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

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

AI