当前位置:   article > 正文

java连接Hbase操作数据库的全过程---java api操作Hbase数据库_java17中如何hbase数据库

java17中如何hbase数据库

要看hadoop和hbase搭建过程的通过传送门过去看:

hadoop:https://blog.csdn.net/qq1049545450/article/details/90019159

hbase:https://blog.csdn.net/qq1049545450/article/details/90023386

在使用java操作Hbase数据库之前,先进行以下配置:

对本地的hosts(C:\Windows\System32\drivers\etc\hosts)进行配置,在hosts文件后面加上:
192.168.5.133 hbasehost-server hbasehost

项目中引入hbase客户端的maven依赖,版本最好以hbase的版本保持一致:

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

将服务器上的hadoop和hbase的几个配置文件拷贝至resources目录下:

开始使用api操作hbase:

api参考:Apache HBase 2.1.9 APIHBase的Java API操作-腾讯云开发者社区-腾讯云

直接上代码:

  1. public class HBaseUtil {
  2. /**
  3. <br>功能描述: 初始化配置
  4. <br>处理逻辑:
  5. <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 11:45
  6. <br>修改记录: {修改人 修改原因 修改时间}
  7. * @param
  8. * @throws
  9. * @return org.apache.hadoop.conf.Configuration
  10. * @see #
  11. */
  12. public static Configuration initConfig(){
  13. Configuration config = new Configuration();
  14. //记住 写服务名
  15. config.set("hbase.zookeeper.quorum","hbasehost");
  16. return config;
  17. }
  18. /**
  19. <br>功能描述: 获取数据库管理员对象
  20. <br>处理逻辑:
  21. <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 11:46
  22. <br>修改记录: {修改人 修改原因 修改时间}
  23. * @throws
  24. * @return org.apache.hadoop.hbase.client.Admin
  25. * @see #
  26. */
  27. public static Admin getAdmin() throws IOException {
  28. Configuration conf = initConfig();
  29. Connection connection = ConnectionFactory.createConnection(conf);
  30. Admin admin = connection.getAdmin();
  31. return admin;
  32. }
  33. /**
  34. <br>功能描述: 检查hbase是否可用
  35. <br>处理逻辑:
  36. <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 12:30
  37. <br>修改记录: {修改人 修改原因 修改时间}
  38. * @param
  39. * @throws
  40. * @return void
  41. * @see #
  42. */
  43. public static void isAvailable() throws IOException {
  44. Configuration conf = initConfig();
  45. HBaseAdmin.available(conf);
  46. }
  47. /**
  48. <br>功能描述: 获取表名集合
  49. <br>处理逻辑:
  50. <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 12:34
  51. <br>修改记录: {修改人 修改原因 修改时间}
  52. * @param
  53. * @throws
  54. * @return java.util.List<java.lang.String>
  55. * @see #
  56. */
  57. public static List<String> getTables() throws IOException {
  58. Admin admin = getAdmin();
  59. TableName[] tableNames = admin.listTableNames();
  60. List<String> tables = new ArrayList<String>();
  61. for(TableName tableName:tableNames){
  62. tables.add(tableName.getNameAsString());
  63. }
  64. return tables;
  65. }
  66. /**
  67. <br>功能描述: 创建表
  68. <br>处理逻辑:
  69. <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 15:09
  70. <br>修改记录: {修改人 修改原因 修改时间}
  71. * @param tableName
  72. * @param cols
  73. * @throws
  74. * @return void
  75. * @see #
  76. */
  77. public static void createTable(String tableName,List<String> cols) throws IOException {
  78. TableName tn = TableName.valueOf(tableName);
  79. HBaseAdmin hBaseAdmin = (HBaseAdmin) getAdmin();
  80. if (hBaseAdmin.tableExists(tn)) {
  81. System.out.println("talbe is exists!");
  82. } else {
  83. TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(tn);
  84. for (String col : cols) {
  85. ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.of(col);
  86. tdb.setColumnFamily(cfd);
  87. }
  88. hBaseAdmin.createTable(tdb.build());
  89. }
  90. hBaseAdmin.close();
  91. }
  92. /**
  93. <br>功能描述: 往表里面插值
  94. <br>处理逻辑:
  95. <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 15:33
  96. <br>修改记录: {修改人 修改原因 修改时间}
  97. * @param tableName
  98. * @param rowKey
  99. * @param columnFamilys
  100. * @param colValue
  101. * @throws
  102. * @return void
  103. * @see #
  104. */
  105. public static void put(String tableName,String rowKey,String columnFamilys,Map<String,Object> colValue) throws IOException {
  106. Connection conn = getAdmin().getConnection();
  107. Table table = conn.getTable(TableName.valueOf(tableName));
  108. Put put = new Put(Bytes.toBytes(rowKey));
  109. for(String col:colValue.keySet()){
  110. put.addColumn(Bytes.toBytes(columnFamilys), Bytes.toBytes(col), ByteArrayUtils.objectToBytes(colValue.get(col)).get());
  111. }
  112. table.put(put);
  113. table.close();
  114. }
  115. /**
  116. <br>功能描述: 删除某行数据
  117. <br>处理逻辑:
  118. <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 15:35
  119. <br>修改记录: {修改人 修改原因 修改时间}
  120. * @param tableName
  121. * @param rowKey
  122. * @throws
  123. * @return void
  124. * @see #
  125. */
  126. public static void delete(String tableName,String rowKey) throws IOException {
  127. Connection conn = getAdmin().getConnection();
  128. Table table = conn.getTable(TableName.valueOf(tableName));
  129. Delete delete = new Delete(Bytes.toBytes(rowKey));
  130. table.delete(delete);
  131. table.close();
  132. }
  133. /**
  134. <br>功能描述: 获取行
  135. <br>处理逻辑:
  136. <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 17:07
  137. <br>修改记录: {修改人 修改原因 修改时间}
  138. * @param tableName
  139. * @param rowKey
  140. * @throws
  141. * @return java.util.List<com.alibaba.fastjson.JSONObject>
  142. * @see #
  143. */
  144. public static List<JSONObject> get(String tableName, String rowKey) throws IOException {
  145. return get(tableName,rowKey,null, (List<String>) null);
  146. }
  147. /**
  148. <br>功能描述: 获取行的具体单元格数据
  149. <br>处理逻辑:
  150. <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 17:07
  151. <br>修改记录: {修改人 修改原因 修改时间}
  152. * @param tableName
  153. * @param rowKey
  154. * @param columnFamilys
  155. * @param col
  156. * @throws
  157. * @return com.alibaba.fastjson.JSONObject
  158. * @see #
  159. */
  160. public static JSONObject get(String tableName,String rowKey,String columnFamilys,String col) throws IOException {
  161. List<String> cols = new ArrayList<>();
  162. cols.add(col);
  163. List<JSONObject> objs = get(tableName,rowKey,columnFamilys,cols);
  164. if(objs!=null&&objs.size()>0){
  165. return objs.get(0);
  166. }
  167. return null;
  168. }
  169. /**
  170. <br>功能描述: 获取行指定列簇的多个单元格数据
  171. <br>处理逻辑:
  172. <br>作者: lwl liuwanli_eamil@163.com 2019/5/9 17:08
  173. <br>修改记录: {修改人 修改原因 修改时间}
  174. * @param tableName
  175. * @param rowKey
  176. * @param columnFamilys
  177. * @param cols
  178. * @throws
  179. * @return java.util.List<com.alibaba.fastjson.JSONObject>
  180. * @see #
  181. */
  182. public static List<JSONObject> get(String tableName,String rowKey,String columnFamilys,List<String> cols) throws IOException {
  183. Connection conn = getAdmin().getConnection();
  184. Table table = conn.getTable(TableName.valueOf(tableName));
  185. Get get = new Get(Bytes.toBytes(rowKey));
  186. if(cols!=null){
  187. for(String col:cols){
  188. get.addColumn(Bytes.toBytes(columnFamilys),Bytes.toBytes(col));
  189. }
  190. }
  191. Result result = table.get(get);
  192. Cell[] cells = result.rawCells();
  193. List<JSONObject> objs = new ArrayList<>();
  194. for(Cell cell:cells){
  195. JSONObject obj = new JSONObject();
  196. obj.put("columnFamilys",Bytes.toString(CellUtil.cloneFamily(cell)));
  197. obj.put("qualifie",Bytes.toString(CellUtil.cloneQualifier(cell)));
  198. obj.put("value",ByteArrayUtils.bytesToObject(CellUtil.cloneValue(cell)).get());
  199. objs.add(obj);
  200. }
  201. return objs;
  202. }
  203. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/754098
推荐阅读
相关标签
  

闽ICP备14008679号