0


大数据技术原理与应用实验2——熟悉常用的Hbase操作

这里写目录标题

一、实验目的

(1)理解HBase在Hadoop体系结构中的角色;
(2)熟练使用HBase操作常用的Shell命令;
(3)熟悉HBase操作常用的Java API。

二、实验环境

(1)Linux操作系统(CentOS7.5)
(2)VMware Workstation Pro 15.5
(3)远程终端工具Xshell7
(4)Xftp7传输工具
(5)Hadoop版本:3.1.3;
(6)HBase版本:2.2.2;
(7)JDK版本:1.8;
(8)Java IDE:Idea。

三、实验内容

(一)编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:

(1) 列出HBase所有的表的相关信息,例如表名;
(2) 在终端打印出指定的表的所有记录数据;
(3) 向已经创建好的表添加和删除指定的列族或列;
(4) 清空指定的表的所有记录数据;
(5) 统计表的行数。

1. 列出HBase所有的表的相关信息,例如表名;

Shell命令

  1. hbase> list

代码

  1. package com.xusheng.HBase.shiyan1;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;import java.util.List;
  2. /**
  3. * 1. 编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
  4. *(1) 列出HBase所有的表的相关信息,例如表名、创建时间等;
  5. *(2) 在终端打印出指定的表的所有记录数据;
  6. *(3) 向已经创建好的表添加和删除指定的列族或列;
  7. *(4) 清空指定的表的所有记录数据;
  8. *(5) 统计表的行数。
  9. */
  10. public class shiyan11 {
  11. public static Configuration configuration;
  12. public static Connection connection;
  13. public static Admin admin;
  14. //建立连接
  15. public static void init(){
  16. configuration = HBaseConfiguration.create();
  17. //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
  18. configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
  19. try{
  20. connection = ConnectionFactory.createConnection(configuration);
  21. admin = connection.getAdmin();}catch (IOException e){
  22. e.printStackTrace();}}
  23. //关闭连接
  24. public static void close(){
  25. try{
  26. if(admin != null){
  27. admin.close();}
  28. if(null != connection){
  29. connection.close();}}catch (IOException e){
  30. e.printStackTrace();}}
  31. //(1)列出HBase所有的表的相关信息,例如表名、创建时间等
  32. public static void listTables(String stu) throws IOException {
  33. init();//建立连接
  34. List<TableDescriptor> tableDescriptors = admin.listTableDescriptors();
  35. for(TableDescriptor tableDescriptor : tableDescriptors){
  36. TableName tableName = tableDescriptor.getTableName();
  37. System.out.println("Table:" + tableName);}
  38. close();//关闭连接
  39. }
  40. public static void main(String[] args) throws IOException {
  41. listTables("s1");}}

结果
在这里插入图片描述
在这里插入图片描述

2. 在终端打印出指定的表的所有记录数据;

Shell命令

  1. hbase> scan 's1'

代码

  1. package com.xusheng.HBase.shiyan1;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;
  2. /**
  3. * 1. 编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
  4. *(1) 列出HBase所有的表的相关信息,例如表名、创建时间等;
  5. *(2) 在终端打印出指定的表的所有记录数据;
  6. *(3) 向已经创建好的表添加和删除指定的列族或列;
  7. *(4) 清空指定的表的所有记录数据;
  8. *(5) 统计表的行数。
  9. */
  10. public class shiyan12 {
  11. public static Configuration configuration;
  12. public static Connection connection;
  13. public static Admin admin;
  14. //建立连接
  15. public static void init(){
  16. configuration = HBaseConfiguration.create();
  17. //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
  18. configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
  19. try{
  20. connection = ConnectionFactory.createConnection(configuration);
  21. admin = connection.getAdmin();}catch (IOException e){
  22. e.printStackTrace();}}
  23. //关闭连接
  24. public static void close(){
  25. try{
  26. if(admin != null){
  27. admin.close();}
  28. if(null != connection){
  29. connection.close();}}catch (IOException e){
  30. e.printStackTrace();}}
  31. //(2)在终端打印出指定的表的所有记录数据
  32. public static void getData(String tableName)throws IOException{
  33. init();
  34. Table table = connection.getTable(TableName.valueOf(tableName));
  35. Scan scan = new Scan();
  36. ResultScanner scanner = table.getScanner(scan);//获取行的遍历器
  37. for(Result result:scanner){
  38. printRecoder(result);}
  39. close();}
  40. //打印一条记录的详情
  41. public static void printRecoder(Result result)throws IOException{
  42. for(Cell cell:result.rawCells()){
  43. System.out.print("行健: "+new String(Bytes.toString(cell.getRowArray(),cell.getRowOffset(), cell.getRowLength())));
  44. System.out.print("列簇: "+new String( Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(), cell.getFamilyLength())));
  45. System.out.print(" 列: "+new String(Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(), cell.getQualifierLength())));
  46. System.out.print(" 值: "+new String(Bytes.toString(cell.getValueArray(),cell.getValueOffset(), cell.getValueLength())));
  47. System.out.println("时间戳: "+cell.getTimestamp());}}
  48. public static void main(String[] args) throws IOException {
  49. getData("s1");}}

