source: sample/hadoop-0.16/tw/org/nchc/code/HBaseClient.java @ 234

Last change on this file since 234 was 23, checked in by waue, 16 years ago

continue the versions

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