0


HBase Java API编程-学习记录

目录列表

Hbase伪分布式环境部署见:Hbase环境部署

前置准备

启动hdfs和hbase并验证环境正常

启动服务

通过

  1. shell

命令启动hdfs文件系统和hbase服务,

  1. jps

下出现如下图所示的进程即为正确。

  1. start-all.sh # 启动HDFS文件系统 (datanode, namenode, secondary namenode)
  2. start-hbase.sh # 启动HBase服务 (HMaster, HRegionServer, HQuorumPeer)
  3. jps # 查看当前运行的任务进程

在这里插入图片描述

验证运行环境

进入

  1. hbase shell

终端或进入

  1. hbase

提供的web页面(

  1. HBase

默认端口:16030),验证环境正常。
在这里插入图片描述

HBase web页面如下图所示

在这里插入图片描述

搭建HBase客户端

使用IDEA创建Maven项目

在这里插入图片描述

修改

  1. pom.xml

配置文件

为项目添加

  1. hbase-lient

的依赖(刷新完成不飘红即为引入成功)

在这里插入图片描述

添加

  1. log4j

输出日志的依赖(刷新完成不飘红即为引入成功)

在这里插入图片描述

使用Java API操作HBase

Java连接Hbase

  1. // 静态代码块static{Configuration conf =HBaseConfiguration.create();
  2. conf.set("hbase.zookeeper.quorum","192.168.159.139");//这里替换成自己Hbase的IP地址try{//创建HBase连接对象
  3. connection =ConnectionFactory.createConnection(conf);}catch(IOException ex){
  4. ex.printStackTrace();}}

创建命名空间

  1. publicstaticvoidcreateNameSpace(String nameSpaceName)throwsIOException{if(nameSpaceName ==null|| nameSpaceName.isEmpty()){
  2. logger.error("namespace 不能为空!");return;}Admin admin = connection.getAdmin();NamespaceDescriptor.Builder builder =NamespaceDescriptor.create(nameSpaceName);NamespaceDescriptor namespaceDescriptor = builder.build();try{
  3. admin.createNamespace(namespaceDescriptor);
  4. logger.info("namespace: {} 创建成功", nameSpaceName);}catch(NamespaceExistException e){
  5. logger.error("namespace 名字已存在!");}}

运行截图:

在这里插入图片描述

  1. Hbase

中前后对比:

在这里插入图片描述

表的创建和删除

  1. // 表的创建和删除publicstaticvoidcreateTable(){TableName tableName =TableName.valueOf("stu");try(Admin admin = connection.getAdmin()){// 检查表是否存在if(admin.tableExists(tableName)){
  2. admin.disableTable(tableName);
  3. admin.deleteTable(tableName);
  4. logger.info(tableName.toString()+" exists, deleted it.");}// 创建表描述符TableDescriptor tableDescriptor =TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(ColumnFamilyDescriptorBuilder.of("StuInfo")).setColumnFamily(ColumnFamilyDescriptorBuilder.of("Grades")).build();// 创建表
  5. admin.createTable(tableDescriptor);
  6. logger.info("Table "+ tableName +" created successfully.");}catch(IOException e){
  7. logger.error("Error: "+ e.getMessage());
  8. e.printStackTrace();}}

运行截图:

在这里插入图片描述

  1. Hbase

中前后对比:

在这里插入图片描述

插入数据

  1. // 数据插入publicstaticvoidaddData()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Put put1 =newPut(Bytes.toBytes("row1"));
  2. put1.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Jack"));
  3. put1.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("18"));
  4. put1.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("90"));
  5. put1.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("85"));
  6. table.put(put1);Put put2 =newPut(Bytes.toBytes("row2"));
  7. put2.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Alice"));
  8. put2.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("19"));
  9. put2.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("88"));
  10. put2.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("92"));
  11. table.put(put2);Put put3 =newPut(Bytes.toBytes("row3"));
  12. put3.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Bob"));
  13. put3.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("20"));
  14. put3.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("95"));
  15. put3.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("80"));
  16. table.put(put3);Put put4 =newPut(Bytes.toBytes("row4"));
  17. put4.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Emma"));
  18. put4.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("21"));
  19. put4.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("82"));
  20. put4.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("90"));
  21. table.put(put4);Put put5 =newPut(Bytes.toBytes("row5"));
  22. put5.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("David"));
  23. put5.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("22"));
  24. put5.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("91"));
  25. put5.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("88"));
  26. table.put(put5);
  27. logger.info("SUCCESS");
  28. table.close();}

运行截图:

在这里插入图片描述

  1. Hbase

表中的变化:

在这里插入图片描述

数据查询

行键查询

  1. // 数据获取(通过行键查询)publicstaticvoidgetData()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Get get =newGet(Bytes.toBytes("row1"));Result result = table.get(get);for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}
  2. table.close();}

运行截图:

在这里插入图片描述

全部查询

  1. // 全部查询publicstaticvoidgetDataAll()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Scan scan =newScan();ResultScanner scanner = table.getScanner(scan);for(Result result : scanner){for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}}
  2. scanner.close();
  3. table.close();}

运行截图:

在这里插入图片描述

特定前缀列查询

  1. // 使用ColumnPrefixFilter过滤器 查询特定列前缀的列publicstaticvoidgetDataWithColumnPrefixFilter()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Filter filter =newColumnPrefixFilter(Bytes.toBytes("na"));Scan scan =newScan();
  2. scan.setFilter(filter);ResultScanner scanner = table.getScanner(scan);for(Result result : scanner){for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}}
  3. scanner.close();
  4. table.close();}