结果
在这里插入图片描述
在这里插入图片描述

3. 向已经创建好的表添加和删除指定的列族或列;

Shell命令
先在Shell中创建表s1,作为示例表,命令如下:

  1. hbase> create 's1','score'

然后,可以在s1中添加数据,命令如下:

  1. hbase> put 's1','zhangsan','score:Math','69'

之后,可以执行如下命令删除指定的列:

  1. hbase> delete 's1','zhangsan','score:Math'

代码

  1. package com.xusheng.HBase.shiyan1;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;
  2. /**
  3. * 1. 编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
  4. *(1) 列出HBase所有的表的相关信息,例如表名、创建时间等;
  5. *(2) 在终端打印出指定的表的所有记录数据;
  6. *(3) 向已经创建好的表添加和删除指定的列族或列;
  7. *(4) 清空指定的表的所有记录数据;
  8. *(5) 统计表的行数。
  9. */
  10. public class shiyan13 {
  11. public static Configuration configuration;
  12. public static Connection connection;
  13. public static Admin admin;
  14. //建立连接
  15. public static void init(){
  16. configuration = HBaseConfiguration.create();
  17. //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
  18. configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
  19. try{
  20. connection = ConnectionFactory.createConnection(configuration);
  21. admin = connection.getAdmin();}catch (IOException e){
  22. e.printStackTrace();}}
  23. //关闭连接
  24. public static void close(){
  25. try{
  26. if(admin != null){
  27. admin.close();}
  28. if(null != connection){
  29. connection.close();}}catch (IOException e){
  30. e.printStackTrace();}}
  31. //(3)想已经创建好的表添加和删除指定的列族或列
  32. //向表添加数据
  33. public static void insterRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
  34. init();
  35. Table table = connection.getTable(TableName.valueOf(tableName));
  36. Put put = new Put(rowKey.getBytes());
  37. put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
  38. table.put(put);
  39. table.close();
  40. close();}
  41. //删除数据
  42. public static void deleRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
  43. init();
  44. Table table = connection.getTable(TableName.valueOf(tableName));
  45. Delete delete = new Delete(rowKey.getBytes());
  46. //删除指定列族
  47. delete.addFamily(Bytes.toBytes(colFamily));
  48. //删除指定列
  49. delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
  50. table.delete(delete);
  51. table.close();
  52. close();}
  53. public static void main(String[] args) throws IOException {
  54. insterRow("s1","1001","score1","english","33");
  55. deleRow("s1","zhangsan","score","Math");}}

结果
在这里插入图片描述

4. 清空指定的表的所有记录数据;

Shell命令

  1. hbase> truncate 's1'

