TableBundle.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. private Row getRow(String rowKey) {
  31. Row row = rows.get(rowKey);
  32. if (row == null) {
  33. row = new Row();
  34. rows.put(rowKey, row);
  35. }
  36. return row;
  37. }
  38. public void addFamily(String rowKey, Object family) {
  39. Row row = getRow(rowKey);
  40. row.addFamily(family.toString());
  41. }
  42. public void addColumns(String rowKey, Object family, String[] columns) {
  43. Row row = getRow(rowKey);
  44. row.addColumns(family.toString(), columns);
  45. }
  46. public void addValues(String rowKey, Object family, Map<String, String> values) {
  47. Row row = getRow(rowKey);
  48. row.addValues(family.toString(), values);
  49. }
  50. public void clear() {
  51. rows.clear();
  52. }
  53. public List<Get> getOperations() {
  54. List<Get> gets = new ArrayList<>(rows.size());
  55. for (String rowKey : rows.keySet()) {
  56. Get get = new Get(Bytes.toBytes(rowKey));
  57. Row row = rows.get(rowKey);
  58. if (row != null) {
  59. for (String family : row.getFamilies()) {
  60. Set<Object> columns = row.getCells(family);
  61. if (CollectionUtils.isEmpty(columns)) {
  62. get.addFamily(Bytes.toBytes(family));
  63. }
  64. for (Object column : columns) {
  65. get.addColumn(Bytes.toBytes(family), Bytes.toBytes((String) column));
  66. }
  67. }
  68. }
  69. gets.add(get);
  70. }
  71. return gets;
  72. }
  73. public List<Put> putOperations() {
  74. List<Put> puts = new ArrayList<>(rows.values().size());
  75. for (String rowKey : rows.keySet()) {
  76. Put put = new Put(Bytes.toBytes(rowKey));
  77. Row row = rows.get(rowKey);
  78. for (String family : row.getFamilies()) {
  79. Set<Object> columns = row.getCells(family);
  80. for (Object column : columns) {
  81. Pair<String, String> pair = (Pair<String, String>) column;
  82. if (StringUtils.isNotEmpty(pair.getRight())) {
  83. put.addColumn(Bytes.toBytes(family),
  84. Bytes.toBytes(pair.getLeft()),
  85. Bytes.toBytes(pair.getRight()));
  86. }
  87. }
  88. }
  89. puts.add(put);
  90. }
  91. return puts;
  92. }
  93. public List<Delete> deleteOperations() {
  94. List<Delete> deletes = new ArrayList<>(rows.values().size());
  95. for (String rowkey : rows.keySet()) {
  96. Delete delete = new Delete(Bytes.toBytes(rowkey));
  97. deletes.add(delete);
  98. }
  99. return deletes;
  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. }