Changes between Version 1 and Version 2 of waue/2010/0125


Ignore:
Timestamp:
Jan 25, 2010, 5:13:29 PM (15 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2010/0125

    v1 v2  
    184184package hbase020;
    185185
    186 // from hbase website
     186
     187
     188// 說明:
     189// 此程式首先建立table, 然後put一個資料後
     190// 用get 方法取得資料,最後用scan方法取出整個資料表
     191//
     192// 參考: 程式碼改編自
    187193// http://hadoop.apache.org/hbase/docs/current/api/org/apache/.接.
    188194//  hadoop/hbase/client/package-summary.html#package_description
     
    205211// Does a Put, Get and a Scan against an hbase table.
    206212public class MyLittleHBaseClient {
     213       
     214        private static String table_name = "mytable";
     215       
     216        private static String the_row = "myLittleRow";
     217       
     218        private static String column_family = "family";
     219       
     220        private static String column_qualifier = "someQualifier";
     221       
     222        private static String cell_value = "somvalue";
     223       
     224       
     225       
    207226        public static void createHBaseTable(String tablename) throws IOException {
    208227                // HTableDescriptor contains the name of an HTable, and its column
     
    214233                // etc.
    215234                // HTableDescriptor 透過 add() 方法來加入Column family
    216                 htd.addFamily(new HColumnDescriptor("content:"));
     235                htd.addFamily(new HColumnDescriptor(column_family+":"));
    217236                // HBaseConfiguration 能接收 hbase-site.xml 的設定值
    218237                HBaseConfiguration config = new HBaseConfiguration();
     
    230249                admin.createTable(htd);
    231250        }
     251
    232252        public static void main(String[] args) throws IOException {
    233253                // You need a configuration object to tell the client where to connect.
    234254                // When you create a HBaseConfiguration, it reads in whatever you've set
    235255                // into your hbase-site.xml and in hbase-default.xml, as long as these
    236                 // can
    237                 // be found on the CLASSPATH
     256                // can be found on the CLASSPATH
     257               
    238258                HBaseConfiguration config = new HBaseConfiguration();
    239259
     260                // create table automatically
     261                createHBaseTable(table_name);
     262               
    240263                // This instantiates an HTable object that connects you to
    241264                // the "myLittleHBaseTable" table.
    242                 HTable table = new HTable(config, "myLittleHBaseTable");
     265                HTable table = new HTable(config, table_name);
     266
    243267
    244268                // To add to a row, use Put. A Put constructor takes the name of the row
     
    254278                // the timestamp to use in your update, etc.If no timestamp, the server
    255279                // applies current time to the edits.
    256                 Put p = new Put(Bytes.toBytes("myLittleRow"));
     280                Put p = new Put(Bytes.toBytes(the_row));
    257281
    258282                // To set the value you'd like to update in the row 'myRow', specify the
     
    264288                // hbase is all about byte arrays. Lets pretend the table
    265289                // 'myLittleHBaseTable' was created with a family 'myLittleFamily'.
    266                 p.add(Bytes.toBytes("myLittleFamily"), Bytes.toBytes("someQualifier"),
    267                                 Bytes.toBytes("Some Value"));
     290                p.add(Bytes.toBytes(column_family), Bytes.toBytes(column_qualifier),
     291                                Bytes.toBytes(cell_value));
    268292
    269293                // Once you've adorned your Put instance with all the updates you want
     
    280304                // up
    281305                // the hbase return into the form you find most palatable.
    282                 Get g = new Get(Bytes.toBytes("myLittleRow"));
     306                Get g = new Get(Bytes.toBytes(the_row));
    283307                Result r = table.get(g);
    284                 byte[] value = r.getValue(Bytes.toBytes("myLittleFamily"), Bytes
    285                                 .toBytes("someQualifier"));
     308                byte[] value = r.getValue(Bytes.toBytes(column_family), Bytes
     309                                .toBytes(column_qualifier));
    286310                // If we convert the value bytes, we should get back 'Some Value', the
    287311                // value we inserted at this location.
     
    296320                // and a Get, create a Scan. Adorn it with column names, etc.
    297321                Scan s = new Scan();
    298                 s.addColumn(Bytes.toBytes("myLittleFamily"), Bytes
    299                                 .toBytes("someQualifier"));
     322                s.addColumn(Bytes.toBytes(column_family), Bytes
     323                                .toBytes(column_qualifier));
    300324                ResultScanner scanner = table.getScanner(s);
    301325                try {
     
    303327                        // Now, for the actual iteration. One way is to use a while loop
    304328                        // like so:
    305                         for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
     329                        for (Result rr : scanner) {
    306330                                // print out the row we found and the columns we were looking
    307331                                // for