[20] | 1 | /* |
---|
| 2 | * nchc hadoop hbase test |
---|
| 3 | */ |
---|
| 4 | |
---|
| 5 | package tw.org.nchc.demo; |
---|
| 6 | |
---|
| 7 | import java.io.IOException; |
---|
| 8 | import java.util.SortedMap; |
---|
| 9 | import java.util.TreeMap; |
---|
| 10 | |
---|
| 11 | import org.apache.hadoop.hbase.HBaseConfiguration; |
---|
| 12 | import org.apache.hadoop.hbase.HScannerInterface; |
---|
| 13 | import org.apache.hadoop.hbase.HStoreKey; |
---|
| 14 | import org.apache.hadoop.hbase.HTable; |
---|
| 15 | import 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 | |
---|
| 27 | 08/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 | +---------+-----------+-----------+ |
---|
| 33 | 1 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 | * |
---|
| 43 | Illustration of adding data... |
---|
| 44 | Writing row = 0, col 'CF:CI' = Hellow0 |
---|
| 45 | Writing row = 1, col 'CF:CI' = Hellow1 |
---|
| 46 | Writing row = 2, col 'CF:CI' = Hellow2 |
---|
| 47 | Writing row = 3, col 'CF:CI' = Hellow3 |
---|
| 48 | Writing row = 4, col 'CF:CI' = Hellow4 |
---|
| 49 | Writing row = 5, col 'CF:CI' = Hellow5 |
---|
| 50 | Writing row = 6, col 'CF:CI' = Hellow6 |
---|
| 51 | Writing row = 7, col 'CF:CI' = Hellow7 |
---|
| 52 | Writing row = 8, col 'CF:CI' = Hellow8 |
---|
| 53 | Writing row = 9, col 'CF:CI' = Hellow9 |
---|
| 54 | |
---|
| 55 | Illustration of querying... |
---|
| 56 | row = 1, 'CF : CI ' = Hellow1 |
---|
| 57 | |
---|
| 58 | Illustration of scanning... |
---|
| 59 | 08/06/03 16:47:51 INFO hbase.HTable: Creating scanner over test_table starting at key |
---|
| 60 | row = 0//9223372036854775807, col 'CF:CI' = Hellow0 |
---|
| 61 | row = 1//9223372036854775807, col 'CF:CI' = Hellow1 |
---|
| 62 | row = 2//9223372036854775807, col 'CF:CI' = Hellow2 |
---|
| 63 | row = 3//9223372036854775807, col 'CF:CI' = Hellow3 |
---|
| 64 | row = 4//9223372036854775807, col 'CF:CI' = Hellow4 |
---|
| 65 | row = 5//9223372036854775807, col 'CF:CI' = Hellow5 |
---|
| 66 | row = 6//9223372036854775807, col 'CF:CI' = Hellow6 |
---|
| 67 | row = 7//9223372036854775807, col 'CF:CI' = Hellow7 |
---|
| 68 | row = 8//9223372036854775807, col 'CF:CI' = Hellow8 |
---|
| 69 | row = 9//9223372036854775807, col 'CF:CI' = Hellow9 |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | * |
---|
| 73 | */ |
---|
| 74 | public 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 | } |
---|