当前位置: 首页 > news >正文

java实现Hbase 增删改查

目录

一、新建一个maven工程

 二、代码实现

2.1、配置hbase信息,连接hbase数据库

2.2、创建命名空间

2.3、创建表

2.4、删除表,删除之前要设置为禁用状态

2.5、添加数据

2.6、获取命令表空间 / tables列表

2.7、get方法查看表的内容

2.8、scan方法查看表的内容


一、新建一个maven工程

 添加 pom依赖

  <dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>2.3.5</version></dependency><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-server</artifactId><version>2.3.5</version></dependency>

 二、代码实现

2.1、配置hbase信息,连接hbase数据库

1.、测试类添加@Before 和@After  便于初始化和关闭资源

2、将对象提到上面,变为全局对象

    static Configuration config = null;private Connection conn = null;private Admin admin = null;@Beforepublic void init() throws IOException {System.out.println("执行init()方法");config = HBaseConfiguration.create();config.set(HConstants.HBASE_DIR, "hdfs://192.168.91.180:9000/hbase");config.set(HConstants.ZOOKEEPER_QUORUM, "192.168.91.180");config.set(HConstants.CLIENT_PORT_STR, "2181");conn = ConnectionFactory.createConnection(config);admin = conn.getAdmin();}@Afterpublic void close() throws IOException {System.out.println("执行close()方法");if (admin != null)admin.close();if (conn != null)conn.close();}

2.2、创建命名空间

    @Testpublic void createNameSpace() throws IOException {NamespaceDescriptor kb21 = NamespaceDescriptor.create("kb21").build();try {admin.createNamespace(kb21);} catch (IOException e) {e.printStackTrace();}}

2.3、创建表

    @Testpublic void createTable() throws IOException {//创建表的描述类TableName tableName = TableName.valueOf("kb21:student");HTableDescriptor desc = new HTableDescriptor(tableName);//创建列族的描述类HColumnDescriptor family1 = new HColumnDescriptor("info1");HColumnDescriptor family2 = new HColumnDescriptor("info2");desc.addFamily(family1);desc.addFamily(family2);admin.createTable(desc);}

2.4、删除表,删除之前要设置为禁用状态

   @Testpublic void deleteTable() throws IOException {admin.disableTable(TableName.valueOf("kb21:student"));admin.deleteTable(TableName.valueOf("kb21:student"));}

2.5、添加数据

    @Testpublic void insertData() throws IOException {Table table = conn.getTable(TableName.valueOf("kb21:student"));Put put = new Put(Bytes.toBytes("student1"));put.addColumn("info1".getBytes(),"name".getBytes(),"zs".getBytes());put.addColumn("info2".getBytes(),"school".getBytes(),"njzb".getBytes());Put put2 = new Put(Bytes.toBytes("student2"));put2.addColumn("info1".getBytes(),"name".getBytes(),"zss".getBytes());put2.addColumn("info2".getBytes(),"school".getBytes(),"njzb".getBytes());Put put3 = new Put(Bytes.toBytes("student3"));put3.addColumn("info1".getBytes(),"name".getBytes(),"zsr".getBytes());put3.addColumn("info2".getBytes(),"school".getBytes(),"njzb".getBytes());ArrayList<Put> list = new ArrayList<>();list.add(put2);list.add(put3);table.put(list);}

2.6、获取命令表空间 / tables列表

   @Testpublic void getAllNameSpace() throws IOException {String[] nps = admin.listNamespaces();String s = Arrays.toString(nps);System.out.println(s);//获得表的列族List<TableDescriptor> tableDesc = admin.listTableDescriptorsByNamespace("kb21".getBytes());System.out.println(tableDesc.toString());}

2.7、get方法查看表的内容

    @Testpublic void queryData() throws IOException {Table table = conn.getTable(TableName.valueOf("kb21:student"));Get get = new Get(Bytes.toBytes("student1"));Result result = table.get(get);byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));System.out.println("姓名:" + Bytes.toString(value));value = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));System.out.println("学校:" + Bytes.toString(value));}

2.8、scan方法查看表的内容

    @Testpublic void scanData() throws IOException {Table table = conn.getTable(TableName.valueOf("kb21:student"));Scan scan = new Scan();ResultScanner scanner = table.getScanner(scan);for (Result result: scanner) {byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));System.out.println("姓名:" + Bytes.toString(value));value = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));System.out.println("学校:" + Bytes.toString(value));System.out.println(Bytes.toString(result.getRow()));}}

http://www.lryc.cn/news/34014.html

相关文章:

  • 1109. 航班预订统计 差分数组
  • 图床搭建,使用typora上传
  • 低代码开发的优势是什么?
  • Ip2Resion线上部署报数据越界及错误处理
  • 致敬我的C++启蒙老师,跟着他学计算机编程就对了 (文末赠书5本)
  • CSS中的伪元素和伪类
  • 逻辑优化基础-rewrite
  • 案例27-单表从9个更新语句调整为2个
  • Wordpress paid-memberships-pro plugins CVE-2023-23488未授权SQLi漏洞分析
  • 【JavaWeb篇】JSTL相关知识点总结
  • 【蓝桥杯刷题】坑爹的负进制转换
  • react+antdpro+ts实现企业级项目二:Strapi及认证登陆模块
  • Android ANR trace日志如何导出
  • Windows SSH 配置和SCP的使用
  • liunx 安装redsi和连接
  • 接口里面可以写实现方法吗【可以】 、接口可以多继承吗【可以】
  • 【YOLOv8/YOLOv7/YOLOv5/YOLOv4/Faster-rcnn系列算法改进NO.57】引入可形变卷积
  • 统计学习--三种常见的相关系数
  • 基于Django4.1.4的入门学习记录
  • C++ Butterworth N阶滤波器设计
  • UXP下不用任何框架创建自己的插件并试运行
  • mac修改国内源快速安装brew
  • Me-and-My-Girlfriend-1靶场通关
  • 2.6 棋盘覆盖
  • JMU软件20 大数据技术复习(只写了对比18提纲的变动部分)
  • MySQL底层存储B-Tree和B+Tree原理分析
  • 基于Vue+Vue-cli+webpack搭建渐进式高可维护性前端实战项目
  • 第十三章:Java反射机制
  • iLok USB不识别怎么办?
  • 【LeetCode与《代码随想录》】二叉树篇:做题笔记与总结-JavaScript版