JAVA API 操作HBASE(二)

package hbase;

import java.io.IOException;

import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;

/**
 * @author Administrator
 *
 */
public class HBaseDML extends HBaseAb {
	
	/**
	 * 表连接池
	 */
	private static  HTablePool _tPool = null;
	
	/**
	 * 表连接池中最大初始化连接数量
	 */
	private static int maxSize = 1000;
	
	static{
		_tPool = new HTablePool(conf, maxSize);
	}
	
	/**
	 * 根据表名和列标识查找一列的数据
	 * @param tableName
	 * @param key
	 * @throws IOException
	 */
	public void getOneRow(String tableName,String key) throws IOException{
		HTableInterface table = _tPool.getTable(tableName);
		Get get = new Get(key.getBytes());
		Result res = table.get(get);
		for (KeyValue entry : res.raw()) {
			System.out.println("*******************************************************************");
			System.out.println("Family:"+new String(entry.getFamily(), "UTF-8"));
			System.out.println("Row:"+new String(entry.getRow(),"UTF-8"));
			System.out.println("Qualifier:"+new String(entry.getQualifier(), "UTF-8"));
			System.out.println("Timestamp:"+entry.getTimestamp());
			System.out.println("Value:"+new String(entry.getValue(),"UTF-8"));
			System.out.println("*******************************************************************");
		}
	}
	
	/**
	 * 得到一张表中的所有数据
	 * @param tableName
	 * @throws IOException 
	 */
	public void getAllRows(String tableName) throws IOException{
		HTableInterface table = _tPool.getTable(tableName);
		Scan scan = new Scan();
		ResultScanner rs = table.getScanner(scan);
		for (Result result : rs) {
			System.out.println("_________________________________________________________________");
			for (KeyValue entry : result.raw()) {
				System.out.println("*******************************************************************");
				System.out.println("Family:"+new String(entry.getFamily(), "UTF-8"));
				System.out.println("Row:"+new String(entry.getRow(),"UTF-8"));
				System.out.println("Qualifier:"+new String(entry.getQualifier(), "UTF-8"));
				System.out.println("Timestamp:"+entry.getTimestamp());
				System.out.println("Value:"+new String(entry.getValue(),"UTF-8"));
				System.out.println("*******************************************************************");
			}
			
			System.out.println("_________________________________________________________________");
		}
	}
	
	/**
	 * 删除一行数据
	 * @param tableName
	 * @param key
	 * @throws IOException
	 */
	public void deleteOneRow(String tableName,String key) throws IOException{
		HTableInterface table = _tPool.getTable(tableName);
		Delete delete = new Delete(key.getBytes());
		table.delete(delete);
	}
	
	/**
	 * 添加一行数据
	 * @param tableName
	 * @param key
	 * @param family
	 * @param qualifier
	 * @param value
	 * @throws IOException
	 */
	public void addOneRow(String tableName,String key,String family,String qualifier,String value) throws IOException{
		HTableInterface table = _tPool.getTable(tableName);
		Put put =  new Put(key.getBytes());
		put.add(family.getBytes(), qualifier.getBytes(), value.getBytes());
		table.put(put);
	}
	
	
	public static void main(String[] args) {
		HBaseDML hbase_dml = new HBaseDML();
		
		try {
//			hbase_dml.getOneRow("user", "zhuss");
			hbase_dml.getAllRows("user");
//			hbase_dml.addOneRow("user", "zhushunshan", "address", "Local", "河南省淮阳县朱庄");
			
			
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		
		
	}
	

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值