码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-07-06 09:04:44      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:hbase   scan   

直接贴代码啦

/**
	 * 
	 * @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();
		}
	      
	}


HBase-scan API 通过scan读取表中数据,布布扣,bubuko.com

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

标签:hbase   scan   

原文地址:http://blog.csdn.net/smile0198/article/details/37069863

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!