Hbase原生态api总结

package com.dh.hbase.summary;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

public class OriginalHbaseAPI {
	public static Configuration conf = null;
    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.133.0.100");
    }
    
    /**
     * 创建一个hbase表
     * @param tableName 表名
     * @param columnfamily 列族名字
     * @return HTable实例
     * @throws Exception
     */
	@Test
	public void creatTable()throws Exception {
		String tableName = "Demo";
		String columnfamily = "Info";
		HBaseAdmin admin = new HBaseAdmin(conf);
		if (admin.tableExists(tableName)) {
			System.out.println("table already exists!");
		}else {
			HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
			tableDesc.addFamily(new HColumnDescriptor(columnfamily));
			admin.createTable(tableDesc);
			System.out.println("create table " + tableName + " ok.");
		}
	}
	
	//查询所有信息
	@Test
	public void queryAll(){
		HTable hTable = null;
		ResultScanner scann = null;
		try {
			hTable = new HTable(conf, "Demo");
			scann = hTable.getScanner(new Scan());
			for(Result rs : scann){
				System.out.println("该表RowKey为:"+new String(rs.getRow()));
				for(Cell cell : rs.rawCells()){
					System.out.println("rowKey:"+new String(CellUtil.cloneRow(cell)));
					System.out.println("列族:"+new String(CellUtil.cloneFamily(cell)));
					System.out.println("列名:"+new String(CellUtil.cloneQualifier(cell)));
					System.out.println("值为:"+new String(CellUtil.cloneValue(cell)));
				}
				System.out.println("==========================================");
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
				try {
					if(scann!=null){
						scann.close();
					}
					if(hTable!=null){
						hTable.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	
	//查询---按rowKey查询(单行)
	@Test
	public void queryByRowKey(){
		HTable hTable = null;
		try {
			hTable = new HTable(conf, "Demo");
			Result rs = hTable.get(new Get("d7".getBytes()));
			for(Cell cell : rs.rawCells()){
				//一个cell表示一个单元格,即关系型数据库中的一列
				System.out.println("rowKey为:"+new String(CellUtil.cloneRow(cell)));
				System.out.println("列族为:"+new String(CellUtil.cloneFamily(cell)));
				System.out.println("列名为:"+new String(CellUtil.cloneQualifier(cell)));
				System.out.println("值为:"+new String(CellUtil.cloneValue(cell)));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(hTable!=null){
				try {
					hTable.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
    //以列多条件查询,条件之间是并列关系
	public void queryByFilterList()throws IOException {
		String family = "Info";
		HTable hTable = new HTable(conf, "Demo");
		//MUST_PASS_ALL,---AND    同理:MUST_PASS_ONE,---OR
	    FilterList filterList = new FilterList(Operator.MUST_PASS_ALL);
	    Scan scan = new Scan(); 
	    //多条件的map,列名+列值
	    Map<String, String> columnAndValue = new HashMap<String, String>();
	    for (String column : columnAndValue.keySet()){
			//System.out.println("key= "+ key + " and value= " + columnAndValue.get(key));
			Filter filter = new SingleColumnValueFilter(Bytes.toBytes(family),
					Bytes.toBytes(column),CompareOp.EQUAL,Bytes.toBytes(columnAndValue.get(column)));//列等于某个值作为条件
			//以列模糊查询,提取列中包含某字符的数据    列包含某个值作为条件
			/*Filter filter = new SingleColumnValueFilter(Bytes.toBytes(family),
					Bytes.toBytes(column),CompareOp.EQUAL,new SubstringComparator(columnAndValue.get(column)));*/
			//同理:CompareOp.GREATER CompareOp.GREATER_OR_EQUAL CompareOp.LESS CompareOp.LESS_OR_EQUAL CompareOp.NOT_EQUAL
			filterList.addFilter(filter);
		}
	    scan.setFilter(filterList);
//	    scan.setMaxVersions()//默认为最新版本,如果加上这个可取多个版本,该方法可带参数数值n,表示取n版本
	    ResultScanner resultScanner = hTable.getScanner(scan);
	    for (Result result : resultScanner) {
			//一个result等于一行,一个cell等于一个单元格,即一列(一列可以有多个版本)
	    	Cell[] cells = result.rawCells();
	    	System.out.println("该表RowKey为:"+new String(result.getRow()));
	    	for(Cell cell:cells){
//	    		System.out.println("rowKey为:"+new String(CellUtil.cloneRow(cell)));
	    		System.out.println("列族为:"+new String(CellUtil.cloneFamily(cell)));
	    		System.out.println("列名为:"+new String(CellUtil.cloneQualifier(cell)));
	    		System.out.println("值为:"+new String(CellUtil.cloneValue(cell)));
	    	}
		}
	    
	}

	//行键包含某个字符串查询
	@Test
	public void getDataByRowKey() throws IOException{
		String value = "d";
		HTable hTable = new HTable(conf, "Demo");
		Filter filter = new RowFilter(CompareOp.EQUAL,new SubstringComparator(value));
		Scan scan = new Scan();
		scan.setFilter(filter);
		ResultScanner resultScanner = hTable.getScanner(scan);
	    for (Result result : resultScanner) {
			//一个result等于一行,一个cell等于一个单元格,即一列(一列可以有多个版本)
	    	Cell[] cells = result.rawCells();
	    	System.out.println("该表RowKey为:"+new String(result.getRow()));
	    	for(Cell cell:cells){
	    		System.out.println("列族为:"+new String(CellUtil.cloneFamily(cell)));
	    		System.out.println("列名为:"+new String(CellUtil.cloneQualifier(cell)));
	    		System.out.println("值为:"+new String(CellUtil.cloneValue(cell)));
	    	}
	    }
	}
	
	//以rowKey的某段范围进行查找
	@Test
	public void getDateByRowKeyRange() throws IOException{
		String startRow = "d1";
		String stopRow = "d3";
		HTable hTable = new HTable(conf, "Demo");
		Scan scan = new Scan();
		scan.setStartRow(Bytes.toBytes(startRow));
		scan.setStopRow(Bytes.toBytes(stopRow));
		ResultScanner resultScanner = hTable.getScanner(scan);
	    for (Result result : resultScanner) {
			//一个result等于一行,一个cell等于一个单元格,即一列(一列可以有多个版本)
	    	Cell[] cells = result.rawCells();
	    	System.out.println("该表RowKey为:"+new String(result.getRow()));
	    	for(Cell cell:cells){
	    		System.out.println("列族为:"+new String(CellUtil.cloneFamily(cell)));
	    		System.out.println("列名为:"+new String(CellUtil.cloneQualifier(cell)));
	    		System.out.println("值为:"+new String(CellUtil.cloneValue(cell)));
	    	}
	    }
	}

	//添加数据
	@Test
	public void addDatatoHbase() throws IOException{
		String tableName = "Demo";
		HTable hTable = new HTable(conf, tableName);
		Put put = new Put(Bytes.toBytes("rowKey"));
		put.add(Bytes.toBytes("Info"), Bytes.toBytes("text_aa"), Bytes.toBytes("aa"));
		put.add(Bytes.toBytes("Info"), Bytes.toBytes("text_bb"), Bytes.toBytes("bb"));
		put.add(Bytes.toBytes("Info"), Bytes.toBytes("text_bb"), Bytes.toBytes("bb"));
		hTable.put(put);
		//数据比较多的时候,建议使用批量插入:(可以使用循环new Put实例,放入List中,当循环到一定数,比如1000条,执行)详细了解断点续传的思想
		/*List<Put> puts = new ArrayList<Put>();
		int i = 0;
		for(){
			Put = new Put(row);
			...
			puts.add(put);
			i++;
			if(i==1000){
				hTable.put(puts);
				i=0;
			}
			//当然你要还考虑不够1000条时,如何处理,这里就不演示了,若知道总数,可以使用求余的方式
		}*/
	}

	//删除数据
	@Test
	public void testDelete() throws IOException{
		String tableName = "Demo";
		HTable hTable = new HTable(conf, tableName);
		String row = "rowkey";
		Delete delete = new Delete(Bytes.toBytes(row));
		hTable.delete(delete);
		//hTable.delete(List<Delete>);也可批量删除
	}

//记得该关闭的关闭,我这程序有些没关,就图省事;这套原生态的api,增删查改也够用了,至于分页,更多条件的查询啊,rowKey设计等等,后续更新吧...
}
另外:spring-apache hadoop (hbase)这个框架,后面有时间在整理上来吧~
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值