HBase基本API操作之CRUD的示例分析
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这篇文章将为大家详细讲解有关HBase基本API操作之CRUD的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。import java.io.IOExcept
千家信息网最后更新 2025年12月02日HBase基本API操作之CRUD的示例分析
这篇文章将为大家详细讲解有关HBase基本API操作之CRUD的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
import java.io.IOException;import java.util.Arrays;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.CellUtil;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.Admin;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.util.Bytes;
创建一张userinfo表,对其表进行相关操作。
public class TestHBase { public static void main(String[] args) throws IOException { //2.获得会话 Admin admin = null; Connection con = null; try { //操作hbase数据库 //1.建立连接 Configuration conf = HBaseConfiguration.create(); //获得配制文件对象 conf.set("hbase.zookeeper.quorum", "192.168.226.129"); con = ConnectionFactory.createConnection(conf); //获得连接对象 admin = con.getAdmin(); //3.操作 //建立数据库 //a.判断数据库是否存在 TableName tn = TableName.valueOf("userinfo"); //创建表名对象 if ( admin.tableExists(tn) ) { System.out.println("----> 表存在, 删除表....."); admin.disableTable(tn); //使用表失效 admin.deleteTable(tn); //删除表 System.out.println("----> 删除表成功...."); }else{ System.out.println("----> 表不存在, 创建表....."); } //创建表结构对象:用于描述表名和相关的列族 HTableDescriptor htd = new HTableDescriptor(tn); //创建族列结构对象 HColumnDescriptor hcd1 = new HColumnDescriptor("familycolumn1"); HColumnDescriptor hcd2 = new HColumnDescriptor("familycolumn2"); //描述相关的列族 htd.addFamily(hcd1); htd.addFamily(hcd2); //创建表 admin.createTable(htd); System.out.println("创建表成功...."); //在表中插入数据 //a.单个插入 //参数是行键 "row01".getBytes() Put put = new Put(Bytes.toBytes("row1")); //定位行: put.addColumn(family, qualifier, value) put.addColumn(Bytes.toBytes("familycolumn1"), Bytes.toBytes("name"), Bytes.toBytes("Berg")); //获得表对象 Table table = con.getTable(tn); table.put(put); //添加数据 //b.批量插入 Put put01 = new Put(Bytes.toBytes("row2")); put01.addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("sex"), Bytes.toBytes("male")). addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("age"), Bytes.toBytes("22")); Put put02 = new Put(Bytes.toBytes("row3")); //参数是行键 "row01".getBytes() put02.addColumn(Bytes.toBytes("familycolumn1"), Bytes.toBytes("sex"), Bytes.toBytes("female")); List puts = Arrays.asList(put01, put02); Table table02 = con.getTable(tn); //获得表对象 table02.put(puts); //********************************* //读取操作 //实例化scan对象。 Scan scan = new Scan(); Table table03 = con.getTable(tn); //获得表对象 ResultScanner rs = table03.getScanner(scan); for (Result result : rs) { List cs = result.listCells(); for (Cell cell : cs) { String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); //取行键 long timestamp = cell.getTimestamp(); //取到时间戳 String family = Bytes.toString(CellUtil.cloneFamily(cell)); //取到族列 String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); //取到修饰名 String value = Bytes.toString(CellUtil.cloneValue(cell)); //取到值 System.out.println("rowKey : " + rowKey + ", timestamp : " + timestamp + ", family : " + family + ", qualifier : " + qualifier + ", value : " + value); } } System.out.println(" \n\n*****************get取数据*****************:"); //get Get get = new Get(Bytes.toBytes("row2")); get.addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("sex")); Table t04 = con.getTable(tn); Result r = t04.get(get); List| cs = r.listCells(); for (Cell cell : cs) { String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); //取行键 long timestamp = cell.getTimestamp(); //取到时间戳 String family = Bytes.toString(CellUtil.cloneFamily(cell)); //取到族列 String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); //取到修饰名 String value = Bytes.toString(CellUtil.cloneValue(cell)); //取到值 System.out.println("rowKey : " + rowKey + ", timestamp : " + timestamp + ", family : " + family + ", qualifier : " + qualifier + ", value : " + value); } //删除数据 System.out.println(" \n\n*****************delete删除数据*****************:"); Delete delete = new Delete(Bytes.toBytes("row2")); delete.addColumn(Bytes.toBytes("familycolumn2"), Bytes.toBytes("age")); Table t05 = con.getTable(tn); t05.delete(delete); System.out.println(" \n\n*****************delete删除数据后*****************:"); //scan scan = new Scan(); table03 = con.getTable(tn); //获得表对象 rs = table03.getScanner(scan); for (Result result : rs) { cs = result.listCells(); for (Cell cell : cs) { String rowKey = Bytes.toString(CellUtil.cloneRow(cell)); //取行键 long timestamp = cell.getTimestamp(); //取到时间戳 String family = Bytes.toString(CellUtil.cloneFamily(cell)); //取到族列 String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell)); //取到修饰名 String value = Bytes.toString(CellUtil.cloneValue(cell)); //取到值 System.out.println("rowKey : " + rowKey + ", timestamp : " + timestamp + ", family : " + family + ", qualifier : " + qualifier + ", value : " + value); } } } catch (IOException e) { e.printStackTrace(); } //4.关闭 try { if (admin != null){ admin.close(); } if(con != null){ con.close(); } } catch (IOException e) { e.printStackTrace(); } }} | | //运行结果:
----> 表存在, 删除表.....----> 删除表成功....创建表成功....rowKey : row1, timestamp : 1463486961279, family : familycolumn1, qualifier : name, value : BergrowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : age, value : 22rowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : sex, value : malerowKey : row3, timestamp : 1463486961289, family : familycolumn1, qualifier : sex, value : female *****************get取数据*****************:rowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : sex, value : male *****************delete删除数据*****************: *****************delete删除数据后*****************:rowKey : row1, timestamp : 1463486961279, family : familycolumn1, qualifier : name, value : BergrowKey : row2, timestamp : 1463486961289, family : familycolumn2, qualifier : sex, value : malerowKey : row3, timestamp : 1463486961289, family : familycolumn1, qualifier : sex, value : female
关于"HBase基本API操作之CRUD的示例分析"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
数据
对象
成功
到时
数据库
篇文章
示例
分析
参数
更多
结构
不错
实用
内容
单个
实例
文件
文章
知识
结果
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
ssas源视图加数据库
网络安全情况通报的范文
论文中的数据库表怎么表示
剑灵正在连接服务器
华显软件开发服务
网络安全保卫总队付茂琼
817数据库下载
服务器ie一直提示加入安全
mongodb数据库备份的步骤
网络安全风险是啥意思
DNF黑白网络安全科技馆
web站点的服务器软件
金蝶kis对应数据库
软件开发师工作自我描述
服务器建设整合迁移业务
梦幻转服可以不能转回原服务器
无线怎么计算机网络安全
psv 代理服务器
摩尔庄园忘记服务器
数据库司机表如何建立
网络安全竞争压力大
web调用数据库时间失败
服务器6
服务器主板驱动影响安装系统吗
广州响当当网络技术公司
word重复数据库
北仑ios软件开发企业
摩尔庄园忘记服务器
饥荒本地模组在服务器模组内
数据库司机表如何建立