wiki:waue/2009/0626

Version 2 (modified by waue, 15 years ago) (diff)

--

新 hbase 解析

  • hbase 已經到0.19.3了,可惜的是 hbase 0.18.1的api及文件都已經從官方網頁被拿掉了,所幸hbase 0.18 與hbase 0.19相差無幾
  • hbase 0.1.3 到 hbase 0.19.3 中間好像已經包含了十幾代,但是其實類似java 1.4 直接跳躍 到 java 5 一樣,hbase 0.1.3 -> hbase 0.2.x -> hbase 0.18.x -> hbase 0.19.x
    • 從 hbase 0.18.x 之後,其版本名稱就直接對應hadoop 的版本名稱,如 hbase 0.18 搭配 hadoop 0.18 , hbase 0.19 搭配hadoop 0.19
  • 筆者開發hbase時是 hbase 0.13的時代,現在又重拾舊業,發現已經差很多了。如
  • 找資料:
    • hbase 0.13 : select * from 'table_name'
    • hbase 0.19 : scan 'table_name'
  • 資料庫結構:
    • hbase 0.13 :
column_family:column_quolify1 column_family:column_quolify2 column_family:column_quolify3
time stamp 2 cell_value1 cell_value2 cell_value3
time stamp 1 cell_value1 cell_value2 cell_value3
  • hbase 0.19 : 加入 row 這個結構
    HBaseConfiguration config = new HBaseConfiguration();
    HBaseAdmin admin = new HBaseAdmin(config);
    if (!admin.tableExists(table_name)) {
      System.out.println("HTable : " + table_name
          + "  creating ... please wait");
      HTableDescriptor tableDesc = new HTableDescriptor(table_name);
      // add column family
      tableDesc.addFamily(new HColumnDescriptor(colomn_family));
      admin.createTable(tableDesc);
    }
    
    BatchUpdate batchUpdate = new BatchUpdate(hbase_row);
    batchUpdate.put(colomn_family+column_quolify, Bytes.toBytes(value));
    batchUpdate.delete(colomn_family+"cellIWantDeleted");
                HTable table = new HTable(config, table_name);
    table.commit(batchUpdate);
    Cell cell = table.get(hbase_row, colomn_family+column_quolify);
    System.out.print(cell.getValue());

    Scanner scanner =
    table.getScanner(new String[] { colomn_family+column_quolify });

    RowResult rowResult = scanner.next();

    while (rowResult != null) {

      System.out.println("Found row: "
          + Bytes.toString(rowResult.getRow())
          + " with value: "
          + rowResult.get(Bytes
              .toBytes(colomn_family+column_quolify)));
      rowResult = scanner.next();
    }

    for (RowResult result : scanner) {
      System.out.println("Found row: "
          + Bytes.toString(rowResult.getRow())
          + " with value: "
          + rowResult.get(Bytes
              .toBytes(colomn_family+column_quolify)));
    }

    scanner.close();



  • 完整程式碼如下
import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Scanner;
import org.apache.hadoop.hbase.io.BatchUpdate;
import org.apache.hadoop.hbase.io.Cell;
import org.apache.hadoop.hbase.io.RowResult;
import org.apache.hadoop.hbase.util.Bytes;

public class hbase {

  public static void main(String args[]) throws IOException {

    String table_name = "waue"; 
    String colomn_family = "family:";
    String column_quolify= "qf";
    String hbase_row = "w-row";
    String value = "0911311311";
    
    HBaseConfiguration config = new HBaseConfiguration();


    HBaseAdmin admin = new HBaseAdmin(config);
    if (!admin.tableExists(table_name)) {
      System.out.println("HTable : " + table_name
          + "  creating ... please wait");
      HTableDescriptor tableDesc = new HTableDescriptor(table_name);
      // add column family
      tableDesc.addFamily(new HColumnDescriptor(colomn_family));
      admin.createTable(tableDesc);
    }

    HTable table = new HTable(config, table_name);


    BatchUpdate batchUpdate = new BatchUpdate(hbase_row);


    batchUpdate.put(colomn_family+column_quolify, Bytes
        .toBytes(value));


    batchUpdate.delete(colomn_family+"cellIWantDeleted");


    table.commit(batchUpdate);


    Cell cell = table.get(hbase_row, colomn_family+column_quolify);
    System.out.print(cell.getValue());


    Scanner scanner =
    table.getScanner(new String[] { colomn_family+column_quolify });

    RowResult rowResult = scanner.next();

    while (rowResult != null) {

      System.out.println("Found row: "
          + Bytes.toString(rowResult.getRow())
          + " with value: "
          + rowResult.get(Bytes
              .toBytes(colomn_family+column_quolify)));
      rowResult = scanner.next();
    }

    for (RowResult result : scanner) {
      System.out.println("Found row: "
          + Bytes.toString(rowResult.getRow())
          + " with value: "
          + rowResult.get(Bytes
              .toBytes(colomn_family+column_quolify)));
    }

    scanner.close();
  }
}