Java Hbase数据几种查询方式

/**Name		:	KcnaufHadoopManagement
 *Author	:	Zhang Bing 
 *Created	:	2013-7-31
 *Function	:	delete of hbase
 */
package com.exercise.hbase.command;

import java.util.LinkedList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.PrefixFilter;

public class HBaseQuery {
	public Configuration config;
	private String tablename;

	public String getTablename() {
		return tablename;
	}

	public void setTablename(String tablename) {
		this.tablename = tablename;
	}

	public HBaseQuery(String tablename) {
		this.tablename = tablename;
		config = ConfigurationHandle.getInstance();
	}
	
	/**
	 * This method can get all rows data from hbase by the same rowkey 
	 * @param rowkey
	 * @return
	 * @throws Exception
	 */
	public List<Record> getDataByRowkey(String rowkey) throws Exception {
		List<Record> recordList = null;
		try {
			recordList = new LinkedList<Record>();
			HTable table = new HTable(config, tablename);
			Get get = new Get(rowkey.getBytes());
			Result rs = null;
			rs = table.get(get);
			
			KeyValue[] kv = rs.raw();
			for (int i = 0; i < kv.length; i++) {
				recordList.add(new Record(
						new String(kv[i].getRow(), "UTF-8"),
						new String(kv[i].getFamily(), "UTF-8"),
						new String(kv[i].getQualifier(), "UTF-8"),
						new String(kv[i].getValue(), "UTF-8"),
						kv[i].getTimestamp()));
			}
			/*
			 * for(KeyValue kv:rs.raw()){ record=new
			 * String(kv.getValue(),"UTF-8"); }
			 */
			return recordList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * This method can get all rows data from hbase by the same rowkeyPrifix
	 * @param rowkeyPrifix
	 * @return
	 * @throws Exception
	 */
	public List<Record> getDataByRowkeyPrifix(String rowkeyPrifix) throws Exception {
		List<Record> recordList = new LinkedList<Record>();
		try {
			HTable table = new HTable(config, tablename);
			Scan scan = new Scan();
			scan.setFilter(new PrefixFilter(rowkeyPrifix.getBytes()));
			ResultScanner rs = table.getScanner(scan);
			
			for (Result r : rs) {
				KeyValue[] kv = r.raw();
				for (int i = 0; i < kv.length; i++) {
					recordList.add(new Record(
							new String(kv[i].getRow(), "UTF-8"),
							new String(kv[i].getFamily(), "UTF-8"),
							new String(kv[i].getQualifier(), "UTF-8"),
							new String(kv[i].getValue(), "UTF-8"),
							kv[i].getTimestamp()));
				}
			}
			rs.close();
			return recordList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * This method can get all rows data from hbase by the same family and qualify
	 * @param family
	 * @param qualify
	 * @return
	 */
	public List<Record> getDataByFamily(String family, String qualify) {
		List<Record> recordList = new LinkedList<Record>();
		try {
			HTable table = new HTable(config, tablename);
			ResultScanner rs = table.getScanner(family.getBytes(),
					qualify.getBytes());
			for (Result r : rs) {
				KeyValue[] kv = r.raw();
				for (int i = 0; i < kv.length; i++) {
					recordList.add(new Record(
							new String(kv[i].getRow(), "UTF-8"),
							new String(kv[i].getFamily(), "UTF-8"),
							new String(kv[i].getQualifier(), "UTF-8"),
							new String(kv[i].getValue(), "UTF-8"),
							kv[i].getTimestamp()));
				}
			}
			rs.close();
			return recordList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * This method can get data by the same timestamp
	 * @param timestamp
	 * @return
	 */
	public List<Record> getDataByTimestamp(long timestamp) {
		List<Record> recordList = new LinkedList<Record>();
		try {
			HTable table = new HTable(config, tablename);
			ResultScanner rs = table.getScanner(new Scan()
					.setTimeStamp(timestamp));
			for (Result r : rs) {
				KeyValue[] kv = r.raw();
				for (int i = 0; i < kv.length; i++) {
					recordList.add(new Record(
							new String(kv[i].getRow(), "UTF-8"),
							new String(kv[i].getFamily(), "UTF-8"),
							new String(kv[i].getQualifier(), "UTF-8"),
							new String(kv[i].getValue(), "UTF-8"),
							kv[i].getTimestamp()));
				}
			}
			rs.close();
			return recordList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值