博客
关于我
封装一个简单的HBaseUtil
阅读量:781 次
发布时间:2019-03-23

本文共 2704 字,大约阅读时间需要 9 分钟。

HBase 作为一种面向列族的数据库,与普通数据库存在显著差异,无法直接使用JDBC封装的工具,因此该项目重新封装了专门的 HBase 工具进行使用。本文将详细介绍 HBase 的使用方法。

Maven 依赖

在项目中使用 HBase 时,需要通过 Maven 加入相关依赖。记得根据项目需求更换版本号,可以从以下地址获取:

示例依赖项:

org.apache.hbase
hbase-server
1.4.5
org.apache.hbase
hbase-client
1.4.5

私有属性

在使用 HBase 时,需要声明一些私有属性用于配置和连接管理:

private Configuration hbaseConf;private Connection hbaseConn;private HBaseAdmin admin;

构造方法

构造方法用于初始化连接和管理相关配置。具体实现如下:

public HBaseUtil() {    hbaseConf = HBaseConfiguration.create();    try {        hbaseConn = ConnectionFactory.createConnection();        admin = (HBaseAdmin) hbaseConn.getAdmin();        System.out.println("连接是否已建立:" + !hbaseConn.isClosed());    } catch (IOException e) {        e.printStackTrace();    }}

表格操作

判断是否存在表格

可以通过以下方法检查表格是否存在:

public boolean isExist(String tableNameStr) {    TableName tableName = TableName.valueOf(tableNameStr);    boolean flag = false;    try {        flag = admin.tableExists(tableName);    } catch (IOException e) {        e.printStackTrace();    }    return flag;}

创建表格

创建表格时需要先检查其是否存在,如存在则跳过,否则继续操作:

public void createTable(String tableNameStr, String[] familys) {    if (this.isExist(tableNameStr)) {        System.out.println("表格已存在");        return;    }    TableName tableName = TableName.valueOf(tableNameStr);    HTableDescriptor table = new HTableDescriptor(tableName);    for (String familyStr : familys) {        HColumnDescriptor family = new HColumnDescriptor(familyStr);        table.addFamily(family);    }    try {        admin.createTable(table);    } catch (IOException e) {        e.printStackTrace();    }}

删除表格

删除表格需要先禁用,再删除:

public void dropTable(String tableName) {    try {        admin.disableTable(tableName);        admin.deleteTable(tableName);    } catch (MasterNotRunningException e) {        e.printStackTrace();    } catch (ZooKeeperConnectionException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }}

数据操作

一条数据的写入

实现单条数据的写入功能:

public void putData(String tableNameStr, Put put) {    TableName tableName = TableName.valueOf(tableNameStr);    Table table;    try {        table = hbaseConn.getTable(tableName);        table.put(put);    } catch (IOException e) {        e.printStackTrace();    }}

多条数据的批量写入

支持批量写入操作:

public void putData(String tableNameStr, List
putList) { TableName tableName = TableName.valueOf(tableNameStr); Table table; try { table = hbaseConn.getTable(tableName); table.put(putList); } catch (IOException e) { e.printStackTrace(); }}

通过以上方法,可以对 HBase 表格进行基本的管理和数据操作。这些方法适合在实际项目中进行定制化开发。

转载地址:http://rejzk.baihongyu.com/

你可能感兴趣的文章
PowerDesigner生成数据模型并导出报告
查看>>
QGIS中导入dwg文件并使用GetWKT插件获取绘制元素WKT字符串以及QuickWKT插件实现WKT显示在图层
查看>>
PowerDesigner逆向工程从SqlServer数据库生成PDM(图文教程)
查看>>
PowerEdge T630服务器安装机器学习环境(Ubuntu18.04、Nvidia 1080Ti驱动、CUDA及CUDNN安装)
查看>>
PowerPC-object与elf中的符号引用
查看>>
QFileSystemModel
查看>>
Powershell DSC 5.0 - 参数,证书加密账号,以及安装顺序
查看>>
PowerShell 批量签入SharePoint Document Library中的文件
查看>>
Powershell 自定义对象小技巧
查看>>
pytorch从预训练权重加载完全相同的层
查看>>
PowerShell~发布你的mvc网站
查看>>
PowerShell使用详解
查看>>
Powershell制作Windows安装U盘
查看>>
powershell命令
查看>>
Powershell如何查看本地公网IP
查看>>
pytorch从csv加载自定义数据模板
查看>>
powershell对txt文件的服务器进行ping操作
查看>>
powershell常用
查看>>
PowerShell操作XML遇到的问题
查看>>
PowerShell攻击工具Empire实战
查看>>