source: sample/hadoop-0.16/tw/org/nchc/demo/DemoHBaseClient.java @ 234

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

hadoop 0.16

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