HBaseAdmin.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.yihu.ehr.hbase;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.fasterxml.jackson.databind.node.ObjectNode;
  4. import com.yihu.ehr.hbase.AbstractHBaseClient;
  5. import org.apache.hadoop.hbase.HColumnDescriptor;
  6. import org.apache.hadoop.hbase.HTableDescriptor;
  7. import org.apache.hadoop.hbase.TableName;
  8. import org.apache.hadoop.hbase.client.Admin;
  9. import org.apache.hadoop.hbase.client.Connection;
  10. import org.apache.hadoop.hbase.client.ConnectionFactory;
  11. import org.apache.hadoop.hbase.client.HTableInterface;
  12. import org.apache.hadoop.hbase.util.Bytes;
  13. import org.springframework.data.hadoop.hbase.TableCallback;
  14. import org.springframework.stereotype.Service;
  15. import java.io.IOException;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. /**
  19. * @author hzp
  20. * @created 2017.05.03
  21. */
  22. @Service
  23. public class HBaseAdmin extends AbstractHBaseClient {
  24. /**
  25. * 判断表是否存在
  26. */
  27. public boolean isTableExists(String tableName) throws Exception {
  28. Connection connection = getConnection();
  29. Admin admin = connection.getAdmin();
  30. boolean ex = admin.tableExists(TableName.valueOf(tableName));
  31. admin.close();
  32. connection.close();
  33. return ex;
  34. }
  35. /**
  36. * 创建表
  37. */
  38. public void createTable(String tableName, String... columnFamilies) throws Exception {
  39. Connection connection = getConnection();
  40. Admin admin = connection.getAdmin();
  41. if (!admin.tableExists(TableName.valueOf(tableName))) {
  42. HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
  43. for (String fc : columnFamilies) {
  44. tableDescriptor.addFamily(new HColumnDescriptor(fc));
  45. }
  46. admin.createTable(tableDescriptor);
  47. }
  48. admin.close();
  49. connection.close();
  50. }
  51. /**
  52. * 模糊匹配表名
  53. */
  54. public List<String> getTableList(String regex, boolean includeSysTables) throws Exception {
  55. Connection connection = getConnection();
  56. Admin admin = connection.getAdmin();
  57. TableName[] tableNames;
  58. if (regex == null || regex.length() == 0) {
  59. tableNames = admin.listTableNames();
  60. } else {
  61. tableNames = admin.listTableNames(regex, includeSysTables);
  62. }
  63. List<String> tables = new ArrayList<>();
  64. for (TableName tableName : tableNames) {
  65. tables.add(tableName.getNameAsString());
  66. }
  67. admin.close();
  68. connection.close();
  69. return tables;
  70. }
  71. /**
  72. * 批量清空表数据
  73. */
  74. public void truncate(List<String> tables) throws Exception {
  75. Connection connection = getConnection();
  76. Admin admin = connection.getAdmin();
  77. try {
  78. for (String tableName : tables) {
  79. TableName tn = TableName.valueOf(tableName);
  80. if (admin.tableExists(TableName.valueOf(tableName))) {
  81. HTableDescriptor descriptor = admin.getTableDescriptor(tn);
  82. admin.disableTable(tn);
  83. admin.deleteTable(tn);
  84. admin.createTable(descriptor);
  85. }
  86. else{
  87. System.out.print("not exit table "+tableName+".\r\n");
  88. }
  89. /*else{
  90. HTableDescriptor descriptor = new HTableDescriptor(tableName);
  91. descriptor.addFamily(new HColumnDescriptor("basic"));
  92. descriptor.addFamily(new HColumnDescriptor("d"));
  93. admin.createTable(descriptor);
  94. }*/
  95. }
  96. } finally {
  97. admin.close();
  98. connection.close();
  99. }
  100. }
  101. /**
  102. * 删除表结构
  103. */
  104. public void dropTable(String tableName) throws Exception {
  105. Connection connection = getConnection();
  106. Admin admin = connection.getAdmin();
  107. try {
  108. admin.disableTable(TableName.valueOf(tableName));
  109. admin.deleteTable(TableName.valueOf(tableName));
  110. } finally {
  111. admin.close();
  112. connection.close();
  113. }
  114. }
  115. public ObjectNode getTableMetaData(String tableName) {
  116. return hbaseTemplate.execute(tableName, new TableCallback<ObjectNode>() {
  117. public ObjectNode doInTable(HTableInterface table) throws Throwable {
  118. ObjectMapper objectMapper = new ObjectMapper();
  119. ObjectNode root = objectMapper.createObjectNode();
  120. HTableDescriptor tableDescriptor = table.getTableDescriptor();
  121. HColumnDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();
  122. for (int i = 0; i < columnDescriptors.length; ++i) {
  123. HColumnDescriptor columnDescriptor = columnDescriptors[i];
  124. root.put(Integer.toString(i), Bytes.toString(columnDescriptor.getName()));
  125. }
  126. return root;
  127. }
  128. });
  129. }
  130. }