HBase:
- 数据库:是一种面向列族存储的非关系型数据库
- 用于存储结构化和非结构化数据:适用于单表非关系型数据的存储,不适合做关联查询,类似于JOIN等操作
- 基于HDFS:数据持久化存储的体现形式是HFile,存放于DataNode中,被ResionServer以Region的形式进行管理
- 延迟较低,接入在线业务使用:面对大量的企业数据,HBase可以直线单表大量数据的存储,同时提供了高效的数据访问速度。
package csdn.dreamzuora;import com.sun.istack.internal.logging.Logger;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;/*** Title:* Description:** @version 1.0* @author: weijie* @date: 2020/11/10 18:02*/
public class HbaseUtils {public static Configuration conf;private Logger logger = Logger.getLogger(HbaseUtils.class);static{//使用 HBaseConfiguration 的单例方法实例化conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "39.96.204.209");conf.set("hbase.zookeeper.property.clientPort", "2181");}public Connection getConnection() throws IOException {return ConnectionFactory.createConnection(conf);}public Admin getAdmin() throws IOException {Connection connection = getConnection();return connection.getAdmin();}public Table getTable(TableName tableName) throws IOException {Connection connection = getConnection();return connection.getTable(tableName);}/*** 判断表是否存在* @param tableName* @return* @throws IOException*/public boolean isTableExist(String tableName) throws IOException {Admin admin = getAdmin();return admin.tableExists(TableName.valueOf(tableName));}/*** 创建表* @param tableName* @param columnFamily* @throws IOException*/public void createTable(String tableName, String... columnFamily) throws IOException {Connection connection = ConnectionFactory.createConnection(conf);Admin admin = connection.getAdmin();//判断表是否存在if (isTableExist(tableName)){logger.info("表" + tableName + "已存在");}else {//创建表属性对象,表名需要转字节HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));//创建多个列族for (String cf : columnFamily){hTableDescriptor.addFamily(new HColumnDescriptor(cf));}//根据对表的配置,创建表admin.createTable(hTableDescriptor);logger.info("表" + tableName + "创建成功!");}}/*** 删除表*/public void dropTable(String tableName) throws IOException {Admin admin = getAdmin();if (isTableExist(tableName)){admin.disableTable(TableName.valueOf(tableName));admin.deleteTable(TableName.valueOf(tableName));logger.info("表" + tableName + "删除成功!");}else {logger.info(tableName + "表不存在");}}/*** 向表中插入数据*/public void addRowData(String tableName, String rowkey, String columnFamily, String column, String value) throws IOException {//创建表对象Table table = getTable(TableName.valueOf(tableName));//向表中插入数据Put put = new Put(Bytes.toBytes(rowkey));//向Put对象中组装数据put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));table.put(put);table.close();logger.info("数据插入成功");}/*** 删除多行数据*/public void deleteMultiRow(String tableName, String... rows) throws IOException {Table table = getTable(TableName.valueOf(tableName));List<Delete> deleteList = new ArrayList<Delete>();for (String row : rows){Delete delete = new Delete(Bytes.toBytes(row));deleteList.add(delete);}table.delete(deleteList);table.close();}/*** 获取所有数据*/public void getAllRows(String tableName) throws IOException {Table table = getTable(TableName.valueOf(tableName));//得到用于扫描region的对象Scan scan = new Scan();//使用Htable得到resultcanner实现类的对象ResultScanner resultScanner = table.getScanner(scan);for (Result result : resultScanner){Cell[] cells = result.rawCells();for (Cell cell : cells){//得到rowkeylogger.info("行健: " + Bytes.toString(CellUtil.cloneRow(cell)));//得到列族logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));//列名logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));//列值logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));}}}/*** 获取某一行数据*/public void getRow(String tableName, String rowkey) throws IOException {Table table = getTable(TableName.valueOf(tableName));Get get = new Get(Bytes.toBytes(rowkey));/*** get.setMaxVersions() 显示所有版本* get.setTimeStamp(timeStamp) 显示指定时间戳版本*/Result result = table.get(get);for (Cell cell : result.rawCells()){logger.info("行键: " + Bytes.toString(result.getRow()));//得到列族logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));//列名logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));//列值logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));}}/*** 获取某一行指定"列族:列"的数据*/public void getRowQualifier(String tableName, String rowkey, String family, String qualifier) throws IOException {Table table = getTable(TableName.valueOf(tableName));Get get = new Get(Bytes.toBytes(rowkey));get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));Result result = table.get(get);for (Cell cell : result.rawCells()){logger.info("行键: " + Bytes.toString(result.getRow()));//得到列族logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));//列名logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));//列值logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));}}}
视频学习地址:https://www.bilibili.com/video/BV1Y4411B7jy?p=7