• HBase学习笔记(2)—— API使用


    HBase中常用的API操作进行简单的介绍

    对应HBase学习笔记(1)—— 知识点总结-CSDN博客中介绍的HBase Shell常用操作

    更多用法请参考官网:Apache HBase ™ Reference Guide

    依赖导入

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.apache.hbasegroupId>
    4. <artifactId>hbase-serverartifactId>
    5. <version>2.4.11version>
    6. <exclusions>
    7. <exclusion>
    8. <groupId>org.glassfishgroupId>
    9. <artifactId>javax.elartifactId>
    10. exclusion>
    11. exclusions>
    12. dependency>
    13. <dependency>
    14. <groupId>org.glassfishgroupId>
    15. <artifactId>javax.elartifactId>
    16. <version>3.0.1-b06version>
    17. dependency>
    18. dependencies>

    打包所用的插件:

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.apache.maven.pluginsgroupId>
    5. <artifactId>maven-shade-pluginartifactId>
    6. <version>3.2.4version>
    7. <executions>
    8. <execution>
    9. <phase>packagephase>
    10. <goals>
    11. <goal>shadegoal>
    12. goals>
    13. execution>
    14. executions>
    15. plugin>
    16. plugins>
    17. build>

    建立连接

    1. package com.why;
    2. import org.apache.hadoop.hbase.client.Connection;
    3. import org.apache.hadoop.hbase.client.ConnectionFactory;
    4. import java.io.IOException;
    5. public class HBaseConnect2 {
    6. public static Connection connection = null;
    7. static {
    8. try {
    9. connection = ConnectionFactory.createConnection(); //使用配置文件中的参数hbase.zookeeper.quorum
    10. System.out.println(connection);
    11. } catch (IOException e) {
    12. System.out.println("连接创建失败");
    13. throw new RuntimeException(e);
    14. }
    15. }
    16. /**
    17. * 关闭连接
    18. * @throws IOException
    19. */
    20. public static void closeConnection() throws IOException {
    21. if (connection != null)
    22. {
    23. connection.close();
    24. }
    25. }
    26. }

    DDL操作

    1. package com.why;
    2. import org.apache.hadoop.hbase.HTableDescriptor;
    3. import org.apache.hadoop.hbase.NamespaceDescriptor;
    4. import org.apache.hadoop.hbase.TableName;
    5. import org.apache.hadoop.hbase.client.*;
    6. import org.apache.hadoop.hbase.util.Bytes;
    7. import java.io.IOException;
    8. public class HBaseDDL {
    9. //获取连接
    10. private static Connection connection = HBaseConnect2.connection;
    11. /**
    12. * 创建命名空间
    13. * @param namespace 命名空间名称
    14. * @throws IOException
    15. */
    16. public static void createNamespace(String namespace) throws IOException {
    17. //获取admin(admin的连接是轻量级的,不是线程安全的,不推荐池化或缓存)
    18. Admin admin = connection.getAdmin();
    19. //创建命名空间builder
    20. NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(namespace);
    21. // builder.addConfiguration("user","why");
    22. try{
    23. admin.createNamespace(builder.build());
    24. }catch (IOException e)
    25. {
    26. System.out.println("命名空间已经存在");
    27. e.printStackTrace();
    28. }
    29. admin.close();
    30. }
    31. /**
    32. * 查看所有命名空间
    33. * @throws IOException
    34. */
    35. public static void listNamespaces() throws IOException {
    36. Admin admin = connection.getAdmin();
    37. try {
    38. String[] strings = admin.listNamespaces();
    39. System.out.println("命名空间如下:");
    40. for(String string : strings)
    41. {
    42. System.out.println(string);
    43. }
    44. }catch (IOException e)
    45. {
    46. e.printStackTrace();
    47. }
    48. admin.close();
    49. }
    50. /**
    51. * 判断表格是否存在
    52. * @param namespace 命名空间
    53. * @param table 表格名称
    54. * @return
    55. * @throws IOException
    56. */
    57. public static boolean isTableExist(String namespace,String table) throws IOException {
    58. Admin admin = connection.getAdmin();
    59. boolean isExist = false;
    60. try {
    61. //通过tableExists方法判断表格是否存在
    62. isExist = admin.tableExists(TableName.valueOf(namespace,table));
    63. }catch (IOException e)
    64. {
    65. e.printStackTrace();
    66. }
    67. admin.close();
    68. return isExist;
    69. }
    70. /**
    71. * 创建表格
    72. * @param namespace 命名空间
    73. * @param table 表格名称
    74. * @param columnFamilies 列族名称
    75. * @throws IOException
    76. */
    77. public static void createTable(String namespace , String table , String... columnFamilies) throws IOException {
    78. //判断是否至少有一个列族
    79. if(columnFamilies.length == 0)
    80. {
    81. System.out.println("至少要有一个列族");
    82. return;
    83. }
    84. //判断表格是否已经存在
    85. if(isTableExist(namespace,table))
    86. {
    87. System.out.println("表格已经存在");
    88. return;
    89. }
    90. Admin admin = connection.getAdmin();
    91. //创建表格描述的构建器
    92. TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(namespace, table));
    93. //添加参数
    94. for(String columnFamily : columnFamilies)
    95. {
    96. //创建列族描述的构建器
    97. ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily));
    98. columnFamilyDescriptorBuilder.setMaxVersions(5); //添加最大版本数
    99. tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build()); //创建添加完参数的列族描述
    100. }
    101. try {
    102. admin.createTable(tableDescriptorBuilder.build());
    103. }catch (IOException e)
    104. {
    105. e.printStackTrace();
    106. }
    107. admin.close();
    108. }
    109. /**
    110. * 查看所有表格
    111. * @throws IOException
    112. */
    113. public static void listTableNames() throws IOException {
    114. Admin admin = connection.getAdmin();
    115. try {
    116. TableName[] tableNames = admin.listTableNames();
    117. System.out.println("所有表格如下");
    118. for(TableName tableName : tableNames)
    119. {
    120. System.out.println(tableName.getNamespaceAsString() + ":" + tableName.getNameAsString());
    121. }
    122. }catch (IOException e)
    123. {
    124. e.printStackTrace();
    125. }
    126. admin.close();
    127. }
    128. /**
    129. * 修改表格中某一个列族的版本号
    130. * @param namespace
    131. * @param table
    132. * @param columnFamily 列族
    133. * @param version 版本号
    134. */
    135. public static void modifyTable(String namespace , String table , String columnFamily , int version) throws IOException {
    136. if(!isTableExist(namespace,table))
    137. {
    138. System.out.println("表格不存在,无法修改");
    139. return;
    140. }
    141. Admin admin = connection.getAdmin();
    142. try {
    143. //获取表格描述
    144. TableDescriptor descriptor = admin.getDescriptor(TableName.valueOf(namespace, table));
    145. //创建新的表格描述构建器
    146. TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(descriptor);
    147. //获取列族描述
    148. ColumnFamilyDescriptor columnFamily1 = descriptor.getColumnFamily(Bytes.toBytes(columnFamily));
    149. //创建新的列族描述构建器
    150. ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(columnFamily1);
    151. //设置参数
    152. columnFamilyDescriptorBuilder.setMaxVersions(version);
    153. //将新的列族描述添加到表格描述中
    154. tableDescriptorBuilder.modifyColumnFamily(columnFamilyDescriptorBuilder.build());
    155. admin.modifyTable(tableDescriptorBuilder.build());
    156. }catch (IOException e)
    157. {
    158. e.printStackTrace();
    159. }
    160. admin.close();
    161. }
    162. /**
    163. *
    164. * @param namespace
    165. * @param table
    166. * @return true表示删除成功
    167. */
    168. public static boolean deleteTable(String namespace , String table) throws IOException {
    169. if(!isTableExist(namespace,table))
    170. {
    171. System.out.println("表格不存在,无法删除");
    172. return false;
    173. }
    174. Admin admin = connection.getAdmin();
    175. try {
    176. TableName tableName = TableName.valueOf(namespace, table);
    177. admin.disableTable(tableName);
    178. admin.deleteTable(tableName);
    179. }catch (IOException e)
    180. {
    181. e.printStackTrace();
    182. }
    183. admin.close();
    184. return true;
    185. }
    186. }

    DML操作

    1. package com.why;
    2. import org.apache.hadoop.hbase.Cell;
    3. import org.apache.hadoop.hbase.CellUtil;
    4. import org.apache.hadoop.hbase.CompareOperator;
    5. import org.apache.hadoop.hbase.TableName;
    6. import org.apache.hadoop.hbase.client.*;
    7. import org.apache.hadoop.hbase.filter.ColumnValueFilter;
    8. import org.apache.hadoop.hbase.filter.FilterList;
    9. import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
    10. import org.apache.hadoop.hbase.util.Bytes;
    11. import java.io.IOException;
    12. public class HBaseDML {
    13. private static Connection connection = HBaseConnect2.connection;
    14. /**
    15. * 插入数据
    16. *
    17. * @param namespace
    18. * @param table
    19. * @param rowKey
    20. * @param columnFamily
    21. * @param column
    22. * @param value
    23. * @throws IOException
    24. */
    25. public static void insert(String namespace, String table, String rowKey, String columnFamily, String column, String value) throws IOException {
    26. //首先获取表格table
    27. Table table1 = connection.getTable(TableName.valueOf(namespace, table));
    28. System.out.println("表格创建成功");
    29. //创建put对象
    30. Put put = new Put(Bytes.toBytes(rowKey));
    31. System.out.println("put对象创建成功");
    32. //向put对象中添加数据
    33. put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
    34. try {
    35. table1.put(put);
    36. System.out.println("数据插入成功");
    37. } catch (IOException e) {
    38. e.printStackTrace();
    39. }
    40. table1.close();
    41. }
    42. /**
    43. * 读取数据
    44. *
    45. * @param namespace
    46. * @param table
    47. * @param rowKey
    48. * @param columnFamily
    49. * @param column
    50. * @throws IOException
    51. */
    52. public static void get(String namespace, String table, String rowKey, String columnFamily, String column) throws IOException {
    53. //获取table
    54. Table table1 = connection.getTable(TableName.valueOf(namespace, table));
    55. //获取get对象
    56. Get get = new Get(Bytes.toBytes(rowKey));
    57. //设置读取某一列的数据(如果不设置的话则读取所有数据)
    58. get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
    59. try {
    60. //调用get方法读取数据
    61. Result result = table1.get(get);
    62. //获取到所有的cell
    63. Cell[] cells = result.rawCells();
    64. //打印value
    65. for (Cell cell : cells) {
    66. String value = new String(CellUtil.cloneValue(cell));
    67. System.out.println(value);
    68. }
    69. } catch (IOException e) {
    70. e.printStackTrace();
    71. }
    72. table1.close();
    73. }
    74. /**
    75. * 扫描表
    76. *
    77. * @param namespace
    78. * @param table
    79. * @param startRow
    80. * @param stopRow
    81. * @throws IOException
    82. */
    83. public static void scan(String namespace, String table, String startRow, String stopRow) throws IOException {
    84. //获取table
    85. Table table1 = connection.getTable(TableName.valueOf(namespace, table));
    86. //创建scan对象
    87. Scan scan = new Scan();
    88. //添加扫描的起止rowKey
    89. scan.withStartRow(Bytes.toBytes(startRow));
    90. scan.withStopRow(Bytes.toBytes(stopRow));
    91. try {
    92. ResultScanner scanner = table1.getScanner(scan);
    93. for (Result result : scanner) {
    94. Cell[] cells = result.rawCells();
    95. for (Cell cell : cells) {
    96. System.out.print(new
    97. String(CellUtil.cloneRow(cell)) + "-" + new
    98. String(CellUtil.cloneFamily(cell)) + "-" + new
    99. String(CellUtil.cloneQualifier(cell)) + "-" + new
    100. String(CellUtil.cloneValue(cell)) + "\t");
    101. }
    102. System.out.println();
    103. ;
    104. }
    105. } catch (IOException e) {
    106. e.printStackTrace();
    107. }
    108. table1.close();
    109. }
    110. /**
    111. * 扫描表(有过滤器)
    112. *
    113. * @param namespace
    114. * @param table
    115. * @param startRow
    116. * @param stopRow
    117. * @param columnFamily
    118. * @param column
    119. * @param value
    120. */
    121. public static void scanWithFilter(String namespace, String table, String startRow, String stopRow, String columnFamily, String column, String value) throws IOException {
    122. //获取table
    123. Table table1 = connection.getTable(TableName.valueOf(namespace, table));
    124. //创建scan对象
    125. Scan scan = new Scan();
    126. //添加扫描的起止rowKey
    127. scan.withStartRow(Bytes.toBytes(startRow));
    128. scan.withStopRow(Bytes.toBytes(stopRow));
    129. //创建过滤器列表
    130. FilterList filterList = new FilterList();
    131. //创建列过滤器,作用:只保留当前列的数据
    132. ColumnValueFilter columnValueFilter = new ColumnValueFilter(
    133. Bytes.toBytes(columnFamily),
    134. Bytes.toBytes(column),
    135. CompareOperator.EQUAL,
    136. Bytes.toBytes(value));
    137. // // (2) 结果保留整行数据
    138. // // 结果同时会保留没有当前列的数据
    139. // SingleColumnValueFilter singleColumnValueFilter = new
    140. // SingleColumnValueFilter(
    141. // // 列族名称
    142. // Bytes.toBytes(columnFamily),
    143. // // 列名
    144. // Bytes.toBytes(column),
    145. // // 比较关系
    146. // CompareOperator.EQUAL,
    147. // // 值
    148. // Bytes.toBytes(value)
    149. // );
    150. filterList.addFilter(columnValueFilter);
    151. scan.setFilter(filterList);
    152. try {
    153. ResultScanner scanner = table1.getScanner(scan);
    154. for (Result result : scanner) {
    155. Cell[] cells = result.rawCells();
    156. for (Cell cell : cells) {
    157. System.out.print(new
    158. String(CellUtil.cloneRow(cell)) + "-" + new
    159. String(CellUtil.cloneFamily(cell)) + "-" + new
    160. String(CellUtil.cloneQualifier(cell)) + "-" + new
    161. String(CellUtil.cloneValue(cell)) + "\t");
    162. }
    163. System.out.println();
    164. ;
    165. }
    166. } catch (IOException e) {
    167. e.printStackTrace();
    168. }
    169. table1.close();
    170. }
    171. /**
    172. * 删除数据
    173. * @param nameSpace
    174. * @param table
    175. * @param rowKey
    176. * @param columnFamily
    177. * @param column
    178. * @throws IOException
    179. */
    180. public static void delete(String nameSpace, String table, String rowKey, String columnFamily, String column) throws IOException {
    181. //获取 table
    182. Table table1 = connection.getTable(TableName.valueOf(nameSpace, table));
    183. //创建 Delete 对象
    184. Delete delete = new Delete(Bytes.toBytes(rowKey));
    185. //添加删除信息
    186. //删除单个版本(默认最新)
    187. delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column));
    188. // //删除所有版本
    189. // delete.addColumns(Bytes.toBytes(columnFamily),Bytes.toBytes(column));
    190. //删除列族
    191. delete.addFamily(Bytes.toBytes(columnFamily));
    192. try {
    193. table1.delete(delete);
    194. }catch (IOException e)
    195. {
    196. e.printStackTrace();
    197. }
    198. table1.close();
    199. }
    200. }

    说明:本学习笔记根据基于尚硅谷课程进行整理,课程链接:hbase

    未完待续~

  • 相关阅读:
    算法笔记-第十章-图的遍历(未处理完-11.22日)
    Asp .Net Core 系列:Asp .Net Core 集成 NLog
    Python数据可视化-----制作全球地震散点图
    java计算机毕业设计Web端校园报修系统MyBatis+系统+LW文档+源码+调试部署
    蓝河操作系统--概述
    重走JAVA之类与对象相关的
    王者荣耀安卓区修改荣耀战区方法 | 最低战力查询(附带视频与安装包)
    Android 应用流量监控实践
    springcloud13:服务网关(gateway)
    什么是泛型?
  • 原文地址:https://blog.csdn.net/qq_51235856/article/details/134313651