HBase基本API操作之CRUD的示例分析
发表于:2024-10-19 作者:千家信息网编辑
千家信息网最后更新 2024年10月19日,这篇文章将为大家详细讲解有关HBase基本API操作之CRUD的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。import java.io.IOExcept
千家信息网最后更新 2024年10月19日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")); Listputs = 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安全错误
数据库的锁怎样保障安全
惠州智能软件开发常见问题
已有数据库如何利用dw连接
数据库表的默认值不能为空
英雄联盟手游国服分几个服务器
赌博马甲软件开发
山西正规软件开发过程检测中心
信息系统软件开发
长宁教育网络安全采购
桓台企业软件开发服务
谢霆 万方数据库
九江职业大学软件开发
管理多个公司dhcp服务器
网络安全应急人员证书
厦门联想服务器维修站
河北区数据网络技术质量保证
洛阳炫优互联网科技
date怎么获取数据库月份
飞盘视频软件开发
无线路由器怎么设置网络安全
无法连接管理服务器
学软件开发都用什么apo
2003服务器版
csgo 服务器如何飞行
数据库信息拼音
js数组隐藏某个数据库
龙奔服务器
海南质量软件开发价格表格
融媒体中心编辑及网络技术
软件开发最新排名
广州冶金计算机软件开发专业