代码

  1. package com.xusheng.HBase.shiyan1;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;
  2. /**
  3. * 1. 编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
  4. *(1) 列出HBase所有的表的相关信息,例如表名、创建时间等;
  5. *(2) 在终端打印出指定的表的所有记录数据;
  6. *(3) 向已经创建好的表添加和删除指定的列族或列;
  7. *(4) 清空指定的表的所有记录数据;
  8. *(5) 统计表的行数。
  9. */
  10. public class shiyan14 {
  11. public static Configuration configuration;
  12. public static Connection connection;
  13. public static Admin admin;
  14. //建立连接
  15. public static void init(){
  16. configuration = HBaseConfiguration.create();
  17. //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
  18. configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
  19. try{
  20. connection = ConnectionFactory.createConnection(configuration);
  21. admin = connection.getAdmin();}catch (IOException e){
  22. e.printStackTrace();}}
  23. //关闭连接
  24. public static void close(){
  25. try{
  26. if(admin != null){
  27. admin.close();}
  28. if(null != connection){
  29. connection.close();}}catch (IOException e){
  30. e.printStackTrace();}}
  31. //(4)清空指定的表的所有记录数据
  32. public static void clearRows(String tableName)throws IOException{
  33. init();
  34. TableName tablename = TableName.valueOf(tableName);
  35. admin.disableTable(tablename);
  36. admin.deleteTable(tablename);
  37. TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tablename);
  38. admin.createTable(tableDescriptor.build());
  39. close();}
  40. public static void main(String[] args) throws IOException {
  41. clearRows("tableName");}}

5. 统计表的行数

Shell命令

  1. hbase> count 'Student'

代码

  1. package com.xusheng.HBase.shiyan1;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;
  2. /**
  3. * 1. 编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
  4. *(1) 列出HBase所有的表的相关信息,例如表名、创建时间等;
  5. *(2) 在终端打印出指定的表的所有记录数据;
  6. *(3) 向已经创建好的表添加和删除指定的列族或列;
  7. *(4) 清空指定的表的所有记录数据;
  8. *(5) 统计表的行数。
  9. */
  10. public class shiyan15 {
  11. public static Configuration configuration;
  12. public static Connection connection;
  13. public static Admin admin;
  14. //建立连接
  15. public static void init(){
  16. configuration = HBaseConfiguration.create();
  17. //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
  18. configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
  19. try{
  20. connection = ConnectionFactory.createConnection(configuration);
  21. admin = connection.getAdmin();}catch (IOException e){
  22. e.printStackTrace();}}
  23. //关闭连接
  24. public static void close(){
  25. try{
  26. if(admin != null){
  27. admin.close();}
  28. if(null != connection){
  29. connection.close();}}catch (IOException e){
  30. e.printStackTrace();}}
  31. //(5)统计表的行数
  32. public static void countRows(String tableName)throws IOException{
  33. init();
  34. Table table = connection.getTable(TableName.valueOf(tableName));
  35. Scan scan = new Scan();
  36. ResultScanner scanner = table.getScanner(scan);
  37. int num =0;for(Result result = scanner.next();result!=null;result=scanner.next()){
  38. num++;}
  39. System.out.println("行数:"+ num);
  40. scanner.close();
  41. close();}
  42. public static void main(String[] args) throws IOException {
  43. countRows("Student");}}

结果
在这里插入图片描述

6. 整合代码

