Changes between Version 1 and Version 2 of NCHCCloudCourse100929_4_HBEX4


Ignore:
Timestamp:
Sep 28, 2010, 7:04:21 PM (14 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • NCHCCloudCourse100929_4_HBEX4

    v1 v2  
    1010[wiki:NCHCCloudCourse100929_4_HBEX3 上一關 < ] 第四關 [wiki:NCHCCloudCourse100929_4_HBEX5 > 下一關]
    1111
    12  = 範例四: Scan all Column =
     12 = 範例四: 掃描並印出 Table 內的指定row =
    1313
    1414{{{
    1515#!java
     16
    1617package org.nchc.hbase;
    1718
     
    2829public class ScanTable {
    2930
    30         static void ScanColumn(String tablename, String family, String column) {
    31                 HBaseConfiguration conf = new HBaseConfiguration();
     31  static void ScanColumn(String tablename, String family, String column) {
     32    HBaseConfiguration conf = new HBaseConfiguration();
    3233
    33                 HTable table;
    34                 try {
    35                         table = new HTable(conf, Bytes.toBytes(tablename));
     34    HTable table;
     35    try {
     36      table = new HTable(conf, Bytes.toBytes(tablename));
    3637
    37                         ResultScanner scanner = table.getScanner(Bytes.toBytes(family));
     38      ResultScanner scanner = table.getScanner(Bytes.toBytes(family));
    3839
    39                         System.out.println("Scan the Table [" + tablename
    40                                         + "]'s Column => " + family + ":" + column);
    41                         int i = 1;
    42                         for (Result rowResult : scanner) {
    43                                 byte[] by = rowResult.getValue(Bytes.toBytes(family), Bytes
    44                                                 .toBytes(column));
    45                                 String str = Bytes.toString(by);
    46                                 System.out.println("row " + i + " is \"" + str + "\"");
    47                                 i++;
    48                         }
    49                 } catch (IOException e) {
    50                         e.printStackTrace();
    51                 }
    52         }
     40      System.out.println("Scan the Table [" + tablename
     41          + "]'s Column => " + family + ":" + column);
     42      int i = 1;
     43      for (Result rowResult : scanner) {
     44        byte[] by = rowResult.getValue(Bytes.toBytes(family), Bytes
     45            .toBytes(column));
     46        String str = Bytes.toString(by);
     47        System.out.println("row " + i + " is \"" + str + "\"");
     48        i++;
     49      }
     50    } catch (IOException e) {
     51      e.printStackTrace();
     52    }
     53  }
    5354
    54         public static void main(String[] argv) {
    55                 String[] args = new GenericOptionsParser(new Configuration(), argv)
    56                                 .getRemainingArgs();
    57                 if (args.length < 3) {
    58                         System.out
    59                                         .println("ScanColumn <TableName> <Family> <Qualifier> ");
    60                         return;
    61                 }
    62                 ScanColumn(args[0], args[1], args[2]);
    63         }
     55  public static void main(String[] argv) {
     56         
     57            // eclipse only
     58//        String[] argt = {"ex1Table", "Detail", "c1"};
     59//        argv = argt; 
     60         
     61    String[] args = new GenericOptionsParser(new Configuration(), argv)
     62        .getRemainingArgs();
     63    if (args.length < 3) {
     64      System.out
     65          .println("ScanColumn <TableName> <Family> <Qualifier> ");
     66      return;
     67    }
     68    ScanColumn(args[0], args[1], args[2]);
     69  }
    6470}
    6571
    6672}}}
    6773
     74 * 執行結果
    6875
     76{{{
     77Scan the Table [ex1Table]'s Column => Detail:c1
     78row 1 is "my value good"
     79}}}