Hbase常用数据库操作类

pom.xml中引用hbase-client

  1. <dependency>
  2. <groupId>org.apache.hbase</groupId>
  3. <artifactId>hbase-client</artifactId>
  4. <version>2.2.5</version>
  5. </dependency>

HBaseConn.java获取hbase链接

  1. package com.rumenz;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.TableName;
  5. import org.apache.hadoop.hbase.client.Connection;
  6. import org.apache.hadoop.hbase.client.ConnectionFactory;
  7. import org.apache.hadoop.hbase.client.Table;
  8. import java.io.IOException;
  9. public class HBaseConn {
  10. private static final HBaseConn INSTANCE=new HBaseConn();
  11. private static Configuration configuration;
  12. private static Connection connection;
  13. private HBaseConn(){
  14. try{
  15. if(configuration==null){
  16. configuration=HBaseConfiguration.create();
  17. configuration.set("hbase.zookeeper.quorum", "192.168.82.177:2181");
  18. }
  19. }catch (Exception e){
  20. e.printStackTrace();
  21. }
  22. }
  23. private Connection getConnection(){
  24. if(connection==null||connection.isClosed()){
  25. try{
  26. connection=ConnectionFactory.createConnection(configuration);
  27. }catch(Exception e){
  28. e.printStackTrace();
  29. }
  30. }
  31. return connection;
  32. }
  33. public static Connection getHbaseConn(){
  34. return INSTANCE.getConnection();
  35. }
  36. public static Table getTable(String table) throws IOException {
  37. return INSTANCE.getConnection().getTable(TableName.valueOf(table));
  38. }
  39. public static void closeConn(){
  40. if(connection!=null){
  41. try{
  42. connection.close();
  43. }catch(Exception e){
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. }

HBaseUtil.java数据库增删改查操作

  1. package com.rumenz;
  2. import org.apache.hadoop.hbase.HTableDescriptor;
  3. import org.apache.hadoop.hbase.TableName;
  4. import org.apache.hadoop.hbase.client.*;
  5. import org.apache.hadoop.hbase.filter.FilterList;
  6. import org.apache.hadoop.hbase.util.Bytes;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. public class HBaseUtil {
  12. /**
  13. * 创建一个表
  14. * @param tableName 表名
  15. * @param cf 列族名
  16. * @return
  17. * @throws IOException
  18. */
  19. public static boolean createTable(String tableName,String[] cf) throws IOException {
  20. try(HBaseAdmin admin = (HBaseAdmin) HBaseConn.getHbaseConn().getAdmin()){
  21. if(admin.tableExists(TableName.valueOf(tableName))){
  22. return false;
  23. }
  24. List<ColumnFamilyDescriptor> res=new ArrayList<>();
  25. Arrays.stream(cf).forEach(cff->{
  26. res.add(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cff)).build());
  27. });
  28. TableDescriptor hTableDescriptor= TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)).setColumnFamilies(res).build();
  29. admin.createTable(hTableDescriptor);
  30. }catch(Exception e){
  31. e.printStackTrace();
  32. }
  33. return true;
  34. }
  35. /**
  36. * 删除一个表
  37. * @param tableName 表名
  38. * @return
  39. */
  40. public static boolean deleteTable(String tableName){
  41. try(HBaseAdmin admin= (HBaseAdmin) HBaseConn.getHbaseConn().getAdmin()){
  42. admin.disableTable(TableName.valueOf(tableName));
  43. admin.deleteTable(TableName.valueOf(tableName));
  44. }catch(Exception e){
  45. e.printStackTrace();
  46. }
  47. return true;
  48. }
  49. /**
  50. * 插入一条数据
  51. * @param tableName 表名
  52. * @param rowkey 主键
  53. * @param cfName 列族
  54. * @param qualifier 列名
  55. * @param data 数据
  56. * @return
  57. */
  58. public static boolean putRow(String tableName,String rowkey,String cfName,String qualifier,String data){
  59. try(Table table= HBaseConn.getTable(tableName)){
  60. Put put=new Put(Bytes.toBytes(rowkey));
  61. put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data));
  62. table.put(put);
  63. }catch (Exception e){
  64. e.printStackTrace();
  65. }
  66. return true;
  67. }
  68. /**
  69. * 批量添加数据
  70. * @param tableName 表名
  71. * @param puts 数据列表
  72. * @return
  73. */
  74. public static boolean putRows(String tableName,List<Put> puts){
  75. try(Table table= HBaseConn.getTable(tableName)){
  76. table.put(puts);
  77. }catch (Exception e){
  78. e.printStackTrace();
  79. }
  80. return true;
  81. }
  82. /**
  83. * 通过rowkey查询数据
  84. * @param tableName 表名
  85. * @param rowkey rowkey
  86. * @return
  87. * @throws IOException
  88. */
  89. public static Result getRow(String tableName,String rowkey) throws IOException {
  90. try(Table table=HBaseConn.getTable(tableName)){
  91. Get get=new Get(Bytes.toBytes(rowkey));
  92. return table.get(get);
  93. }catch (Exception e){
  94. e.printStackTrace();
  95. }
  96. return null;
  97. }
  98. /**
  99. * 通过scan 查询数据
  100. * @param tableName 表名
  101. * @param startRow 开始的row
  102. * @param endRow 结束的row
  103. * @return
  104. */
  105. public static ResultScanner getScanner(String tableName,String startRow,String endRow){
  106. try(Table table=HBaseConn.getTable(tableName)){
  107. Scan scan=new Scan();
  108. scan.withStartRow(Bytes.toBytes(startRow));
  109. scan.withStopRow(Bytes.toBytes(endRow));
  110. scan.setCaching(1000);
  111. return table.getScanner(scan);
  112. }catch (Exception e){
  113. e.printStackTrace();
  114. }
  115. return null;
  116. }
  117. /**
  118. * 通过过滤器查询数据
  119. * @param tableName 表名
  120. * @param startRow 开始
  121. * @param endRow 结束
  122. * @param flist 过滤器
  123. * @return
  124. */
  125. public static ResultScanner getScanner(String tableName, String startRow, String endRow, FilterList flist){
  126. try(Table table=HBaseConn.getTable(tableName)){
  127. Scan scan=new Scan();
  128. scan.withStartRow(Bytes.toBytes(startRow));
  129. scan.withStopRow(Bytes.toBytes(endRow));
  130. scan.setFilter(flist);
  131. scan.setCaching(1000);
  132. return table.getScanner(scan);
  133. }catch (Exception e){
  134. e.printStackTrace();
  135. }
  136. return null;
  137. }
  138. /**
  139. * 通过rowkey删除数据
  140. * @param tableName 表名
  141. * @param rowKey rowkey
  142. * @return
  143. */
  144. public static boolean deleteRow(String tableName,String rowKey){
  145. try(Table table=HBaseConn.getTable(tableName)){
  146. Delete delete=new Delete(Bytes.toBytes(rowKey));
  147. table.delete(delete);
  148. }catch (Exception e){
  149. e.printStackTrace();
  150. }
  151. return true;
  152. }
  153. /**
  154. * 删除列族
  155. * @param tableName 表名
  156. * @param cfName 列族
  157. * @return
  158. */
  159. public static boolean deleteColumnFamily(String tableName,String cfName){
  160. try(HBaseAdmin admin= (HBaseAdmin) HBaseConn.getHbaseConn().getAdmin()){
  161. admin.deleteColumnFamily(TableName.valueOf(tableName),Bytes.toBytes(cfName));
  162. }catch (Exception e){
  163. e.printStackTrace();
  164. }
  165. return true;
  166. }
  167. /**
  168. * 删除列名
  169. * @param tableName 表名
  170. * @param rowKey rowkey
  171. * @param cfName 列族
  172. * @param qualifier 列名
  173. * @return
  174. */
  175. public static boolean deleteQualifier(String tableName,String rowKey,String cfName,String qualifier){
  176. try(Table table=HBaseConn.getTable(tableName)){
  177. Delete delete=new Delete(Bytes.toBytes(rowKey));
  178. delete.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier));
  179. table.delete(delete);
  180. }catch (Exception e){
  181. e.printStackTrace();
  182. }
  183. return true;
  184. }
  185. }

返回笔记列表
入门小站