HBase常用操作-HBaseUtil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 | package com.zhen.hbase; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.Map.Entry; 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.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.filter.BinaryComparator; import org.apache.hadoop.hbase.filter.CompareFilter; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.PageFilter; import org.apache.hadoop.hbase.filter.QualifierFilter; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.filter.SubstringComparator; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.util.Bytes; import org.apache.log4j.Logger; public class HBaseUtil { //@Value("${hbase.zookeeper.quorum}") private String addr= "HDP1,HDP2,HDP3" ; //@Value("${hbase.zookeeper.property.clientPort}") private String port= "2181" ; Logger logger = Logger.getLogger(getClass()); private static Connection connection; @SuppressWarnings( "static-access" ) public void getConnection(){ Configuration conf = HBaseConfiguration.create(); conf. set ( "hbase.zookeeper.quorum" ,addr); conf. set ( "hbase.zookeeper.property.clientPort" , port); try { this .connection = ConnectionFactory.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } } public HBaseUtil() { getConnection(); } /* * 判断表是否存在 * * * @tableName 表名 */ public boolean isExist(String tableName) throws IOException { TableName table_name = TableName.valueOf(tableName); Admin admin = connection.getAdmin(); boolean exit = admin.tableExists(table_name); admin.close(); return exit; } /** * 单行添加 * @param tableName * @param rowKey * @param family * @param keyValue * @throws IOException */ @SuppressWarnings( "unused" ) private static void addRow(String tableName, String rowKey, String family, Map<String, String> keyValue) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowKey)); for (Entry<String, String> entry : keyValue.entrySet()) { put.addColumn(Bytes.toBytes(family), Bytes.toBytes(entry.getKey()), Bytes.toBytes(entry.getValue())); } table.put(put); table.close(); keyValue.clear(); } public static void addRows(String tableName, String rowFamilySeparator, Map<String, Map<String, String>> keyValues) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); List<Put> puts = new ArrayList<Put>(); for (Entry<String, Map<String, String>> entry : keyValues.entrySet()) { String key = entry.getKey(); if ( null == rowFamilySeparator || rowFamilySeparator.isEmpty()) { rowFamilySeparator = "_" ; } String rowKey = key.split(rowFamilySeparator)[0]; String family = key.split(rowFamilySeparator)[1]; Map<String, String> keyValue = entry.getValue(); Put put = new Put(Bytes.toBytes(rowKey), System.currentTimeMillis()); for (Entry<String, String> entry2 : keyValue.entrySet()) { put.addColumn(Bytes.toBytes(family), Bytes.toBytes(entry2.getKey()), Bytes.toBytes(entry2.getValue())); } puts.add(put); } table.put(puts); table.close(); keyValues.clear(); } /** * 单行删除 * @param tableName * @param rowKey * @param family * @throws IOException */ public static void deleteByRowKey(String tableName, String rowKey) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowKey)); table.delete(delete); table.close(); } /** * 查询所有 * @param tableName * @param family * @return * @throws IOException */ public static List<Map<String, String>> queryForScan(String tableName, String family) throws IOException { List<Map<String, String>> rows = new ArrayList<Map<String, String>>(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); scan.addFamily(Bytes.toBytes(family)); ResultScanner rs = table.getScanner(scan); Map<String, String> row = null ; try { for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); row = new HashMap<String, String>(); for (Cell cell : cells) { row.put( new String(CellUtil.cloneQualifier(cell)), new String(CellUtil.cloneValue(cell))); } rows.add(row); } } finally { rs.close(); } return rows; } /** * 查询所有rowKey * @param tableName * @return * @throws IOException */ public static List<String> queryAllRowKeyForScan(String tableName) throws IOException { List<String> result = new ArrayList<String>(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner rs = table.getScanner(scan); try { for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); if ( null == cells || cells.length <= 0) { continue ; } Cell cell = cells[0]; String rowKey = new String(CellUtil.cloneRow(cell)); result.add(rowKey); } } finally { rs.close(); } return result; } /** * 查询所有字段 * @param tableName * @return * @throws IOException */ public static List<Map<String, String>> queryForScan(String tableName) throws IOException { List<Map<String, String>> rows = new ArrayList<Map<String, String>>(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner rs = table.getScanner(scan); Map<String, String> row = null ; try { for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); row = new HashMap<String, String>(); for (Cell cell : cells) { row.put( "rowKey" , new String(CellUtil.cloneRow(cell))); row.put( "family" , new String(CellUtil.cloneFamily(cell))); row.put( new String(CellUtil.cloneQualifier(cell)), new String(CellUtil.cloneValue(cell))); } rows.add(row); } } finally { rs.close(); } return rows; } /** * 根据时间范围 * @param tableName * @param family * @param minStamp * @param maxStamp * @return * @throws IOException */ public static List<Map<String, String>> queryForTimeRange(String tableName, String family, long minStamp, long maxStamp) throws IOException { List<Map<String, String>> rows = new ArrayList<Map<String, String>>(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); scan.addFamily(Bytes.toBytes(family)); scan.setTimeRange(minStamp, maxStamp); ResultScanner rs = table.getScanner(scan); Map<String, String> row = null ; try { for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); row = new HashMap<String, String>(); for (Cell cell : cells) { row.put( new String(CellUtil.cloneQualifier(cell)), new String(CellUtil.cloneValue(cell))); } rows.add(row); } } finally { rs.close(); } return rows; } /** * 根据RowKey查询 * @param tableName * @param rowKey * @param family * @return * @throws IOException */ public static Map<String, String> queryForRowKey(String tableName, String rowKey, String family) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); get .addFamily(Bytes.toBytes(family)); Scan scan = new Scan( get ); ResultScanner rs = table.getScanner(scan); Map<String, String> row = null ; try { for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); row = new HashMap<String, String>(); for (Cell cell : cells) { row.put( new String(CellUtil.cloneQualifier(cell)), new String(CellUtil.cloneValue(cell), "UTF-8" )); } } } finally { rs.close(); } return row; } /** * 根据多个RowKey查询 * @param tableName * @param rowKeys * @param family * @return * @throws IOException */ public static List<Map<String, String>> queryForRowKeys(String tableName, List<String> rowKeys, String family) throws IOException { List<Map<String, String>> resultList = new ArrayList<Map<String, String>>(); Table table = connection.getTable(TableName.valueOf(tableName)); List<Get> getList = new ArrayList(); for (String rowKey : rowKeys) { Get get = new Get(Bytes.toBytes(rowKey)); get .addFamily(Bytes.toBytes(family)); getList.add( get ); } Result[] results = table. get (getList); for (Result result : results){ //对返回的结果集进行操作 Map<String, String> row = new HashMap<String, String>(); for (Cell kv : result.rawCells()) { row.put( new String(CellUtil.cloneQualifier(kv)), new String(CellUtil.cloneValue(kv), "UTF-8" )); } resultList.add(row); } return resultList; } /** * 根据RowKey范围查询 * @param tableName * @param family * @param startRow * @param stopRow * @return * @throws IOException */ public static List<Map<String, String>> queryForRowKeyRange(String tableName, String family, String startRow, String stopRow) throws IOException { List<Map<String, String>> rows = new ArrayList<Map<String, String>>(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); scan.addFamily(Bytes.toBytes(family)); scan.setStartRow(Bytes.toBytes(startRow)); scan.setStopRow(Bytes.toBytes(stopRow)); ResultScanner rs = table.getScanner(scan); Map<String, String> row = null ; try { for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); row = new HashMap<String, String>(); for (Cell cell : cells) { row.put( "timestamp" , cell.getTimestamp() + "" ); row.put( "rowKey" , new String(CellUtil.cloneRow(cell))); row.put( "family" , new String(CellUtil.cloneFamily(cell))); row.put( new String(CellUtil.cloneQualifier(cell)), new String(CellUtil.cloneValue(cell))); } rows.add(row); } } finally { rs.close(); } return rows; } /** * 根据指定列名匹配列值 * @param tableName * @param family * @param qualifier * @param value * @return * @throws IOException */ public static Collection<Map<String, String>> queryForQuilfier(String tableName, String family, String column, String value) throws IOException { Map<String, Map<String, String>> rows = new HashMap<String, Map<String, String>>(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); SubstringComparator comp = new SubstringComparator(value); SingleColumnValueFilter filter = new SingleColumnValueFilter(family.getBytes(), column.getBytes(), CompareOp.EQUAL, comp); filter.setFilterIfMissing( true ); PageFilter p = new PageFilter(5); scan.setFilter(filter); scan.setFilter(p); ResultScanner rs = table.getScanner(scan); Map<String, String> row = null ; try { for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); for (Cell cell : cells) { String rowKey = new String(CellUtil.cloneRow(cell)); if ( null == row || !rows.containsKey(rowKey)) { row = new HashMap<String, String>(); } row.put( "timestamp" , cell.getTimestamp() + "" ); row.put( "rowKey" , rowKey); row.put( "family" , new String(CellUtil.cloneFamily(cell))); row.put( new String(CellUtil.cloneQualifier(cell)), new String(CellUtil.cloneValue(cell), "UTF-8" )); rows.put(rowKey,row); } } } finally { rs.close(); } return rows.values(); } /** * 根据指定列名完全匹配列值 * @param tableName * @param family * @param qualifier * @param value * @return * @throws IOException */ public static Collection<Map<String, String>> queryForQuilfierExactly(String tableName, String family, String column, String value) throws IOException { Map<String, Map<String, String>> rows = new HashMap<String, Map<String, String>>(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); SingleColumnValueFilter filter = new SingleColumnValueFilter(family.getBytes(), column.getBytes(), CompareOp.EQUAL,value.getBytes()); filter.setFilterIfMissing( true ); scan.setFilter(filter); ResultScanner rs = table.getScanner(scan); Map<String, String> row = null ; try { for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); for (Cell cell : cells) { String rowKey = new String(CellUtil.cloneRow(cell)); if ( null == row || !rows.containsKey(rowKey)) { row = new HashMap<String, String>(); } row.put( "timestamp" , cell.getTimestamp() + "" ); row.put( "rowKey" , rowKey); row.put( "family" , new String(CellUtil.cloneFamily(cell))); row.put( new String(CellUtil.cloneQualifier(cell)), new String(CellUtil.cloneValue(cell), "UTF-8" )); rows.put(rowKey,row); } } } finally { rs.close(); } return rows.values(); } /** * 获取列名匹配的rowkey * @param tableName * @param qualifier 列名 * @param pageSize 数量 * @return * @throws IOException */ public static List<String> queryForQuilfierName(String tableName, String qualifier, long pageSize) throws IOException { Set<String> rowKeys = new HashSet<String>(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); Filter filter = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(qualifier))); PageFilter pageFilter = new PageFilter(pageSize); FilterList filterList = new FilterList(); filterList.addFilter(filter); filterList.addFilter(pageFilter); scan.setFilter(filterList); ResultScanner rs = table.getScanner(scan); try { for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); for (Cell cell : cells) { String rowKey = new String(CellUtil.cloneRow(cell)); rowKeys.add(rowKey); } } } finally { rs.close(); } List<String> rows = new ArrayList<String>(rowKeys); return rows; } /** * 获取多个列值匹配的rowkey * @param tableName * @param family * @param qualifierMap 列名:列值 * @param pageSize 数量 * @return * @throws IOException */ public static List<String> queryForMultiQuilfierName(String tableName, String family, Map<String, String> qualifierMap, long pageSize) throws IOException { Set<String> rowKeys = new HashSet<String>(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL); for (Entry<String, String> entry : qualifierMap.entrySet()) { SingleColumnValueFilter filter = new SingleColumnValueFilter(family.getBytes(), entry.getKey().getBytes(), CompareOp.EQUAL,entry.getValue().getBytes()); filter.setFilterIfMissing( true ); filterList.addFilter(filter); } PageFilter pageFilter = new PageFilter(pageSize); filterList.addFilter(pageFilter); scan.setFilter(filterList); scan.addFamily(Bytes.toBytes(family)); ResultScanner rs = table.getScanner(scan); try { for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); for (Cell cell : cells) { String rowKey = new String(CellUtil.cloneRow(cell)); rowKeys.add(rowKey); } } } finally { rs.close(); } List<String> rows = new ArrayList<String>(rowKeys); return rows; } public static void qualifierFilter() throws IOException { Table mTable = connection.getTable(TableName.valueOf( "portrait" )); QualifierFilter columnsNameFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator( "basictag-14" .getBytes())); Scan scan = new Scan(); scan.setFilter(columnsNameFilter); ResultScanner rs = mTable.getScanner(scan); for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); for (Cell cell : cells) { System. out .println( "==== " + new String(CellUtil.cloneRow(cell))+ "\t" + new String(CellUtil.cloneFamily(cell))+ "\t" + new String(CellUtil.cloneQualifier(cell))+ "\t" + new String(CellUtil.cloneValue(cell))); } } } /** * 获取列名匹配的rowkey * @param tableName * @param qualifier 列名 * @param pageSize 数量 * @return * @throws IOException */ public static List<String> queryForQuilfierName(String tableName, String family, String qualifier, long pageSize) throws IOException { Set<String> rowKeys = new HashSet<String>(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); Filter qualifierFilter = new QualifierFilter(CompareFilter.CompareOp.EQUAL, new BinaryComparator(qualifier.getBytes())); PageFilter pageFilter = new PageFilter(pageSize); FilterList list = new FilterList(); list.addFilter(pageFilter); list.addFilter(qualifierFilter); scan.addFamily(Bytes.toBytes(family)); scan.setFilter(list); System. out .println(scan.toJSON()); ResultScanner rs = table.getScanner(scan); try { for (Result r = rs.next(); r != null ; r = rs.next()) { Cell[] cells = r.rawCells(); for (Cell cell : cells) { System. out .println( "==== " + new String(CellUtil.cloneRow(cell))+ "\t" + new String(CellUtil.cloneFamily(cell))+ "\t" + new String(CellUtil.cloneQualifier(cell))+ "\t" + new String(CellUtil.cloneValue(cell))); String rowKey = new String(CellUtil.cloneRow(cell)); rowKeys.add(rowKey); } } } finally { rs.close(); } List<String> rows = new ArrayList<String>(rowKeys); return rows; } /** * 根据指定列名匹配列值条数 * @param tableName * @param family * @param qualifier * @param value * @return * @throws IOException */ public static long queryForQuilfierCount(String tableName, String family, String column, String value) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); SubstringComparator comp = new SubstringComparator(value); SingleColumnValueFilter filter = new SingleColumnValueFilter(family.getBytes(), column.getBytes(), CompareOp.EQUAL, comp); filter.setFilterIfMissing( true ); scan.setFilter(filter); ResultScanner rs = table.getScanner(scan); long count = 0; try { for (Result r = rs.next(); r != null ; r = rs.next()) { count++; } } finally { rs.close(); } return count; } /* * 关闭连接 * */ public void close() { /** * close connection **/ if (connection != null ) { try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示