代码

  1. package com.xusheng.HBase.shiyan;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.client.ClusterConnection;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;import java.util.Scanner;
  2. public class xu {
  3. public static Configuration configuration;
  4. public static Connection connection;
  5. public static Admin admin;
  6. public static long ts;
  7. static Scanner sc=new Scanner(System.in);
  8. public static void main(String[] args) throws IOException{while(true){
  9. System.out.println("\n1、列出所有表的信息;");
  10. System.out.println("2、打印指定表的所有记录数据;");
  11. System.out.println("3、向创建好的表添加或删除指定的列族或列;");
  12. System.out.println("4、清空指定表的所有记录数据;");
  13. System.out.println("5、通过表的行数;");
  14. System.out.println("请输入你的选择:");
  15. int no=sc.nextInt();if(no==1){
  16. First();}else if(no==2){
  17. System.out.println("输入你要查询的表:");
  18. String tablename=sc.next();
  19. Second(tablename);}else if(no==3){
  20. System.out.println("输入你要操作的表:");
  21. String tablename=sc.next();
  22. System.out.println("输入你要操作的行建名:");
  23. String rowKey=sc.next();
  24. System.out.println("输入你要操作的列组名:");
  25. String colFamily=sc.next();
  26. System.out.println("输入你要操作的列限定符名:");
  27. String col=sc.next();
  28. System.out.println("输入你要操作的参数值:");
  29. String val=sc.next();
  30. Third(tablename,rowKey,colFamily,col,val);}else if(no==4){
  31. System.out.println("输入你要操作的表:");
  32. String tablename=sc.next();
  33. Fourth(tablename);
  34. System.out.println("成功清空!");}elseif(no==5){
  35. System.out.println("输入你要操作的表:");
  36. String tablename=sc.next();
  37. fift(tablename);}}}
  38. public static void init(){
  39. configuration = HBaseConfiguration.create();
  40. //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
  41. configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
  42. try{
  43. connection = ConnectionFactory.createConnection(configuration);
  44. admin = connection.getAdmin();}catch (IOException e){
  45. e.printStackTrace();}}
  46. public static void close(){
  47. try{
  48. if(admin != null){
  49. admin.close();}
  50. if(null != connection){
  51. connection.close();}}catch (IOException e){
  52. e.printStackTrace();}}
  53. public static void First() throws IOException{
  54. init();
  55. HTableDescriptor hTableDescriptor[]=admin.listTables();for(HTableDescriptor s:hTableDescriptor ){
  56. System.out.println(s.getNameAsString());}
  57. close();}
  58. public static void Second(String tablename) throws IOException{
  59. init();
  60. Table table=connection.getTable(TableName.valueOf(tablename));
  61. Scan scan=new Scan();
  62. ResultScanner res=table.getScanner(scan);for(Result result:res){
  63. showCell(result);}}
  64. public static void Third(String tableName, String row, String column,String c ,String val) throws IOException{
  65. System.out.println("1、添加列;2、删除列");
  66. int no=sc.nextInt();if(no==1){
  67. insertRow(tableName,row,column,c,val);}else if(no==2){
  68. deleteRow(tableName,row,column,c);}}
  69. public static void Fourth(String tablename) throws IOException{
  70. init();
  71. //HBaseAdmin admin1=new HBaseAdmin(configuration);
  72. //HTableDescriptor ht=admin1.getTableDescriptor(TableName.valueOf(Bytes.toBytes(tablename)));
  73. TableName tableName=TableName.valueOf(tablename);
  74. admin.disableTable(tableName);
  75. admin.deleteTable(tableName);
  76. //admin.createTable(ht);
  77. close();}
  78. public static void fift(String tablename) throws IOException{
  79. init();
  80. Table table=connection.getTable(TableName.valueOf(tablename));
  81. Scan scan=new Scan();
  82. ResultScanner scanner=table.getScanner(scan);
  83. int n=0;for(Result result=scanner.next();result!=null;result=scanner.next()){
  84. n++;}
  85. System.out.println("行数有"+n);
  86. scanner.close();
  87. close();}
  88. public static void insertRow(String tableName, String row, String column,String c ,String val) throws IOException {
  89. init();
  90. Table table=connection.getTable(TableName.valueOf(tableName));
  91. Put put=new Put(row.getBytes());
  92. put.addColumn(column.getBytes(), c.getBytes(), val.getBytes());
  93. table.put(put);
  94. System.out.println("成功添加!");
  95. table.close();
  96. close();}
  97. public static void deleteRow(String tableName, String row, String column,String c) throws IOException{
  98. init();
  99. Table table=connection.getTable(TableName.valueOf(tableName));
  100. System.out.println("1、删除列族;2、删除列限定符");
  101. Scanner sc=new Scanner(System.in);
  102. int no=sc.nextInt();
  103. Delete delete=new Delete(row.getBytes());if(no==1){
  104. delete.addFamily(Bytes.toBytes(column));
  105. System.out.println("成功删除"+column+"这个列族");}else if(no==2){
  106. delete.addColumn(Bytes.toBytes(column), Bytes.toBytes(c));
  107. System.out.println("成功删除"+c+"这个列限定符");}
  108. table.delete(delete);
  109. table.close();
  110. close();}
  111. public static void showCell(Result result){
  112. Cell[] cells = result.rawCells();
  113. for(Cell cell:cells){
  114. System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
  115. System.out.println("Timetamp:"+cell.getTimestamp()+" ");
  116. System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
  117. System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
  118. System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");}}}

结果
(1) 列出HBase所有的表的相关信息,例如表名;
在这里插入图片描述

(2) 在终端打印出指定的表的所有记录数据;
在这里插入图片描述

(3) 向已经创建好的表添加和删除指定的列族或列;
在这里插入图片描述
前后对比:
在这里插入图片描述
在这里插入图片描述

(4) 清空指定的表的所有记录数据;
在这里插入图片描述

(5) 统计表的行数。
在这里插入图片描述

(二)HBase数据库操作

1. 现有以下关系型数据库中的表和数据(见表14-3到表14-5),要求将其转换为适合于HBase存储的表并插入数据:

表14-3 学生表(Student)

在这里插入图片描述
(1)学生Student表
创建表的HBase Shell命令语句如下:

  1. create 'Student','S_No','S_Name','S_Sex','S_Age'

插入数据的HBase Shell命令如下:
第一行数据

  1. put 'Student','s001','S_No','2015001'
  2. put 'Student','s001','S_Name','Zhangsan'
  3. put 'Student','s001','S_Sex','male'
  4. put 'Student','s001','S_Age','23'

在这里插入图片描述

第二行数据

  1. put 'Student','s002','S_No','2015002'
  2. put 'Student','s002','S_Name','Mary'
  3. put 'Student','s002','S_Sex','female'
  4. put 'Student','s002','S_Age','22'

在这里插入图片描述

第三行数据

  1. put 'Student','s003','S_No','2015003'
  2. put 'Student','s003','S_Name','Lisi'
  3. put 'Student','s003','S_Sex','male'
  4. put 'Student','s003','S_Age','24'

在这里插入图片描述
在这里插入图片描述

表14-4 课程表(Course)
在这里插入图片描述
(2)课程Course表
创建表的HBase Shell命令语句如下:

  1. create 'Course','C_No','C_Name','C_Credit'

插入数据的HBase Shell命令如下:
第一行数据

  1. put 'Course','c001','C_No','123001'
  2. put 'Course','c001','C_Name','Math'
  3. put 'Course','c001','C_Credit','2.0'

第二行数据

  1. put 'Course','c002','C_No','123002'
  2. put 'Course','c002','C_Name','Computer'
  3. put 'Course','c002','C_Credit','5.0'

第三行数据

  1. put 'Course','c003','C_No','123003'
  2. put 'Course','c003','C_Name','English'
  3. put 'Course','c003','C_Credit','3.0'

在这里插入图片描述
在这里插入图片描述

表14-5 选课表(SC)
在这里插入图片描述
(3)选课表
创建表的HBase Shell命令语句如下:

  1. create 'SC','SC_Sno','SC_Cno','SC_Score'

插入数据的HBase Shell命令如下:
第一行数据

  1. put 'SC','sc001','SC_Sno','2015001'
  2. put 'SC','sc001','SC_Cno','123001'
  3. put 'SC','sc001','SC_Score','86'

第二行数据

  1. put 'SC','sc002','SC_Sno','2015001'
  2. put 'SC','sc002','SC_Cno','123003'
  3. put 'SC','sc002','SC_Score','69'

第三行数据

  1. put 'SC','sc003','SC_Sno','2015002'
  2. put 'SC','sc003','SC_Cno','123002'
  3. put 'SC','sc003','SC_Score','77'

第四行数据

  1. put 'SC','sc004','SC_Sno','2015002'
  2. put 'SC','sc004','SC_Cno','123003'
  3. put 'SC','sc004','SC_Score','99'

第五行数据

  1. put 'SC','sc005','SC_Sno','2015003'
  2. put 'SC','sc005','SC_Cno','123001'
  3. put 'SC','sc005','SC_Score','98'

第六行数据

  1. put 'SC','sc006','SC_Sno','2015003'
  2. put 'SC','sc006','SC_Cno','123002'
  3. put 'SC','sc006','SC_Score','95'

在这里插入图片描述

2. 请编程实现以下功能:

(1)createTable(String tableName, String[] fields)

创建表,参数tableName为表的名称,字符串数组fields为存储记录各个字段名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。
代码:

  1. package com.xusheng.HBase.shiyan31;import org.apache.hadoop.conf.Configuration;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.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;
  2. public class CreateTable {
  3. public static Configuration configuration;
  4. public static Connection connection;
  5. public static Admin admin;
  6. public static void createTable(String tableName,String[] fields) throws IOException {
  7. init();
  8. TableName tablename = TableName.valueOf(tableName);
  9. if(admin.tableExists(tablename)){
  10. System.out.println("table is exists!");
  11. admin.disableTable(tablename);
  12. admin.deleteTable(tablename);//删除原来的表
  13. }
  14. TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tablename);
  15. for(String str : fields){
  16. tableDescriptor.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build());
  17. admin.createTable(tableDescriptor.build());}
  18. close();}
  19. //建立连接
  20. public static void init(){
  21. configuration = HBaseConfiguration.create();
  22. //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
  23. configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
  24. try {
  25. connection = ConnectionFactory.createConnection(configuration);
  26. admin = connection.getAdmin();} catch (IOException e){
  27. e.printStackTrace();}}
  28. //关闭连接
  29. public static void close(){
  30. try {if(admin != null){
  31. admin.close();}if(null != connection){
  32. connection.close();}} catch (IOException e){
  33. e.printStackTrace();}}
  34. public static void main(String[] args){
  35. String[] fields ={"Score"};
  36. try {
  37. createTable("person", fields);} catch (IOException e){
  38. e.printStackTrace();}}}

结果:
在这里插入图片描述
在这里插入图片描述

(2)addRecord(String tableName, String row, String[] fields, String[] values)

向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对应的数据values。其中,fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,字符串数组fields为{“Score:Math”, ”Score:Computer Science”, ”Score:English”},数组values存储这三门课的成绩。
代码:

  1. package com.xusheng.HBase.shiyan31;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import java.io.IOException;
  2. public class addRecord {
  3. public static Configuration configuration;
  4. public static Connection connection;
  5. public static Admin admin;
  6. public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {
  7. init();
  8. Table table = connection.getTable(TableName.valueOf(tableName));for(int i =0; i != fields.length; i++){
  9. Put put = new Put(row.getBytes());
  10. String[] cols = fields[i].split(":");
  11. put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());
  12. table.put(put);}
  13. table.close();
  14. close();}
  15. public static void init(){
  16. configuration = HBaseConfiguration.create();
  17. //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
  18. configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
  19. try {
  20. connection = ConnectionFactory.createConnection(configuration);
  21. admin = connection.getAdmin();} catch (IOException e){
  22. e.printStackTrace();}}
  23. public static void close(){
  24. try {if(admin != null){
  25. admin.close();}if(null != connection){
  26. connection.close();}} catch (IOException e){
  27. e.printStackTrace();}}
  28. public static void main(String[] args){
  29. String[] fields ={"Score:Math", "Score:Computer Science", "Score:English"};
  30. String[] values ={"99", "80", "100"};
  31. try {
  32. addRecord("tableName", "Score", fields, values);} catch (IOException e){
  33. e.printStackTrace();}}}

