HBase-scan API 通過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();
		}
	      
	}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM