直接贴代码啦

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**
  2.      * 
  3.      * @param zkIp
  4.      * @param zkPort
  5.      * @param tablename
  6.      * @param startRow   传null扫全表
  7.      * @param stopRow 已~结尾
  8.      * @throws Exception
  9.      */
  10.     public static void scanTable(String zkIp,String zkPort,String tablename,String startRow,String stopRow) throws Exception {
  11.         HTablePool pool;
  12.         Configuration config = HBaseConfiguration.create();
  13.         config.set(“hbase.zookeeper.quorum”,zkIp);//
  14.         config.set(“hbase.zookeeper.property.clientPort”, zkPort);
  15.         pool = new HTablePool(config, 2);
  16.         HTableInterface hbTable = null;
  17.         try {
  18.             hbTable = pool.getTable(tablename); // 表名
  19.             ResultScanner rs = null;
  20.             Scan scan = new Scan();
  21.             // scan.addColumn(Bytes.toBytes(“cf1”),Bytes.toBytes(“qual1”));扫某一列
  22.             if (startRow != null) { // 设置扫描的范围
  23.                 scan.setStartRow(Bytes.toBytes(startRow));
  24.             }
  25.             if (stopRow != null) {
  26.                 scan.setStopRow(Bytes.toBytes(stopRow));
  27.             }
  28.             rs = hbTable.getScanner(scan);
  29.             hbTable.close();
  30.             for (Result r : rs) {// 按行去遍历
  31.                 for (KeyValue kv : r.raw()) {// 遍历每一行的各列
  32.                     StringBuffer sb = new StringBuffer()
  33.                             .append(Bytes.toString(kv.getRow())).append(“\t”)
  34.                             .append(Bytes.toString(kv.getFamily()))
  35.                             .append(“\t”)
  36.                             .append(Bytes.toString(kv.getQualifier()))
  37.                             .append(“\t”).append(Bytes.toString(kv.getValue()));
  38.                     System.out.println(sb.toString());
  39.                     // kv.getRow() key
  40.                     // kv.getFamily() cf1
  41.                     // kv.getQualifier() 列名
  42.                     // kv.getValue() value
  43.                 }
  44.             }
  45.         } catch (Exception e) {
  46.             System.out.println(e.getMessage());
  47.         }finally{
  48.             pool.close();
  49.         }
  50.     }