hbase-0.96.x相對hbase-0.94.x的改變

環境:
Hadoop:hadoop-2.2.0
hbase:hbase-0.96.0
1.org.apache.hadoop.hbase.client.Put
    <1>取消了無參的構造方法
    <2>Put類再也不繼承Writable類
    
        0.94.6時public class Put extends Mutation implements HeapSize, Writable, Comparable<Row>
        0.96.0時public class Put extends Mutation implements HeapSize, Comparable<Row>
解決方法:
        由public class MonthUserLoginTimeIndexReducer extends Reducer<BytesWritable,MonthUserLoginTimeIndexWritable, ImmutableBytesWritable, Writable> {
改public class MonthUserLoginTimeIndexReducer extends Reducer<BytesWritable,MonthUserLoginTimeIndexWritable, ImmutableBytesWritable, Put> {
2.org.apache.hadoop.hbase.client.Mutation.familyMap
     org.apache.hadoop.hbase.client.Mutation.familyMap類型改變:
     /**
     * 0.94.6
     * protected Map<byte[],List<KeyValue>> familyMap
     * 
     * 0.96.*
     * protected NavigableMap<byte[],List<Cell>> familyMap
     * org.apache.hadoop.hbase.Cell hbase-0.94.*中是沒有的
     */    apache

     org.apache.hadoop.hbase.KeyValue的改變:
     /**
     * 0.94.*
     * public class KeyValue extends Object implements Writable, HeapSize
     * 
     * 0.96.0
     * public class KeyValue extends Object implements Cell, HeapSize, Cloneable
     */
     解決方法:將代碼中的List<KeyValue>改爲List<Cell>
3. org.apache.hadoop.hbase.KeyValue
     0.96.0中方法getFamily已被棄用(Deprecated),改爲方法getFamilyArray() 
4.org.apache.hadoop.hbase.HTableDescriptor   
     類org.apache.hadoop.hbase.HTableDescriptor的構造方法public HTableDescriptor(String name)已被棄用(Deprecated)
     解決方法:使用public HTableDescriptor(TableName name)
     舊:HTableDescriptor tableDesc = new HTableDescriptor(tableName);
     新:HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
5.org.apache.hadoop.hbase.client.HTablePool
     類org.apache.hadoop.hbase.client.HTablePool整個被棄用(Deprecated)
     解決方法:使用HConnection.getTable(String)代替,HConnection是個接口,類CoprocessorHConnection是它惟一的實現類:
     HRegionServer hRegionServer = new HRegionServer(conf) ;
     HConnection connection = HConnectionManager.createConnection(conf);
     hConnection = new CoprocessorHConnection(connection,hRegionServer);
6.org.apache.hadoop.hbase.client.Result
     方法public KeyValue[] raw()被棄用(Deprecated),建議使用public Cell[] rawCells()
     方法getRow被棄用(Deprecated)
     方法getFamily被棄用(Deprecated)
     方法getQualifier被棄用(Deprecated)
     方法getValue被棄用(Deprecated)
     方法public List<KeyValue> getColumn(byte[] family,byte[] qualifier)被棄用(Deprecated)
     方法public KeyValue getColumnLatest(byte[] family,byte[] qualifier)被棄用(Deprecated)
     Cell中:改爲如下方法
     getRowArray()
     getFamilyArray()
     getQualifierArray()
     getValueArray()
     Result中:增長以下方法
     public List<KeyValue> getColumnCells(byte[] family,byte[] qualifier)
     public KeyValue getColumnLatestCell(byte[] family,byte[] qualifier)
     改動:全部ipeijian_data中凡是和【新增用戶活躍用戶流失用戶】相關的都作以下變化:
     舊代碼:if (value.raw().length == 1
     新代碼:if (value.rawCells().length == 1
7.job中設置TableInputFormat.SCAN
     0.96.0中去掉了方法:public void write(DataOutput out)throws IOException
     以前版本使用conf.set(TableInputFormat.SCAN, StatUtils.convertScanToString(scan));進行設置
     StatUtils.convertScanToString的具體實現爲:
     public static String convertScanToString(Scan scan) throws IOException {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(out);
            scan.write(dos);
            return Base64.encodeBytes(out.toByteArray());
     }
     該方法的實現與TableMapReduceUtil.convertScanToString(Scan scan)是同樣的。
     可是當hbase升級到了0.96.*是對於類Scan棄用(不單單是Deprecated,而是Deleted)了方法write,因此上面
     的實現變爲不正確
     hbase0.96.*中對該方法進行了從新的實現:
     public static String convertScanToString(Scan scan) throws IOException {
            ClientProtos.Scan proto = ProtobufUtil.toScan(scan);
            return Base64.encodeBytes(proto.toByteArray());
     }
     因此作以下更改:
     StatUtils類中方法convertScanToString的實現作如上更改以適配hbase0.96.* 
8.cn.m15.ipj.db.hbase.MyPut
    自定義的Put類,比傳統的Put類多一個length,原版和新版代碼比較:
    原版:(紅色字體爲API變爲新版時報錯的地方)oop

public class MyPut extends Put {
     public MyPut(byte[] row, int length) {                                    
     //緣由是put的無參構造方法已經在新本中消失
          if (row == null || length > HConstants.MAX_ROW_LENGTH) {
               throw new IllegalArgumentException(「Row key is invalid」);
          }
          this.row = Arrays.copyOf(row, length);
          this.ts = HConstants.LATEST_TIMESTAMP;
     }    
     public MyPut add(byte[] family, byte[] qualifier, long ts, byte[] value,int length) {
          List<KeyValue> list = getKeyValueList(family);
          KeyValue kv = createPutKeyValue(family, qualifier, ts, value, length);
          list.add(kv);
          familyMap.put(kv.getFamily(), list);                                   
          //familyMap的類型已經改變
          return this;
      }
     private List<KeyValue> getKeyValueList(byte[] family) {
          List<KeyValue> list = familyMap.get(family);                     
          //familyMap的類型已經改變
          if (list == null) {
               list = new ArrayList<KeyValue>(0);
          }
          return list;
     }
     private KeyValue createPutKeyValue(byte[] family, byte[] qualifier,long ts, byte[] value, int length) {
          return new KeyValue(this.row, 0, this.row.length, family, 0,
          family.length, qualifier, 0, qualifier.length, ts,
          KeyValue.Type.Put, value, 0, length);
     }
}字體

更改以後:this

public MyPut(byte[] row, int length) {
     super(row,length);                                                                      
     //新增長

     if (row == null || length > HConstants.MAX_ROW_LENGTH) {
          throw new IllegalArgumentException(「Row key is invalid」);
     }
     this.row = Arrays.copyOf(row, length);
     this.ts = HConstants.LATEST_TIMESTAMP;
     }
     public MyPut add(byte[] family, byte[] qualifier, long ts, byte[] value,int length) {
          List<Cell> list = getCellsList(family);
          KeyValue kv = createPutKeyValue(family, qualifier, ts, value, length);
          list.add(kv);
          familyMap.put(CellUtil.cloneFamily(kv), list);
          return this;
     }    
     private List<Cell> getCellsList(byte[] family) {
          List<Cell> list = familyMap.get(family);
          if (list == null) {
              list = new ArrayList<Cell>(0);
          }
          return list;
     }
     private KeyValue createPutKeyValue(byte[] family, byte[] qualifier,long ts, byte[] value, int length) {
          return new KeyValue(this.row, 0, this.row.length, family, 0,family.length, qualifier, 0, qualifier.length, ts,
                    KeyValue.Type.Put, value, 0, length);
     }
}spa

相關文章
相關標籤/搜索