API简介
几个主要 Hbase API 类(以HBase-1.2.5版本为例)和数据模型之间的对应关系:
| Java类 | HBase数据模型 |
|---|---|
| HBaseAdmin | 数据库(DataBase) |
| HBaseConfiguration | |
| HTable | 表(Table) |
| HTableDescriptor | 列簇(Column Family) |
| HColumnDescriptor | 列(Column) |
| Put | 列修饰符(Column Qualifier) |
| Get | |
| Scanner |
01. HBaseAdmin
- 类名:org.apache.hadoop.hbase.client.HBaseAdmin
作用:提供了一个接口来管理 HBase 数据库的表信息。它提供的方法包括:创建表,删 除表,列出表项,使表有效或无效,以及添加或删除表列族成员等。 | 返回值 | 函数 | 描述 | | —- | —- | —- | | void | addColumn(String tableName,HColumnDescriptor column) | 向一个已经存在的表添加列 | | void | checkHBaseAvailable(HBaseConfiguration conf) | 静态函数,查看HBase是否处于运行状态 | | void | createTable(HTableDescriptor desc) | 创建一个表(同步操作) | | void | deleteTable(byte[] tableName) | 删除一个已经存在的表 | | void | enableTable(byte[] tableName) | 使表处于有效状态 | | void | disableTable(byte[] tableName) | 使表处于无效状态 | | HTableDescriptor[] | listTables() | 列出所有列表项 | | void | modifyTable(byte[] tableName,HTableDescriptor htd) | 修改表的模式(异步操作) | | boolean | tableExists(String tableName) | 检查表是否存在 |
用法示例
HBaseAdmin admin = new HBaseAdmin(config);admin.disableTable("tableName");
02. HBaseConfiguration
类名:org.apache.hadoop.hbase.HBaseConfiguration
作用:对 HBase 进行配置 | 返回值 | 函数 | 描述 | | —- | —- | —- | | void | addResource(Path file) | 通过给定的路径所指的文件来添加资源 | | void | clear() | 清空所有已设置的属性 | | String | get(String name) | 获取属性名对应的值 | | String | getBoolean(String name,boolean defaultValue) | 获取为boolean类型的属性值,如果其属性值类型不为boolean,则返回默认属性值 | | void | set(String name,String value) | 通过属性名来设置值 | | void | setBoolean(String name,boolean value) | 设置boolean类型的属性值 |
用法示例
HBaseConfiguration hconfig = new HBaseConfiguration();# 设置zk端口为2181。一般情况下,HBaseCOnfiguration会使用构造函数进行初始化,然后再使用其他方法。hconfig.set("hbase.zookeeper.property.clientPort","2181");
03. HTableDescriptor
类名:org.apache.hadoop.hbase.HTableDescriptor
作用:包含了表的名字极其对应表的列族 | 返回值 | 函数 | 描述 | | —- | —- | —- | | void | addFamily(HColumnDescriptor column) | 添加一个列簇 | | HColumnDescriptor | removeFamily(byte[] column) | 移除一个列簇 | | byte[] | getName() | 获取表的名称 | | byte[] | getValue(byte[] key) | 获取属性的值 | | void | setValue(String key,String value) | 设置属性的值 |
用法示例
HTableDescriptor htd = new HTableDescriptor(tableName);# 添加列簇htd.addFamily(new HColumnDescriptor("family"));
04. HColumnDescriptor
类名:org.apache.hadoop.hbase.HColumnDescriptor
作用:维护着关于列族的信息,例如版本号,压缩设置等。它通常在创建表或者为表添 加列族的时候使用。列族被创建后不能直接修改,只能通过删除然后重新创建的方式。列族被删除的时候,列族里面的数据也会同时被删除。 | 返回值 | 函数 | 描述 | | —- | —- | —- | | byte[] | getName() | 获取列簇的名称 | | byte[] | getValue(byte[] key) | 获取属性的值 | | void | setValue(String key,String value) | 设置属性的值 |
用法示例
HTableDescriptor htd = new HTableDescriptor(tableName);HColumnDescriptor col = new HColumnDescriptor("family2");# 添加列簇family2htd.addFamily(col);
05. HTable
类名:org.apache.hadoop.hbase.client.HTable
作用:可以用来和 HBase 表直接通信。此方法对于更新操作来说是非线程安全的。 | 返回值 | 函数 | 描述 | | —- | :—- | :—- | | void | checkAdnPut(byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put | 自动的检查row/family/qualifier是否与给定的值匹配 | | void | close() | 释放所有的资源或挂起内部缓冲区中的更新 | | Boolean | exists(Get get) | 检查Get实例所指定的值是否存在于HTable的列中 | | Result | get(Get get) | 获取指定行的某些单元格所对应的值 | | byte[][] | getEndKeys() | 获取当前一打开的表每个区域的结束键值 | | ResultScanner | getScanner(byte[] family) | 获取当前给定列族的scanner实例 | | HTableDescriptor | getTableDescriptor() | 获取当前表的HTableDescriptor实例 | | byte[] | getTableName() | 获取表名 | | static boolean | isTableEnabled(HBaseConfiguration conf, String tableName) | 检查表是否有效 | | void | put(Put put) | 向表中添加值 |
用法示例
HTable table = new HTable(conf, Bytes.toBytes(tableName));ResultScanner scanner = table.getScanner(family);
06. Put
类名:org.apache.hadoop.hbase.client.Put
作用:用来对单个行执行添加操作 | 返回值 | 函数 | 描述 | | —- | —- | —- | | Put | add(byte[] family, byte[] qualifier, byte[] value) | 将指定的列和对应的值添加到Put实例中 | | Put | add(byte[] family, byte[] qualifier, long ts, byte[] value) | 将指定的列和对应的值及时间戳添加到Put实例中 | | byte[] | getRow() | 获取Put实例的行 | | RowLock | getRowLock() | 获取Put实例的行锁 | | long | getTimeStamp() | 获取Put实例的时间戳 | | boolean | isEmpty() | 检查familyMap是否为空 | | Put | setTimeStamp(long timeStamp) | 设置Put实例的时间戳 |
用法示例
HTable table = new HTable(conf,Bytes.toBytes(tableName));Put p = new Put(brow); // 为指定行创建一个Put操作p.add(family,qualifier,value);table.put(p);
07. Get
类名:org.apache.hadoop.hbase.client.Get
作用:用来获取单个行的相关信息 | 返回值 | 函数 | 描述 | | —- | —- | —- | | Get | addColumn(byte[] family, byte[] qualifier) | 获取指定列族和列修饰符对应的列 | | Get | addFamily(byte[] family) | 通过指定的列族获取其对应列的所有列 | | Get | setTimeRange(long minStamp,long maxStamp) | 获取指定取件的列的版本号 | | Get | setFilter(Filter filter) | 当执行Get操作时设置服务器端的过滤器 |
用法示例
HTable table = new HTable(conf, Bytes.toBytes(tableName));Get g = new Get(Bytes.toBytes(row));
08. Result
类名:org.apache.hadoop.hbase.client.Result
- 作用:存储 Get 或者 Scan 操作后获取表的单行值。使用此类提供的方法可以直接获取值 或者各种 Map 结构( key-value 对)
| 返回值 | 函数 | 描述 |
| —- | —- | —- |
| boolean | containsColumn(byte[] family,byte[] qualifier) | 检查指定的列是否存在 |
| NavigableMap
| getFamilyMap(byte[] family) | 获取对应列簇所包含的修饰符与值的键值对 | | byte[] | getValue(byte[] family,byte[] qualifier) | 获取对应列的最新值 |
HBase示例
pom.xml依赖:
<dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.2.5</version></dependency><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-server</artifactId><version>1.2.5</version></dependency>
01. 获取连接对象
public static Configuration conf;static{// 使用HBaseConfiguration的单例方法实例化conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "192.168.0.101,192.168.0.102,192.168.0.103");conf.set("hbase.zookeeper.property.clientPort", "2181");}/*** 获得链接* @return*/public static synchronized Connection getConnection() {try {if (conn == null || conn.isClosed()) {conn = ConnectionFactory.createConnection(conf);}} catch (IOException e) {System.err.println("HBase 建立链接失败 "+ e.getMessage());}return conn;}
02. 判断表是否存在(DDL)
/*** 判断表是否存在(DDL)* create 't1', 'cf1', 'cf2'* @param tableName 表名* @return 是否创建成功* @throws IOException*/public static boolean isTableExist(String tableName) throws IOException {boolean result = false;// 在 HBase 中管理、访问表需要先创建 HBaseAdmin 对象Connection connection = ConnectionFactory.createConnection(conf);HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();try {result = admin.tableExists(tableName);} finally {admin.close();}return result;}
03. 创建表(DDL)
public static void createTable(String tableName, String... columnFamily) throws IOException {Connection connection = ConnectionFactory.createConnection(conf);HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();//判断表是否存在if (isTableExist(tableName)) {System.out.println("Table:" + tableName + " already exists!");} else {// 创建表属性对象,表名需要转字节HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));// 创建多个列族for (String cf : columnFamily) {descriptor.addFamily(new HColumnDescriptor(cf));}// 根据对表的配置,创建表admin.createTable(descriptor);System.out.println("Table:" + tableName + " create successfully!");}}
04. 新增记录(DML)
public static void addRowData(String tableName, String rowKey, String columnFamily, Stringcolumn, String value) throws IOException {// 创建HTable对象//HTable hTable = new HTable(conf, tableName);Connection connection = ConnectionFactory.createConnection(conf);HTable hTable = (HTable) connection.getTable(TableName.valueOf(tableName));// 向表中插入数据Put put = new Put(Bytes.toBytes(rowKey));// 向Put对象中组装数据put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));hTable.put(put);hTable.close();System.out.println("insert successfully!");}
05. 全表扫描(Scan)
public static void getAllRows(String tableName) throws IOException {Connection connection = ConnectionFactory.createConnection(conf);HTable hTable = (HTable) connection.getTable(TableName.valueOf(tableName));// 得到用于扫描region的对象Scan scan = new Scan();// 使用HTable得到resultcanner实现类的对象ResultScanner resultScanner = hTable.getScanner(scan);for(Result result : resultScanner){Cell[] cells = result.rawCells();for(Cell cell : cells){System.out.print("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell))+"\t");System.out.print("ColumnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell))+"\t");System.out.print("Qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell))+"\t");System.out.print("Value:" + Bytes.toString(CellUtil.cloneValue(cell)));System.out.println();}}}
06. 获取单行记录(get)
public static void getRow(String tableName, String rowKey) throws IOException {Connection connection = ConnectionFactory.createConnection(conf);HTable hTable = (HTable) connection.getTable(TableName.valueOf(tableName));Get get = new Get(Bytes.toBytes(rowKey));//get.setMaxVersions(); // 显示所有版本//get.setTimeStamp(); // 显示指定时间戳的版本Result result = hTable.get(get);for(Cell cell : result.rawCells()){System.out.print("RowKey:" + Bytes.toString(result.getRow())+"\t");System.out.print("ColumnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell))+"\t");System.out.print("Qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell))+"\t");System.out.print("Value:" + Bytes.toString(CellUtil.cloneValue(cell))+"\t");System.out.print("Timestamp:" + cell.getTimestamp());System.out.println();}}
07. 获取多行记录(get)
/*** 获取多行记录(get)* @param tableName 表名* @param rows 行键(可扩展参数)* @throws IOException*/public static void getRow(String tableName, String... rows) throws IOException {Connection connection = ConnectionFactory.createConnection(conf);HTable hTable = (HTable) connection.getTable(TableName.valueOf(tableName));List<Get> gets = null;Result[] results;try {if (hTable != null) {gets = new ArrayList<>();for (String row : rows) {if(row!=null){gets.add(new Get(Bytes.toBytes(String.valueOf(row))));}else{throw new RuntimeException("hbase have no data");}}}if (gets.size() > 0) {results = hTable.get(gets);if(results!=null && results.length>0){for (Result result : results) {for(Cell cell : result.rawCells()){System.out.print("RowKey:" + Bytes.toString(result.getRow())+"\t");System.out.print("ColumnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell))+"\t");System.out.print("Qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell))+"\t");System.out.print("Value:" + Bytes.toString(CellUtil.cloneValue(cell))+"\t");System.out.print("Timestamp:" + cell.getTimestamp());System.out.println();}}}}} catch (IOException e) {System.err.print(e);} finally {try {hTable.close();} catch (IOException e) {System.err.print(e);}}}
08. 根据限定符获取单行记录(get)
public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException {Connection connection = ConnectionFactory.createConnection(conf);HTable hTable = (HTable) connection.getTable(TableName.valueOf(tableName));Get get = new Get(Bytes.toBytes(rowKey));get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));Result result = hTable.get(get);for (Cell cell : result.rawCells()) {System.out.print("RowKey:" + Bytes.toString(result.getRow()) + "\t");System.out.print("ColumnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");System.out.print("Qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");System.out.print("Value:" + Bytes.toString(CellUtil.cloneValue(cell)) + "\t");System.out.print("Timestamp:" + cell.getTimestamp());System.out.println();}}
09. 删除多行数据(DML)
public static void deleteMultiRow(String tableName, String... rows) throws IOException {Connection connection = ConnectionFactory.createConnection(conf);HTable hTable = (HTable) connection.getTable(TableName.valueOf(tableName));List<Delete> deleteList = new ArrayList<>();for (String row : rows) {Delete delete = new Delete(Bytes.toBytes(row));deleteList.add(delete);}hTable.delete(deleteList);hTable.close();}
10. 删除表(DDL)
public static void dropTable(String tableName) throws IOException {Connection connection = ConnectionFactory.createConnection(conf);HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();if(isTableExist(tableName)){admin.disableTable(tableName);admin.deleteTable(tableName);System.out.println("Table:" + tableName + " delete successfully!");}else{System.out.println("Table:" + tableName + " not exist!");}}
完整示例
■ 常量类(Constant.java)
public class Constant {/** HBase配置 **/public static class HBaseConfig {public final static String ZK_HOST="192.168.0.101,192.168.0.102,192.168.0.103";public final static String ZK_PORT="2181";public final static String ZK_PATH="/hbase";}}
■ 工具类(HBaseBaseUtil.java)
import com.lonton.bigdata.constant.Constant;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.IOException;import java.util.ArrayList;import java.util.List;/*** Description HBase Base API* Version 1.0.0*/public class HBaseBaseUtil {private static final Logger log = LoggerFactory.getLogger(HBaseBaseUtil.class);public static Configuration conf;private static Connection conn;static {// 使用HBaseConfiguration的单例方法实例化conf = HBaseConfiguration.create();conf.set(HConstants.ZOOKEEPER_QUORUM, Constant.HBaseConfig.ZK_HOST);conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, Constant.HBaseConfig.ZK_PORT);conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, Constant.HBaseConfig.ZK_PATH);}/*** 获得链接* @return*/public static synchronized Connection getConnection() {try {if (conn == null || conn.isClosed()) {conn = ConnectionFactory.createConnection(conf);}} catch (IOException e) {log.error("HBase 建立链接失败 ", e);}return conn;}/*** 关闭连接* @throws IOException*/public static void closeConnect(Connection conn) {if (null != conn) {try {conn.close();} catch (Exception e) {log.error("closeConnect failure !", e);}}}/*** 判断表是否存在(DDL)* create 't_user', {NAME => 'info', VERSIONS => '3'},{NAME => 'data'}* @param tableName 表名* @return 是否创建成功* @throws IOException*/public static boolean isTableExist(String tableName) throws IOException {boolean result = false;Connection conn = getConnection();HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();try {result = admin.tableExists(tableName);} catch (Exception e) {log.error(e.getMessage());} finally {admin.close();}return result;}/*** 创建表(DDL)* create 't1', 'cf1', 'cf2'* @param tableName 表名* @param columnFamily 列簇* @throws IOException*/public static void createTable(String tableName, String... columnFamily) throws IOException {// 判断表是否存在if (isTableExist(tableName)) {log.error("Table:" + tableName + " already exists!");return;}Connection conn = getConnection();HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();try {// 创建表属性对象,表名需要转字节HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));// 创建多个列族for (String cf : columnFamily) {descriptor.addFamily(new HColumnDescriptor(cf));}// 根据对表的配置,创建表admin.createTable(descriptor);log.info("Table:" + tableName + " create successfully!");} catch (Exception e) {log.error(e.getMessage());} finally {admin.close();}}/*** 新增记录(DML)* put 't1', '1001', 'cf1:name', 'zhangsan'* put 't1', '1001', 'cf1:age', '23'* @param tableName 表名* @param rowKey 行键* @param columnFamily 列簇* @param column 列* @param value 值* @throws IOException*/public static void addRowData(String tableName, String rowKey, String columnFamily, Stringcolumn, String value) throws IOException {Connection conn = getConnection();HTable hTable = (HTable) conn.getTable(TableName.valueOf(tableName));try {// 向表中插入数据Put put = new Put(Bytes.toBytes(rowKey));// 向Put对象中组装数据put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));hTable.put(put);log.info("insert successfully!");} catch (Exception e) {log.error(e.getMessage());} finally {hTable.close();}}/*** 全表扫描(Scan)* scan "t1"* @param tableName 表名* @return* @throws IOException*/public static ResultScanner getAllRows(String tableName) throws IOException {ResultScanner results = null;Connection conn = getConnection();HTable hTable = (HTable) conn.getTable(TableName.valueOf(tableName));try {// 得到用于扫描region的对象Scan scan = new Scan();// setCaching设置的值为每次rpc的请求记录数,默认是1;cache大可以优化性能,但是太大了会花费很长的时间进行一次传输。scan.setCaching(1000);// 使用HTable得到resultcanner实现类的对象results = hTable.getScanner(scan);/*for (Result result : results) {Cell[] cells = result.rawCells();for (Cell cell : cells) {System.out.print("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)) + "\t");System.out.print("ColumnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");System.out.print("Qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");System.out.print("Value:" + Bytes.toString(CellUtil.cloneValue(cell)));System.out.println();}}*/} catch (Exception e) {log.error(e.getMessage());} finally {hTable.close();}return results;}/*** 获取单行记录(get)* get "t1","1001"* @param tableName 表名* @param rowKey 行键* @return* @throws IOException*/public static Result getRow(String tableName, String rowKey) throws IOException {Result result = null;Connection conn = getConnection();HTable hTable = (HTable) conn.getTable(TableName.valueOf(tableName));try {Get get = new Get(Bytes.toBytes(rowKey));//get.setMaxVersions(); // 显示所有版本//get.setTimeStamp(); // 显示指定时间戳的版本result = hTable.get(get);/*for (Cell cell : result.rawCells()) {System.out.print("RowKey:" + Bytes.toString(result.getRow()) + "\t");System.out.print("ColumnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");System.out.print("Qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");System.out.print("Value:" + Bytes.toString(CellUtil.cloneValue(cell)) + "\t");System.out.print("Timestamp:" + cell.getTimestamp());System.out.println();}*/} catch (Exception e) {log.error(e.getMessage());} finally {hTable.close();}return result;}/*** 获取多行记录(get)* @param tableName 表名* @param rows 行键(可扩展参数)* @param <T>* @return* @throws IOException*/public static <T> Result[] getRows(String tableName, List<T> rows) throws IOException {Connection conn = getConnection();HTable hTable = (HTable) conn.getTable(TableName.valueOf(tableName));List<Get> gets = null;Result[] results = null;try {if (hTable != null) {gets = new ArrayList<Get>();for (T row : rows) {if (row != null) {gets.add(new Get(Bytes.toBytes(String.valueOf(row))));} else {throw new RuntimeException("hbase have no data");}}}if (gets.size() > 0) {results = hTable.get(gets);}} catch (IOException e) {log.error("getRows failure !", e);} finally {try {hTable.close();} catch (IOException e) {log.error("table.close() failure !", e);}}return results;}/*** 根据限定符获取单行记录(get)* get "t1","1001","cf1:name"* @param tableName 表名* @param rowKey 行键* @param family 列簇* @param qualifier 限定符* @throws IOException*/public static Result getRowQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException {Result result = null;Connection conn = getConnection();HTable hTable = (HTable) conn.getTable(TableName.valueOf(tableName));try {Get get = new Get(Bytes.toBytes(rowKey));get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));result = hTable.get(get);/*for (Cell cell : result.rawCells()) {System.out.print("RowKey:" + Bytes.toString(result.getRow()) + "\t");System.out.print("ColumnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");System.out.print("Qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");System.out.print("Value:" + Bytes.toString(CellUtil.cloneValue(cell)) + "\t");System.out.print("Timestamp:" + cell.getTimestamp());System.out.println();}*/} catch (Exception e) {log.error(e.getMessage());} finally {hTable.close();}return result;}/*** 删除多行数据(DML)* @param tableName 表名* @param rows 行键(可扩展参数)* @throws IOException*/public static void deleteMultiRow(String tableName, String... rows) throws IOException {Connection conn = getConnection();HTable hTable = (HTable) conn.getTable(TableName.valueOf(tableName));try {List<Delete> deleteList = new ArrayList<>();for (String row : rows) {Delete delete = new Delete(Bytes.toBytes(row));deleteList.add(delete);}hTable.delete(deleteList);} catch (Exception e) {log.error(e.getMessage());} finally {hTable.close();}}/*** 删除表(DDL)* @param tableName 表名* @throws IOException*/public static void dropTable(String tableName) throws IOException {if (!isTableExist(tableName)) {log.error("Table:" + tableName + " not exist!");return;}Connection conn = getConnection();HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();try {if (isTableExist(tableName)) {admin.disableTable(tableName);admin.deleteTable(tableName);log.info("Table:" + tableName + " delete successfully!");}} catch (Exception e) {log.error(e.getMessage());} finally {admin.close();}}}
■ 测试类(HBaseBaseUtilTest.java)
import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.util.Bytes;import org.junit.*;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class HBaseBaseUtilTest {private String tableName="t1";private String rowKey="1001";/** BeforeClass:会在所有方法被调用前被执行,* 而且该方法是静态的,所有当测试类被加载后接着就会运行它,* 而且在内存中它只会存在一份实例,它比较适合加载配置文件**/@BeforeClasspublic static void setUpBeforeClass() {System.out.println("this is @BeforeClass ...");HBaseBaseUtil.getConnection();}/** AfterClass:通常用来对资源的清理,如关闭数据库的连接 **/@AfterClasspublic static void tearDownAfterClass() {System.out.println("this is @AfterClass ...");HBaseBaseUtil.closeConnect(HBaseBaseUtil.getConnection());}/** Before:每个测试方法调用前执行一次 **/@Beforepublic void setUp() {System.out.println("this is @Before ...");}/** Before:每个测试方法调用后执行一次 **/@Afterpublic void tearDown() {System.out.println("this is @After ...");}@Testpublic void createTable() throws IOException {HBaseBaseUtil.createTable(tableName, "cf1", "cf2");assert HBaseBaseUtil.isTableExist(tableName);}@Testpublic void isTableExist() throws IOException {assert HBaseBaseUtil.isTableExist(tableName);}@Testpublic void addRowData() throws IOException {String value="tom";HBaseBaseUtil.addRowData(tableName, rowKey, "cf1", "name", value);Result result=HBaseBaseUtil.getRowQualifier(tableName, rowKey, "cf1", "name");Cell[] cells=result.rawCells();Assert.assertNotNull(cells);Assert.assertEquals(value,Bytes.toString(CellUtil.cloneValue(cells[0])));}@Testpublic void getAllRows() throws IOException {ResultScanner results =HBaseBaseUtil.getAllRows(tableName);Assert.assertNotNull(results);for (Result result : results) {Assert.assertNotNull(result);}}@Testpublic void getRow() throws IOException {String value="tom";HBaseBaseUtil.addRowData(tableName, rowKey, "cf1", "name", value);Result result=HBaseBaseUtil.getRow(tableName,rowKey);Cell[] cells=result.rawCells();Assert.assertNotNull(cells);Assert.assertEquals(value,Bytes.toString(CellUtil.cloneValue(cells[0])));}@Testpublic void getRows() throws IOException {String rowKey1="1003";String rowKey2="1004";String value1="tom1";String value2="tom2";HBaseBaseUtil.addRowData(tableName, rowKey1, "cf1", "name", value1);HBaseBaseUtil.addRowData(tableName, rowKey2, "cf1", "name", value2);List<String> rows=new ArrayList<>();rows.add(rowKey1);rows.add(rowKey2);Result[] results=HBaseBaseUtil.getRows(tableName,rows);Assert.assertNotNull(results);Assert.assertTrue(results.length==2);}@Testpublic void getRowQualifier() throws IOException {String value="tom";HBaseBaseUtil.addRowData(tableName, rowKey, "cf1", "name", value);Result result=HBaseBaseUtil.getRowQualifier(tableName, rowKey, "cf1", "name");Cell[] cells=result.rawCells();Assert.assertNotNull(cells);Assert.assertEquals(value,Bytes.toString(CellUtil.cloneValue(cells[0])));}@Testpublic void deleteMultiRow() throws IOException {String rowKey1="1003";String rowKey2="1004";String value1="tom1";String value2="tom2";HBaseBaseUtil.addRowData(tableName, rowKey1, "cf1", "name", value1);HBaseBaseUtil.addRowData(tableName, rowKey2, "cf1", "name", value2);HBaseBaseUtil.deleteMultiRow(tableName, rowKey1, rowKey2);List<String> rows=new ArrayList<>();rows.add(rowKey1);rows.add(rowKey2);Result[] results=HBaseBaseUtil.getRows(tableName,rows);Assert.assertNotNull(results);for (Result result : results) {Cell[] cells=result.rawCells();Assert.assertTrue(cells.length==0);}}@Testpublic void dropTable() throws IOException {assert HBaseBaseUtil.isTableExist(tableName);HBaseBaseUtil.dropTable(tableName);assert !HBaseBaseUtil.isTableExist(tableName);}}
附件
完整案例代码:hbase-example-src.zip