结果:
在这里插入图片描述

(3)scanColumn(String tableName, String column)

浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。
代码:

  1. package com.xusheng.HBase.shiyan31;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.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;
  2. public class scanColumn {
  3. public static Configuration configuration;
  4. public static Connection connection;
  5. public static Admin admin;
  6. public static void scanColumn(String tableName, String column) throws IOException {
  7. init();
  8. Table table = connection.getTable(TableName.valueOf(tableName));
  9. Scan scan = new Scan();
  10. scan.addFamily(Bytes.toBytes(column));
  11. ResultScanner scanner = table.getScanner(scan);for(Result result = scanner.next(); result != null; result = scanner.next()){
  12. showCell(result);}
  13. table.close();
  14. close();}
  15. public static void showCell(Result result){
  16. Cell[] cells = result.rawCells();for(Cell cell : cells){
  17. System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " ");
  18. System.out.println("Timetamp:" + cell.getTimestamp() + " ");
  19. System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " ");
  20. System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)) + " ");
  21. System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " ");}}
  22. public static void init(){
  23. configuration = HBaseConfiguration.create();
  24. //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
  25. configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
  26. try {
  27. connection = ConnectionFactory.createConnection(configuration);
  28. admin = connection.getAdmin();} catch (IOException e){
  29. e.printStackTrace();}}
  30. // 关闭连接
  31. public static void close(){
  32. try {if(admin != null){
  33. admin.close();}if(null != connection){
  34. connection.close();}} catch (IOException e){
  35. e.printStackTrace();}}
  36. public static void main(String[] args){
  37. try {
  38. scanColumn("tableName", "Score");} catch (IOException e){
  39. e.printStackTrace();}}}

