Changes between Initial Version and Version 1 of hadoop_hbase_sample1


Ignore:
Timestamp:
Jun 3, 2008, 6:27:22 PM (16 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • hadoop_hbase_sample1

    v1 v1  
     1 = Sample Code of Show Hbase Write&Read by Map-Reduce " =
     2 == Abstract ==
     3The demo program will insert values to " Column Family: Column Identify" and then print these.
     4
     5 *  pre-do : create a hbase table "test_table" with (CF:CI) which (" column family ", " column ID ")  [[BR]]
     6
     7 1 $ bin/hbase shell [[BR]]
     8 2 > create table test_table("CF"); [[BR]]
     9 ok ! we can test it  [[BR]]
     10 3 > insert into test_table("CF:CI") values=("Hellow World") where row = "1"; [[BR]]
     11 4 > select * from test_table;  [[BR]]
     12{{{
     1308/06/03 16:16:36 INFO hbase.HTable: Creating scanner over test_table starting at key
     14+---------+-----------+-----------+
     15| Row                   | Column                  | Cell                     |
     16+---------+-----------+-----------+
     17| 1                        | CF:CI                     | Hellow World      |
     18+---------+-----------+-----------+
     191 row(s) in set. (0.24 sec)
     20}}}
     21
     22 on the  structure , '''Row''' means Row ID which is a key to describe a column; [[br]]
     23 '''Column''' means the database structure in test_table, [[br]]
     24 Column Family , "CF",  should be defined while creating table. [[br]]
     25 Column Id , "CI" , can be added dynamically. [[br]]
     26 '''Cell''' is the value of CF:CI [[br]]
     27
     28 == Output ==
     29
     30 That's the structure; then the demo program will show you in console as below :
     31
     32{{{
     33Illustration of adding data...
     34Writing row = 0, col 'CF:CI' = Hellow0
     35Writing row = 1, col 'CF:CI' = Hellow1
     36Writing row = 2, col 'CF:CI' = Hellow2
     37Writing row = 3, col 'CF:CI' = Hellow3
     38Writing row = 4, col 'CF:CI' = Hellow4
     39Writing row = 5, col 'CF:CI' = Hellow5
     40Writing row = 6, col 'CF:CI' = Hellow6
     41Writing row = 7, col 'CF:CI' = Hellow7
     42Writing row = 8, col 'CF:CI' = Hellow8
     43Writing row = 9, col 'CF:CI' = Hellow9
     44
     45Illustration of querying...
     46row = 1, 'CF : CI ' = Hellow1
     47
     48Illustration of scanning...
     4908/06/03 16:47:51 INFO hbase.HTable: Creating scanner over test_table starting at key
     50row = 0//9223372036854775807, col 'CF:CI' = Hellow0
     51row = 1//9223372036854775807, col 'CF:CI' = Hellow1
     52row = 2//9223372036854775807, col 'CF:CI' = Hellow2
     53row = 3//9223372036854775807, col 'CF:CI' = Hellow3
     54row = 4//9223372036854775807, col 'CF:CI' = Hellow4
     55row = 5//9223372036854775807, col 'CF:CI' = Hellow5
     56row = 6//9223372036854775807, col 'CF:CI' = Hellow6
     57row = 7//9223372036854775807, col 'CF:CI' = Hellow7
     58row = 8//9223372036854775807, col 'CF:CI' = Hellow8
     59row = 9//9223372036854775807, col 'CF:CI' = Hellow9
     60}}}
     61
     62 == code ==
     63
     64{{{
     65/*
     66 *  DemoHBaseClient.java
     67 */
     68
     69// package tw.org.nchc.demo;
     70
     71import java.io.IOException;
     72import java.util.SortedMap;
     73import java.util.TreeMap;
     74
     75import org.apache.hadoop.hbase.HBaseConfiguration;
     76import org.apache.hadoop.hbase.HScannerInterface;
     77import org.apache.hadoop.hbase.HStoreKey;
     78import org.apache.hadoop.hbase.HTable;
     79import org.apache.hadoop.io.Text;
     80
     81
     82public class DemoHBaseClient {
     83
     84        public static void main(String[] args) throws IOException {
     85
     86                // Open the "test_table" table. If it hasn't been in Hbase, you should create.
     87                HBaseConfiguration conf = new HBaseConfiguration();
     88                HTable table = new HTable(conf, new Text("test_table"));
     89       
     90                System.out.println("Illustration of adding data...");
     91
     92                // create column formed  (Column Family:Column Id)
     93                Text column = new Text("CF:CI");
     94
     95                // create row_id
     96                Text row_id = new Text();
     97
     98                // demo 1  : Insert ten demo values
     99                for (int i = 0; i < 10; i++) {
     100                       
     101                        // give row_id  value
     102                        row_id.set(new Integer(i).toString());
     103                       
     104                        // let "indicate_id" indicate the column which row = row_id
     105                        long indicate_id= table.startUpdate(row_id);
     106                       
     107                        //val =  value of CF:CI where row_id = i
     108                        Text val = new Text("Hellow" + i);
     109
     110                        // put "val" to "column" from "table" where "row_id"
     111                        // the same as : 
     112                        // hql> INSERT INTO table( column ) VALUES=( val) WHERE ROW = row_id ;
     113                        table.put(indicate_id, column, val.getBytes());
     114                        table.commit(indicate_id);
     115
     116                        System.out.println("Writing row = " + row_id + ", col '" + column
     117                                        + "' = " + val);
     118                }
     119
     120                // demo 2 : print column value only row = 1 ;
     121                System.out.println("\n Querying row = 1");
     122               
     123                // Get a single value for the specified row and column
     124                // byte[] = HTable.get(Text row, Text column)
     125               
     126                String s = Text.decode(table.get(new Text("1"),new Text("CF:CI")));
     127                // if change as 
     128                // String s = (table.get(new Text("1"),new Text("CF:CI"))).toString();
     129                // will get chaos code "  [B@1f14ceb"
     130                System.out.println("row = 1, 'CF : CI ' = " + s);
     131
     132                // demo 3 :  Print the all contents of this table
     133                System.out.println("\nIllustration of scanning...");
     134
     135                // we only want one column, but you can specify multiple columns to
     136                // fetch at once
     137                Text[] cols = { column };
     138
     139                // Use HScannerInterface to crawl table
     140                HScannerInterface scanner = table.obtainScanner(cols, new Text());
     141
     142                // column values are stored in a Map
     143                SortedMap<Text, byte[]> values = new TreeMap<Text, byte[]>();
     144                HStoreKey currentKey = new HStoreKey();
     145                while (scanner.next(currentKey, values)) {
     146                        // decode the stored byte[] back into a String
     147                        String val = Text.decode(values.get(column));
     148                        System.out.println("row = " + currentKey + ", col '" + column + "' = "
     149                                        + val);
     150                }
     151
     152                // remember to close scanner when done
     153                scanner.close();
     154
     155        }
     156
     157}
     158
     159}}}