hbase 2.0.2 java api的简单使用


package com.hbase.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.FilterList.Operator;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HbaseTest {
	
	Configuration conf = null;
	Connection conn = null;
	@Before
	public void getConfigAndConnection() {
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "bigdata01,bigdata02,bigdata03");
		conf.set("hbase.zookeeper.property.clientPort", "2181");
		try {
			conn = ConnectionFactory.createConnection(conf);
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
	@Test
	public void createTable() throws IOException {
			Admin admin = conn.getAdmin();
			if(!admin.isTableAvailable(TableName.valueOf("test"))) {
				TableName tableName = TableName.valueOf("test");
				//表描述器构造器
				TableDescriptorBuilder  tdb  =TableDescriptorBuilder.newBuilder(tableName)  ;
				//列族描述起构造器
				ColumnFamilyDescriptorBuilder cdb =  ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("user"));
				//获得列描述起
				ColumnFamilyDescriptor  cfd = cdb.build();
				//添加列族
				tdb.setColumnFamily(cfd);
				//获得表描述器
				TableDescriptor td = tdb.build();
				//创建表
				//admin.addColumnFamily(tableName, cfd); //给标添加列族
				admin.createTable(td);
			}
			//关闭链接
	}
	//单条插入
	@Test
	public void insertOneData() throws IOException {
		//new 一个列  ,hgs_000为row key
		Put put = new Put(Bytes.toBytes("hgs_000"));
		//下面三个分别为,列族,列名,列值
		put.addColumn(Bytes.toBytes("testfm"),Bytes.toBytes("name") , Bytes.toBytes("hgs"));
		TableName tableName = TableName.valueOf("test");
		//得到 table
		Table table = conn.getTable(tableName);
		//执行插入
		table.put(put);				
	}
	//插入多个列
	@Test
	public void insertManyData() throws IOException {
		Table table = conn.getTable(TableName.valueOf("test"));
		List<Put> puts = new ArrayList<Put>();
		Put put1 = new Put(Bytes.toBytes("hgs_001"));
		put1.addColumn(Bytes.toBytes("testfm"),Bytes.toBytes("name") , Bytes.toBytes("wd"));
		
		Put put2 = new Put(Bytes.toBytes("hgs_001"));
		put2.addColumn(Bytes.toBytes("testfm"),Bytes.toBytes("age") , Bytes.toBytes("25"));
		
		Put put3 = new Put(Bytes.toBytes("hgs_001"));
		put3.addColumn(Bytes.toBytes("testfm"),Bytes.toBytes("weight") , Bytes.toBytes("60kg"));
		
		Put put4 = new Put(Bytes.toBytes("hgs_001"));
		put4.addColumn(Bytes.toBytes("testfm"),Bytes.toBytes("sex") , Bytes.toBytes("男"));
		puts.add(put1);
		puts.add(put2);
		puts.add(put3);
		puts.add(put4);		
		table.put(puts);
		table.close();
}
	//同一条数据的插入
	@Test
	public void singleRowInsert() throws IOException {
		Table table = conn.getTable(TableName.valueOf("test"));
		
		Put put1 = new Put(Bytes.toBytes("hgs_005"));
		
		put1.addColumn(Bytes.toBytes("testfm"),Bytes.toBytes("name") , Bytes.toBytes("cm"));		
		put1.addColumn(Bytes.toBytes("testfm"),Bytes.toBytes("age") , Bytes.toBytes("22"));		
		put1.addColumn(Bytes.toBytes("testfm"),Bytes.toBytes("weight") , Bytes.toBytes("88kg"));
		put1.addColumn(Bytes.toBytes("testfm"),Bytes.toBytes("sex") , Bytes.toBytes("男"));	
		
		table.put(put1);
		table.close();
	}
	//数据的更新,hbase对数据只有追加,没有更新,但是查询的时候会把最新的数据返回给哦我们
	@Test
	public void updateData() throws IOException {
		Table table = conn.getTable(TableName.valueOf("test"));
		Put put1 = new Put(Bytes.toBytes("hgs_002"));
		put1.addColumn(Bytes.toBytes("testfm"),Bytes.toBytes("weight") , Bytes.toBytes("63kg"));
		table.put(put1);
		table.close();
	}
	
	//删除数据
	@Test
	public void deleteData() throws IOException {
		Table table = conn.getTable(TableName.valueOf("test"));
		//参数为 row key
		//删除一列
		Delete delete1 = new Delete(Bytes.toBytes("hgs_000"));
		delete1.addColumn(Bytes.toBytes("testfm"), Bytes.toBytes("weight"));
		//删除多列
		Delete delete2 = new Delete(Bytes.toBytes("hgs_001"));
		delete2.addColumns(Bytes.toBytes("testfm"), Bytes.toBytes("age"));
		delete2.addColumns(Bytes.toBytes("testfm"), Bytes.toBytes("sex"));
		//删除某一行的列族内容
		Delete delete3 = new Delete(Bytes.toBytes("hgs_002"));
		delete3.addFamily(Bytes.toBytes("testfm"));
		
		//删除一整行
		Delete delete4 = new Delete(Bytes.toBytes("hgs_003"));
		table.delete(delete1);
		table.delete(delete2);
		table.delete(delete3);
		table.delete(delete4);
		table.close();
	}
	
	//查询
	//
	@Test
	public void querySingleRow() throws IOException {
		Table table = conn.getTable(TableName.valueOf("test"));
		//获得一行
		Get get = new Get(Bytes.toBytes("hgs_000"));
		Result set = table.get(get);
		Cell[] cells  = set.rawCells();
		for(Cell cell : cells) {
			System.out.println(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())+"::"+
		                    Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
		}
		table.close();
		//Bytes.toInt(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("password")))
		
	}
	//全表扫描
	@Test
	public void scanTable() throws IOException {
		Table table = conn.getTable(TableName.valueOf("test"));
		Scan scan = new Scan();
		//scan.addFamily(Bytes.toBytes("info"));
		//scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("password"));
		//scan.setStartRow(Bytes.toBytes("wangsf_0"));
		//scan.setStopRow(Bytes.toBytes("wangwu"));
		ResultScanner rsacn = table.getScanner(scan);
		for(Result rs:rsacn) {
			String rowkey = Bytes.toString(rs.getRow());
			System.out.println("row key :"+rowkey);
			Cell[] cells  = rs.rawCells();
			for(Cell cell : cells) {
				System.out.println(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())+"::"+
			                    Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
			}
			System.out.println("-----------------------------------------");
		}
	}
	//过滤器
	
	@Test
	//列值过滤器
	public void singColumnFilter() throws IOException {
		Table table = conn.getTable(TableName.valueOf("test"));
		Scan scan = new Scan();
		//下列参数分别为,列族,列名,比较符号,值
		SingleColumnValueFilter filter =  new SingleColumnValueFilter( Bytes.toBytes("testfm"),  Bytes.toBytes("name"),
                 CompareOperator.EQUAL,  Bytes.toBytes("wd")) ;
		scan.setFilter(filter);
		ResultScanner scanner = table.getScanner(scan);
		for(Result rs:scanner) {
			String rowkey = Bytes.toString(rs.getRow());
			System.out.println("row key :"+rowkey);
			Cell[] cells  = rs.rawCells();
			for(Cell cell : cells) {
				System.out.println(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())+"::"+
			                    Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
			}
			System.out.println("-----------------------------------------");
		}
	}
	//row key过滤器
	@Test
	public void rowkeyFilter() throws IOException {
		Table table = conn.getTable(TableName.valueOf("test"));
		Scan scan = new Scan();
		RowFilter filter = new RowFilter(CompareOperator.EQUAL,new RegexStringComparator("^hgs_00*"));
		scan.setFilter(filter);
		ResultScanner scanner  = table.getScanner(scan);
		for(Result rs:scanner) {
			String rowkey = Bytes.toString(rs.getRow());
			System.out.println("row key :"+rowkey);
			Cell[] cells  = rs.rawCells();
			for(Cell cell : cells) {
				System.out.println(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())+"::"+
			                    Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
			}
			System.out.println("-----------------------------------------");
		}
	}
	//列名前缀过滤器
	@Test
	public void columnPrefixFilter() throws IOException {
		Table table = conn.getTable(TableName.valueOf("test"));
		Scan scan = new Scan();
		ColumnPrefixFilter filter = new ColumnPrefixFilter(Bytes.toBytes("name"));
		scan.setFilter(filter);
		ResultScanner scanner  = table.getScanner(scan);
		for(Result rs:scanner) {
			String rowkey = Bytes.toString(rs.getRow());
			System.out.println("row key :"+rowkey);
			Cell[] cells  = rs.rawCells();
			for(Cell cell : cells) {
				System.out.println(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())+"::"+
			                    Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
			}
			System.out.println("-----------------------------------------");
		}
	}
	
	//过滤器集合
	@Test
	public void FilterSet() throws IOException {
		Table table = conn.getTable(TableName.valueOf("test"));
		Scan scan = new Scan();
		FilterList list = new FilterList(Operator.MUST_PASS_ALL);
		SingleColumnValueFilter filter1 =  new SingleColumnValueFilter( Bytes.toBytes("testfm"),  Bytes.toBytes("age"),
                CompareOperator.GREATER,  Bytes.toBytes("23")) ;
		ColumnPrefixFilter filter2 = new ColumnPrefixFilter(Bytes.toBytes("weig"));
		list.addFilter(filter1);
		list.addFilter(filter2);
		
		scan.setFilter(list);
		ResultScanner scanner  = table.getScanner(scan);
		for(Result rs:scanner) {
			String rowkey = Bytes.toString(rs.getRow());
			System.out.println("row key :"+rowkey);
			Cell[] cells  = rs.rawCells();
			for(Cell cell : cells) {
				System.out.println(Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength())+"::"+
			                    Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
			}
			System.out.println("-----------------------------------------");
		}
		
	}
	@After
	public void closeConn() throws IOException {
		conn.close();
	}
}
	
	


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31506529/viewspace-2214159/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31506529/viewspace-2214159/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: HBase-Java API 是一种用于管理 HBase 表的编程接口。使用 HBase-Java API,开发人员可以编写 Java 代码来创建、删除、修改和查询 HBase 表。HBase-Java API 提供了一系列类和方法,可以让开发人员轻松地与 HBase 表进行交互。例如,可以使用 HBaseAdmin 类来管理 HBase 表,使用 HTable 类来访问 HBase 表中的数据,使用 Put 类来插入数据,使用 Get 类来获取数据,等等。总之,HBase-Java APIHBase 的重要组成部分,它为开发人员提供了强大的工具来管理和操作 HBase 表。 ### 回答2: HBase是一个分布式的列式存储数据库,在很多大数据应用中得到广泛的使用。它采用Hadoop作为其底层基础框架,同时提供了Java API供开发人员使用HBaseJava API为开发人员提供了一个管理表的接口,使得开发人员可以对HBase数据库中的表进行创建、读取、修改和删除等基本操作。 首先,我们需要用Java API创建一个HBase数据库中的表。使用HBaseJava API创建表的流程如下: 1. 首先需要获取HBase Configuration对象,并设置HBase连接参数以连接HBase数据库。 2. 接下来,需要获取HBase Admin对象,以便在操作HBase数据库表之前检查表是否存在,如果表不存在,需要创建该表。 3. 通过HBaseJava API创建表时,需要指定表的表名、列族的名称以及版本数等属性。 4. 创建表时需要考虑表的region的分配问题,可以对表的region进行手动或自动分片,以此来控制HBase的负载均衡。 创建了HBase数据库中的表之后,我们可以使用Java API对表进行读写操作。在进行读写操作时,需要使用HBaseJava API提供的Get的方法来获取表中的数据、Scan方法来扫描整个表、以及Put方法来向表中插入数据。此外,在进行表操作时还需要设置一些常见的数据操作参数,例如版本数、时间戳等。 在使用HBaseJava API时,还需要注意一些性能优化的问题,例如何时启用缓存、何时触发分区策略以及如何优化HBase表的大小等。这些优化措施能够在HBase的性能以及数据读写时的延迟方面提供很好的支持和帮助。 总的来说,HBaseJava API提供的表管理接口为开发人员提供了非常方便和快捷的方式来操作HBase数据库中的表。通过使用这些API,开发人员可以创建、读取、修改和删除表的数据,并且能够充分应用HBase的分布式特性来优化数据管理和性能提升。 ### 回答3: HBase是一个开源、分布式、非关系型数据库,它可以存储和处理大规模结构化、半结构化和非结构化数据。HBase Java APIHBase的官方API,它提供了对HBase表的管理和操作功能,让开发人员可以通过Java代码轻松地连接到HBase数据库。 在使用HBase Java API管理表时,首先需要创建一个HBaseConfiguration对象,它包含了连接HBase数据库所需的配置信息,如Zookeeper地址、HBase根目录等。然后,可以使用HBaseAdmin类创建、删除、修改表,以及列族等操作。例如,创建一个表可以通过以下代码实现: ``` HBaseAdmin admin = new HBaseAdmin(HBaseConfiguration.create()); HTableDescriptor tableDescriptor = new HTableDescriptor("table_name"); HColumnDescriptor columnDescriptor = new HColumnDescriptor("column_family"); tableDescriptor.addFamily(columnDescriptor); admin.createTable(tableDescriptor); ``` 创建表时,需要先通过HTableDescriptor类定义表名称,然后通过HColumnDescriptor类定义列族名称。可以通过addFamily()方法将列族添加到表描述中,最后通过HBaseAdmin的createTable()方法创建表。 除了创建表之外,HBase Java API还提供了许多其他的操作,如获取表信息、获取所有表的列表、删除表等。同时,HBase Java API还提供了对数据的CRUD操作,包括put、get、scan、delete等方法,让开发人员可以方便地进行数据操作。 总之,HBase Java API是一个非常强大的工具,它可以使开发人员轻松地管理HBase数据库,并实现数据的高效存储和处理。但是,在使用HBase Java API时,需要了解HBase的基本知识和API的用法,才能更好地发挥其功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值