Changes between Version 3 and Version 4 of waue/2009/0626


Ignore:
Timestamp:
Jun 26, 2009, 5:31:44 PM (15 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2009/0626

    v3 v4  
    44   * 從 hbase 0.18.x 之後,其版本名稱就直接對應hadoop 的版本名稱,如 hbase 0.18 搭配 hadoop 0.18 , hbase 0.19 搭配hadoop 0.19
    55
    6 == 差別 ==
    7 筆者開發hbase時是 hbase 0.13的時代,現在又重拾舊業,發現已經差很多了。
    8 
    9  === 找資料: ===
     6 == 差別 ==
     7筆者開發hbase時是 hbase 0.13的時代,現在又重拾舊業,發現已經差很多了。如撈取資料的方式:
    108   * hbase 0.13 : select * from 'table_name'
    119   * hbase 0.19 : scan 'table_name'
    12  === 資料庫結構: ===
    13    * hbase 0.13 :
    14 || || column_family:column_quolify1|| column_family:column_quolify2|| column_family:column_quolify3||
    15 || time stamp 2 || cell_value1 || cell_value2|| cell_value3||
    16 || time stamp 1 || cell_value1 || cell_value2|| cell_value3||
    17    * hbase 0.19 : 加入 row 這個結構
    18 
     10   
     11 == 資料庫結構: ==
     12table A
     13 || || column_family:column_quolify1|| column_family:column_quolify2|| column_family:column_quolify3||
     14 || row : time stamp 2 || cell_value1 || cell_value2|| cell_value3||
     15 || row : time stamp 1 || cell_value1 || cell_value2|| cell_value3||
     16
     17 * 因此 table A這個資料表,總共有二度的空間,每一度空間又有二維結構。
     18   * 每個 column_family有許多column_quolify1
     19   * 每個 row 又分許多 timestamp
     20 
    1921 == 程式碼 ==
    2022
     23 === 宣告 ===
    2124 * 定義宣告以下參數
    2225
    2326{{{
    24                 String table_name = "waue";
    25                 String colomn_family = "family:";
    26                 String column_quolify= "qf";
    27                 String hbase_row = "w-row";
    28                 String value = "0911311311";
    29 }}}
    30 
    31  * 首先,建立config 物件與admin 物件,前者用來設定資料表屬性,後者用來執行資料庫的建立刪除操作
    32 
    33 {{{
    34 #!java
    35 
    36                 HBaseConfiguration config = new HBaseConfiguration();
    37                 HBaseAdmin admin = new HBaseAdmin(config);
    38 }}}
    39 
    40  *
    41 
    42 {{{
    43 #!java
    44 
    45                 if (!admin.tableExists(table_name)) {
    46                         System.out.println("HTable : " + table_name
    47                                         + "  creating ... please wait");
    48                         HTableDescriptor tableDesc = new HTableDescriptor(table_name);
    49                         // add column family
    50                         tableDesc.addFamily(new HColumnDescriptor(colomn_family));
    51                         admin.createTable(tableDesc);
    52                 }
    53 }}}
    54 
    55  *
    56 
    57 {{{
    58 #!java
    59 
    60                
    61                 BatchUpdate batchUpdate = new BatchUpdate(hbase_row);
    62                 batchUpdate.put(colomn_family+column_quolify, Bytes.toBytes(value));
    63                 batchUpdate.delete(colomn_family+"cellIWantDeleted");
    64 }}}
    65 
    66  *
    67 
    68 {{{
    69 #!java
    70                 HTable table = new HTable(config, table_name);
    71                 table.commit(batchUpdate);
    72 }}}
    73 
    74  *
    75 
    76 {{{
    77 #!java
    78 
    79                 Cell cell = table.get(hbase_row, colomn_family+column_quolify);
    80                 System.out.print(cell.getValue());
    81 
    82 }}}
    83 
    84  *
    85 
    86 {{{
    87 #!java
    88                 Scanner scanner =
    89                 table.getScanner(new String[] { colomn_family+column_quolify });
    90 
    91                 RowResult rowResult = scanner.next();
    92 
    93                 while (rowResult != null) {
    94 
    95                         System.out.println("Found row: "
    96                                         + Bytes.toString(rowResult.getRow())
    97                                         + " with value: "
    98                                         + rowResult.get(Bytes
    99                                                         .toBytes(colomn_family+column_quolify)));
    100                         rowResult = scanner.next();
    101                 }
    102 
    103                 for (RowResult result : scanner) {
    104                         System.out.println("Found row: "
    105                                         + Bytes.toString(rowResult.getRow())
    106                                         + " with value: "
    107                                         + rowResult.get(Bytes
    108                                                         .toBytes(colomn_family+column_quolify)));
    109                 }
    110 
    111                 scanner.close();
    112 
    113 
    114 
     27String table_name = "waue";
     28String colomn_family = "family:";
     29String column_quolify= "qf";
     30String hbase_row = "w-row";
     31String value = "yes ! good !";
     32}}}
     33
     34 === 必要條件 資料表屬性:HBaseConfiguration ===
     35 
     36 * 首先,建立HTableDescriptor 物件,用來設定資料表屬性,為必要條件。
     37
     38{{{
     39#!java
     40
     41HBaseConfiguration config = new HBaseConfiguration();
     42}}}
     43
     44 === 資料表的特權操作:HBaseAdmin ===
     45 * HBaseAdmin 用來執行資料庫的建立刪除操作,下面這段是用來檢查是否已經存在這個資料表,沒有的話則新增,
     46 新增時需要用到HTableDescriptor這個類別來新增Column_Family這個成員。此非必要條件,因為不見得每次執行程式都要新建一個表單
     47 
     48 * 注意: 在此colomn_family 的後面要以:結尾,如" family:"
     49
     50{{{
     51#!java
     52HBaseAdmin admin = new HBaseAdmin(config);
     53
     54if (!admin.tableExists(table_name)) {
     55        System.out.println("HTable : " + table_name
     56                        + "  creating ... please wait");
     57        HTableDescriptor tableDesc = new HTableDescriptor(table_name);
     58        // add column family
     59        tableDesc.addFamily(new HColumnDescriptor(colomn_family));
     60        admin.createTable(tableDesc);
     61}
     62}}}
     63
     64 * 接著,我們用BatchUpdate來加入表單內容,我們首先用建構值填入hbase_row,接著用put放入值,用delete刪除值。
     65 * 此非必要條件(也就是沒有用到這個類別程式也能跑),但若是希望能新增或刪除資料的話還是得用到它
     66
     67 注意:這個類別不需要其他類別支援。
     68
     69  === 新增/刪除 資料表內容:BatchUpdate ===
     70 
     71{{{
     72#!java
     73
     74BatchUpdate batchUpdate = new BatchUpdate(hbase_row);
     75batchUpdate.put(colomn_family+column_quolify, Bytes.toBytes(value));
     76batchUpdate.delete(colomn_family+"cellIWantDeleted");
     77}}}
     78
     79  === 資料表內容:HTable ===
     80 
     81 * HTable 類別在此是用來把之前的建構的內容寫入 ,之後還會用這個類別取得資料表內容,因此也算是必要條件
     82 * HTable 類別用來連接HBaseConfiguration與BatchUpdate,如此完成表單建立
     83 這個類別填入HBaseConfiguration與table_name的建構值,並用commit這個函式輸入之前BatchUpdate建立的表單資料。
     84
     85{{{
     86#!java
     87HTable table = new HTable(config, table_name);
     88table.commit(batchUpdate);
     89}}}
     90
     91 * 如果你已經知道資料表中的 "row" "colomn_family" "column_quolify" 的資訊,可以用下列方法把該cell的值印出來。
     92
     93 注意: Hbase內的資料屬性雖然是byte,但要用 org.apache.hadoop.hbase.util.Bytes 的 toString(值) 才可以轉出正確格式
     94 
     95{{{
     96#!java
     97
     98Cell cell = table.get(hbase_row, colomn_family+column_quolify);
     99// System.out.print(cell.getValue()); 會印出亂碼,因為資料格式不正確
     100System.out.print(Bytes.toString(cell.getValue()));  // cell value = yes ! good !
     101
     102}}}
     103
     104  === 掃描資料表:Scanner ===
     105 
     106 * 接下來的程式碼用來印出資料表內容,因此需要用到HTable
     107 * Scanner連接到HBase後,怎麼把內容萃取出來呢? 用到 RowResult承接 Scanner.next()方法,然而一個row裡面,會有
     108 許多colomn_family,每個colomn_family又會有自己的column_quolify,形成colomn_family:column_quolify的二維結構。
     109   * getRow() : 把row的名稱秀出來
     110   * get(colomn_family:column_quolify) : 把該cell的值秀出來
     111 * for (RowResult result : scanner)  可以看成是
     112 for (int i=0; i< scanner.length; i++){ RowResult result[i] = scanner[i];
     113 
     114 
     115{{{
     116#!java
     117Scanner scanner = table.getScanner(new String[] { colomn_family+column_quolify });
     118
     119RowResult rowResult = scanner.next();
     120
     121while (rowResult != null) {
     122
     123        System.out.println("Found row: "
     124                        + Bytes.toString(rowResult.getRow())
     125                        + " with value: "
     126                        + rowResult.get(Bytes
     127                                        .toBytes(colomn_family+column_quolify)));
     128        rowResult = scanner.next();
     129}
     130
     131for (RowResult result : scanner) {
     132        System.out.println("Found row: "
     133                        + Bytes.toString(rowResult.getRow())
     134                        + " with value: "
     135                        + rowResult.get(Bytes
     136                                        .toBytes(colomn_family+column_quolify)));
     137}
     138
     139scanner.close();
    115140}}}
    116141