结果:
在这里插入图片描述

(4)modifyData(String tableName, String row, String column)

修改表tableName,行row(可以用学生姓名S_Name表示),列column指定的单元格的数据。
代码:

  1. package com.xusheng.HBase.shiyan31;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import java.io.IOException;
  2. public class modifyData {
  3. public static long ts;
  4. public static Configuration configuration;
  5. public static Connection connection;
  6. public static Admin admin;
  7. public static void modifyData(String tableName, String row, String column, String val) throws IOException {
  8. init();
  9. Table table = connection.getTable(TableName.valueOf(tableName));
  10. Put put = new Put(row.getBytes());
  11. Scan scan = new Scan();
  12. ResultScanner resultScanner = table.getScanner(scan);for(Result r : resultScanner){for(Cell cell : r.getColumnCells(row.getBytes(), column.getBytes())){
  13. ts = cell.getTimestamp();}}
  14. put.addColumn(row.getBytes(), column.getBytes(), ts, val.getBytes());
  15. table.put(put);
  16. table.close();
  17. close();}
  18. public static void init(){
  19. configuration = HBaseConfiguration.create();
  20. //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
  21. configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
  22. try {
  23. connection = ConnectionFactory.createConnection(configuration);
  24. admin = connection.getAdmin();} catch (IOException e){
  25. e.printStackTrace();}}
  26. public static void close(){
  27. try {if(admin != null){
  28. admin.close();}if(null != connection){
  29. connection.close();}} catch (IOException e){
  30. e.printStackTrace();}}
  31. public static void main(String[] args){
  32. try {
  33. modifyData("tableName", "Score", "Math", "100");} catch (IOException e){
  34. e.printStackTrace();}}}

