HBase Java API 使用示例

在使用HBase Java API 之前,大家首先要了解HBase Java API类,可参考博客:http://www.cnblogs.com/ggjucheng/p/3380267.html

几个相关类与HBase数据模型之间的对应关系

java类 HBase数据模型
HBaseAdmin 数据库(DataBase)
HBaseConfiguration
HTable 表(Table)
HTableDescriptor 列族(Column Family)
Put 列修饰符(Column Qualifier)
Get
Scanner

HBaseConfiguration:对HBase进行配置。

HBaseAdmin:提供了一个接口来管理HBase数据库的表信息。它提供的方法包括:创建表,删除表,列出表项,使表有效或无效,以及添加或删除表列族成员等。

HTableDescriptor:包含了表的名字极其对应表的列族

HColumnDescriptor:维护着关于列族的信息,例如版本号,压缩设置等。它通常在创建表或者为表添加列族的时候使用。列族被创建后不能直接修改,只能通过删除然后重新创建的方式。列族被删除的时候,列族里面的数据也会同时被删除。

HTable:可以用来和HBase表直接通信。此方法对于更新操作来说是非线程安全的。

Put:用来对单个行执行添加操作。

Get:用来获取单个行的相关信息。

Result:存储Get或者Scan操作后获取表的单行值。使用此类提供的方法可以直接获取值或者各种Map结构(key-value对)。

ResultScanner:客户端获取值的接口。

代码示例:
public class HBaseDemo {

	private Configuration conf = null;
	
	@Before
	public void init(){
		conf = HBaseConfiguration.create();
		//客户端连接zookeeper
		conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
	}
	
	@Test
	public void testDrop() throws Exception{
		
		HBaseAdmin admin = new HBaseAdmin(conf);
		admin.disableTable("account");
		admin.deleteTable("account");
		admin.close();
	}
	/**
	*插入数据
	*/
	@Test
	public void testPut() throws Exception{
		HTable table = new HTable(conf, "user");
		//提供row key
		Put put = new Put(Bytes.toBytes("rk0003"));
		//列族、列的标示符、值,参数都是字节数组
		put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("liuyan"));
		table.put(put);
		table.close();
	}
	/**
	*获取数据
	*/
	@Test
	public void testGet() throws Exception{
		//HTablePool pool = new HTablePool(conf, 10);
		//HTable table = (HTable) pool.getTable("user");
		HTable table = new HTable(conf, "user");
		Get get = new Get(Bytes.toBytes("rk0001"));
		//get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
		get.setMaxVersions(5);
		Result result = table.get(get);
		
	  //String r = Bytes.toString(result.getValue(family, qualifier) );这个获取方法不常用
		
		for(KeyValue kv : result.list()){
			String family = new String(kv.getFamily());
			System.out.println(family);
			String qualifier = new String(kv.getQualifier());
			System.out.println(qualifier);
			System.out.println(new String(kv.getValue()));
		}
		table.close();
	}
	
	@Test
	public void testScan() throws Exception{
		HTablePool pool = new HTablePool(conf, 10);
		HTableInterface table = pool.getTable("user");
		Scan scan = new Scan(Bytes.toBytes("rk0001"), Bytes.toBytes("rk0002"));
		scan.addFamily(Bytes.toBytes("info"));
		//结果集 
		ResultScanner scanner = table.getScanner(scan);
		for(Result r : scanner){
			/**
			for(KeyValue kv : r.list()){
				String family = new String(kv.getFamily());
				System.out.println(family);
				String qualifier = new String(kv.getQualifier());
				System.out.println(qualifier);
				System.out.println(new String(kv.getValue()));
			}
			*/
			byte[] value = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
			System.out.println(new String(value));
		}
		pool.close();
	}
	
	
	@Test
	public void testDel() throws Exception{
		HTable table = new HTable(conf, "user");
		Delete del = new Delete(Bytes.toBytes("rk0001"));
		del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));
		table.delete(del);
		table.close();
	}
	
	
	
	
	public static void main(String[] args) throws Exception {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
		//ddl操作,传入conf,可知操作哪个hbase集群
		HBaseAdmin admin = new HBaseAdmin(conf);
		HTableDescriptor td = new HTableDescriptor("account");
		HColumnDescriptor cd = new HColumnDescriptor("info");
		cd.setMaxVersions(10);
		td.addFamily(cd);
		admin.createTable(td);
		admin.close();

	}
	
	public void createTable(String tableName, int maxVersion, String... cf){
		
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值