TableBundle.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.yihu.ehr.hbase;
  2. import org.apache.commons.collections.CollectionUtils;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.apache.commons.lang3.tuple.ImmutablePair;
  5. import org.apache.commons.lang3.tuple.Pair;
  6. import org.apache.hadoop.hbase.client.Delete;
  7. import org.apache.hadoop.hbase.client.Get;
  8. import org.apache.hadoop.hbase.client.Put;
  9. import org.apache.hadoop.hbase.util.Bytes;
  10. import java.util.*;
  11. import java.util.stream.Collectors;
  12. /**
  13. * 将HBase中的行,列族,列捆绑成一束。并一次性生成所需要的Get, Put操作。
  14. * <p>
  15. * 仅支持单表操作。
  16. * <p>
  17. * 虽然支持多种HBase操作,但请注意,一次只能用于一种操作,如:Get,Put,Delete不能混用,
  18. * 否则将出现难以预料的后果。
  19. *
  20. * @author Sand
  21. * @created 2016.04.27 14:38
  22. */
  23. public class TableBundle {
  24. Map<String, Row> rows = new HashMap<>();
  25. public void addRows(String... rowkeys) {
  26. for (String rowkey : rowkeys) {
  27. rows.put(rowkey, null);
  28. }
  29. }
  30. public void addFamily(String rowkey, Object family) {
  31. Row row = getRow(rowkey);
  32. row.addFamily(family.toString());
  33. }
  34. public void addColumns(String rowkey, Object family, String[] columns) {
  35. Row row = getRow(rowkey);
  36. row.addColumns(family.toString(), columns);
  37. }
  38. public void addValues(String rowkey, Object family, Map<String, String> values) {
  39. Row row = getRow(rowkey);
  40. row.addValues(family.toString(), values);
  41. }
  42. public void clear() {
  43. rows.clear();
  44. }
  45. public List<Get> getOperations() {
  46. List<Get> gets = new ArrayList<>(rows.size());
  47. for (String rowkey : rows.keySet()) {
  48. Get get = new Get(Bytes.toBytes(rowkey));
  49. Row row = rows.get(rowkey);
  50. if (row != null) {
  51. for (String family : row.getFamilies()) {
  52. Set<Object> columns = row.getCells(family);
  53. if (CollectionUtils.isEmpty(columns)) {
  54. get.addFamily(Bytes.toBytes(family));
  55. }
  56. for (Object column : columns) {
  57. get.addColumn(Bytes.toBytes(family), Bytes.toBytes((String) column));
  58. }
  59. }
  60. }
  61. gets.add(get);
  62. }
  63. return gets;
  64. }
  65. public List<Put> putOperations() {
  66. List<Put> puts = new ArrayList<>(rows.values().size());
  67. for (String rowkey : rows.keySet()) {
  68. Put put = new Put(Bytes.toBytes(rowkey));
  69. Row row = rows.get(rowkey);
  70. for (String family : row.getFamilies()) {
  71. Set<Object> columns = row.getCells(family);
  72. for (Object column : columns) {
  73. Pair<String, String> pair = (Pair<String, String>) column;
  74. if (StringUtils.isNotEmpty(pair.getRight())) {
  75. put.addColumn(Bytes.toBytes(family),
  76. Bytes.toBytes(pair.getLeft()),
  77. Bytes.toBytes(pair.getRight()));
  78. }
  79. }
  80. }
  81. puts.add(put);
  82. }
  83. return puts;
  84. }
  85. public List<Delete> deleteOperations() {
  86. List<Delete> deletes = new ArrayList<>(rows.values().size());
  87. for (String rowkey : rows.keySet()) {
  88. Delete delete = new Delete(Bytes.toBytes(rowkey));
  89. deletes.add(delete);
  90. }
  91. return deletes;
  92. }
  93. private Row getRow(String rowkey) {
  94. Row row = rows.get(rowkey);
  95. if (row == null) {
  96. row = new Row();
  97. rows.put(rowkey, row);
  98. }
  99. return row;
  100. }
  101. /**
  102. * HBase中的一行
  103. */
  104. public static class Row {
  105. private Map<String, Set<Object>> cells = new HashMap<>(); // key为family,value为columns
  106. public void addFamily(String family) {
  107. cells.put(family, null);
  108. }
  109. public void addColumns(String family, String... columns) {
  110. Set value = getFamily(family);
  111. for (String column : columns) {
  112. value.add(column);
  113. }
  114. }
  115. public void addValues(String family, Map<String, String> values) {
  116. Set value = getFamily(family);
  117. value.addAll(values.keySet().stream().map(key -> new ImmutablePair<>(key, values.get(key))).collect(Collectors.toList()));
  118. }
  119. public Set<String> getFamilies() {
  120. return cells.keySet();
  121. }
  122. public Set<Object> getCells(String family) {
  123. return cells.get(family);
  124. }
  125. private Set<Object> getFamily(String family) {
  126. Set value = cells.get(family);
  127. if (value == null) {
  128. value = new TreeSet<>();
  129. cells.put(family, value);
  130. }
  131. return value;
  132. }
  133. }
  134. }