HBase-scan API 通过scan读取表中数据

本文提供了一个使用Java API进行HBase表扫描的示例代码,介绍了如何配置连接参数、创建扫描实例并设置扫描范围,最后展示了如何遍历扫描结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接贴代码啦

[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.           
  12.         HTablePool pool;  
  13.         Configuration config = HBaseConfiguration.create();  
  14.         config.set("hbase.zookeeper.quorum",zkIp);//  
  15.         config.set("hbase.zookeeper.property.clientPort", zkPort);  
  16.         pool = new HTablePool(config, 2);  
  17.           
  18.         HTableInterface hbTable = null;  
  19.         try {  
  20.             hbTable = pool.getTable(tablename); // 表名  
  21.             ResultScanner rs = null;  
  22.             Scan scan = new Scan();  
  23.             // scan.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes("qual1"));扫某一列  
  24.             if (startRow != null) { // 设置扫描的范围  
  25.                 scan.setStartRow(Bytes.toBytes(startRow));  
  26.             }  
  27.             if (stopRow != null) {  
  28.                 scan.setStopRow(Bytes.toBytes(stopRow));  
  29.             }  
  30.   
  31.             rs = hbTable.getScanner(scan);  
  32.             hbTable.close();  
  33.             for (Result r : rs) {// 按行去遍历  
  34.                 for (KeyValue kv : r.raw()) {// 遍历每一行的各列  
  35.                     StringBuffer sb = new StringBuffer()  
  36.                             .append(Bytes.toString(kv.getRow())).append("\t")  
  37.                             .append(Bytes.toString(kv.getFamily()))  
  38.                             .append("\t")  
  39.                             .append(Bytes.toString(kv.getQualifier()))  
  40.                             .append("\t").append(Bytes.toString(kv.getValue()));  
  41.                     System.out.println(sb.toString());  
  42.                     // kv.getRow() key  
  43.                     // kv.getFamily() cf1  
  44.                     // kv.getQualifier() 列名  
  45.                     // kv.getValue() value  
  46.   
  47.                 }  
  48.   
  49.             }  
  50.   
  51.         } catch (Exception e) {  
  52.             System.out.println(e.getMessage());  
  53.         }finally{  
  54.             pool.close();  
  55.         }  
  56.             
  57.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值