运行截图:

在这里插入图片描述

完整代码

  1. packageorg.example;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.*;importorg.apache.hadoop.hbase.client.*;importorg.apache.hadoop.hbase.filter.*;importorg.apache.hadoop.hbase.util.Bytes;importorg.apache.logging.log4j.LogManager;importorg.apache.logging.log4j.Logger;importjava.io.IOException;publicclassHBaseDemo{privatestaticConnection connection;privatestaticfinalLogger logger =LogManager.getLogger(HBaseDemo.class);static{Configuration conf =HBaseConfiguration.create();
  2. conf.set("hbase.zookeeper.quorum","192.168.159.139");try{
  3. connection =ConnectionFactory.createConnection(conf);
  4. logger.info("Hbase 连接成功!");}catch(IOException ex){
  5. logger.error(ex.getMessage(), ex);}}//创建命名空间publicstaticvoidcreateNameSpace(String nameSpaceName)throwsIOException{if(nameSpaceName ==null|| nameSpaceName.isEmpty()){
  6. logger.error("namespace 不能为空!");return;}Admin admin = connection.getAdmin();NamespaceDescriptor.Builder builder =NamespaceDescriptor.create(nameSpaceName);NamespaceDescriptor namespaceDescriptor = builder.build();try{
  7. admin.createNamespace(namespaceDescriptor);
  8. logger.info("namespace: {} 创建成功", nameSpaceName);}catch(NamespaceExistException e){
  9. logger.error("namespace 名字已存在!");}}// 表的创建和删除publicstaticvoidcreateTable(){TableName tableName =TableName.valueOf("stu");try(Admin admin = connection.getAdmin()){// 检查表是否存在if(admin.tableExists(tableName)){
  10. admin.disableTable(tableName);
  11. admin.deleteTable(tableName);
  12. logger.info(tableName.toString()+" exists, deleted it.");}// 创建表描述符TableDescriptor tableDescriptor =TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(ColumnFamilyDescriptorBuilder.of("StuInfo")).setColumnFamily(ColumnFamilyDescriptorBuilder.of("Grades")).build();// 创建表
  13. admin.createTable(tableDescriptor);
  14. logger.info("Table "+ tableName +" created successfully.");}catch(IOException e){
  15. logger.error("Error: "+ e.getMessage());
  16. e.printStackTrace();}}// 数据插入publicstaticvoidaddData()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Put put1 =newPut(Bytes.toBytes("row1"));
  17. put1.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Jack"));
  18. put1.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("18"));
  19. put1.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("90"));
  20. put1.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("85"));
  21. table.put(put1);Put put2 =newPut(Bytes.toBytes("row2"));
  22. put2.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Alice"));
  23. put2.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("19"));
  24. put2.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("88"));
  25. put2.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("92"));
  26. table.put(put2);Put put3 =newPut(Bytes.toBytes("row3"));
  27. put3.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Bob"));
  28. put3.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("20"));
  29. put3.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("95"));
  30. put3.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("80"));
  31. table.put(put3);Put put4 =newPut(Bytes.toBytes("row4"));
  32. put4.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("Emma"));
  33. put4.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("21"));
  34. put4.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("82"));
  35. put4.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("90"));
  36. table.put(put4);Put put5 =newPut(Bytes.toBytes("row5"));
  37. put5.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("name"),Bytes.toBytes("David"));
  38. put5.addColumn(Bytes.toBytes("StuInfo"),Bytes.toBytes("age"),Bytes.toBytes("22"));
  39. put5.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("math"),Bytes.toBytes("91"));
  40. put5.addColumn(Bytes.toBytes("Grades"),Bytes.toBytes("english"),Bytes.toBytes("88"));
  41. table.put(put5);
  42. logger.info("SUCCESS");
  43. table.close();}// 全部查询publicstaticvoidgetDataAll()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Scan scan =newScan();ResultScanner scanner = table.getScanner(scan);for(Result result : scanner){for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}}
  44. scanner.close();
  45. table.close();}// 数据获取 - 行键publicstaticvoidgetDataRow()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Get get =newGet(Bytes.toBytes("row1"));Result result = table.get(get);for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}
  46. table.close();}// 使用ColumnPrefixFilter过滤器 查询特定列前缀的列publicstaticvoidgetDataWithColumnPrefixFilter()throwsIOException{Table table = connection.getTable(TableName.valueOf("stu"));Filter filter =newColumnPrefixFilter(Bytes.toBytes("na"));Scan scan =newScan();
  47. scan.setFilter(filter);ResultScanner scanner = table.getScanner(scan);for(Result result : scanner){for(Cell cell : result.rawCells()){System.out.println(CellUtil.getCellKeyAsString(cell));System.out.println(newString(CellUtil.cloneQualifier(cell)));System.out.println(newString(CellUtil.cloneValue(cell)));System.out.println(cell.getTimestamp());System.out.println("----------------------------");}}
  48. scanner.close();
  49. table.close();}publicstaticvoidmain(String[] args)throwsIOException{// 创建命名空间// createNameSpace("test");// 表的创建和删除// createTable();// 数据插入// addData();// 数据获取// getDataRow();// 全部查询// getDataAll();// 使用PrefixFilter过滤器查询// getDataWithColumnPrefixFilter();}}

在这里插入图片描述

@鲨鱼爱兜兜

标签: hbase java 学习

本文转载自: https://blog.csdn.net/m0_73641772/article/details/142721564
版权归原作者 鲨鱼爱兜兜 所有, 如有侵权,请联系我们删除。

“HBase Java API编程-学习记录”的评论:

还没有评论