结果:
在这里插入图片描述

(5)deleteRow(String tableName, String row)

删除表tableName中row指定的行的记录。
代码:

  1. package com.xusheng.HBase.shiyan31;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;
  2. public class deleteRow {
  3. public static long ts;
  4. public static Configuration configuration;
  5. public static Connection connection;
  6. public static Admin admin;
  7. public static void deleteRow(String tableName, String row) throws IOException {
  8. init();
  9. Table table = connection.getTable(TableName.valueOf(tableName));
  10. Delete delete=new Delete(row.getBytes());
  11. table.delete(delete);
  12. table.close();
  13. close();}
  14. public static void init(){
  15. configuration = HBaseConfiguration.create();
  16. //configuration.set("hbase.rootdir", "hdfs://hadoop102:8020/HBase");
  17. configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");
  18. try {
  19. connection = ConnectionFactory.createConnection(configuration);
  20. admin = connection.getAdmin();} catch (IOException e){
  21. e.printStackTrace();}}
  22. public static void close(){
  23. try {if(admin != null){
  24. admin.close();}if(null != connection){
  25. connection.close();}} catch (IOException e){
  26. e.printStackTrace();}}
  27. public static void main(String[] args){
  28. try {
  29. deleteRow("tableName", "Score");} catch (IOException e){
  30. e.printStackTrace();}}}

结果:
在这里插入图片描述
对比运行前和运行后的结果,可以看出删除一行数据成功!

四、心得体会

(1)HB ase 定义
HBase 是一种分布式、可扩展、支持海量数据存储的 NoSQL 数据库。
(2)HBase 数据模型
逻辑上,HBase 的数据模型同关系型数据库很类似,数据存储在一张表中,有行有列。
但从 HBase 的底层物理存储结构(K-V)来看,HBase 更像是一个 multi-dimensional map。
(3)HBase 逻辑结构

在这里插入图片描述

标签: hbase big data java

本文转载自: https://blog.csdn.net/m0_52435951/article/details/124718615
版权归原作者 虚神公子 所有, 如有侵权,请联系我们删除。

“大数据技术原理与应用实验2——熟悉常用的Hbase操作”的评论:

还没有评论