HBase-scan API 通過scan讀取表中資料

來源:互聯網
上載者:User

直接貼代碼啦

/** *  * @param zkIp * @param zkPort * @param tablename * @param startRow   傳null掃全表 * @param stopRow 已~結尾 * @throws Exception */public static void scanTable(String zkIp,String zkPort,String tablename,String startRow,String stopRow) throws Exception {HTablePool pool;Configuration config = HBaseConfiguration.create();config.set("hbase.zookeeper.quorum",zkIp);//config.set("hbase.zookeeper.property.clientPort", zkPort);pool = new HTablePool(config, 2);HTableInterface hbTable = null;try {hbTable = pool.getTable(tablename); // 表名ResultScanner rs = null;Scan scan = new Scan();// scan.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes("qual1"));掃某一列if (startRow != null) { // 設定掃描的範圍scan.setStartRow(Bytes.toBytes(startRow));}if (stopRow != null) {scan.setStopRow(Bytes.toBytes(stopRow));}rs = hbTable.getScanner(scan);hbTable.close();for (Result r : rs) {// 按行去遍曆for (KeyValue kv : r.raw()) {// 遍曆每一行的各列StringBuffer sb = new StringBuffer().append(Bytes.toString(kv.getRow())).append("\t").append(Bytes.toString(kv.getFamily())).append("\t").append(Bytes.toString(kv.getQualifier())).append("\t").append(Bytes.toString(kv.getValue()));System.out.println(sb.toString());// kv.getRow() key// kv.getFamily() cf1// kv.getQualifier() 列名// kv.getValue() value}}} catch (Exception e) {System.out.println(e.getMessage());}finally{pool.close();}      }


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.