|
17 | 17 | package com.navercorp.pinpoint.common.hbase;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.util.Objects; |
20 | 21 |
|
21 |
| -import org.apache.hadoop.conf.Configuration; |
22 | 22 | import org.apache.hadoop.hbase.HTableDescriptor;
|
23 | 23 | import org.apache.hadoop.hbase.TableName;
|
24 | 24 | import org.apache.hadoop.hbase.client.Admin;
|
25 |
| -import org.apache.hadoop.hbase.client.Connection; |
26 |
| -import org.apache.hadoop.hbase.client.ConnectionFactory; |
27 | 25 |
|
28 | 26 | /**
|
29 | 27 | * @author emeroad
|
| 28 | + * @author HyunGil Jeong |
30 | 29 | */
|
31 | 30 | public class HBaseAdminTemplate {
|
32 | 31 |
|
33 |
| - private final Admin admin; |
34 |
| - private final Connection connection; |
| 32 | + private final AdminFactory adminFactory; |
35 | 33 |
|
36 |
| - public HBaseAdminTemplate(Configuration configuration) { |
37 |
| - try { |
38 |
| - connection = ConnectionFactory.createConnection(configuration); |
39 |
| - admin = connection.getAdmin(); |
40 |
| - } catch (Exception e) { |
41 |
| - throw new HbaseSystemException(e); |
42 |
| - } |
| 34 | + public HBaseAdminTemplate(AdminFactory adminFactory) { |
| 35 | + this.adminFactory = Objects.requireNonNull(adminFactory, "adminFactory must not be null"); |
43 | 36 | }
|
44 | 37 |
|
45 | 38 | public boolean createTableIfNotExist(HTableDescriptor htd) {
|
| 39 | + Admin admin = adminFactory.getAdmin(); |
46 | 40 | try {
|
47 |
| - if (!admin.tableExists(htd.getTableName())) { |
48 |
| - this.admin.createTable(htd); |
| 41 | + TableName tableName = htd.getTableName(); |
| 42 | + if (!admin.tableExists(tableName)) { |
| 43 | + admin.createTable(htd); |
49 | 44 | return true;
|
50 | 45 | }
|
51 | 46 | return false;
|
52 | 47 | } catch (IOException e) {
|
53 | 48 | throw new HbaseSystemException(e);
|
| 49 | + } finally { |
| 50 | + adminFactory.releaseAdmin(admin); |
54 | 51 | }
|
55 | 52 | }
|
56 | 53 |
|
57 | 54 | public boolean tableExists(TableName tableName) {
|
| 55 | + Admin admin = adminFactory.getAdmin(); |
58 | 56 | try {
|
59 | 57 | return admin.tableExists(tableName);
|
60 | 58 | } catch (IOException e) {
|
61 | 59 | throw new HbaseSystemException(e);
|
| 60 | + } finally { |
| 61 | + adminFactory.releaseAdmin(admin); |
62 | 62 | }
|
63 | 63 | }
|
64 | 64 |
|
65 | 65 | public boolean dropTableIfExist(TableName tableName) {
|
| 66 | + Admin admin = adminFactory.getAdmin(); |
66 | 67 | try {
|
67 | 68 | if (admin.tableExists(tableName)) {
|
68 |
| - this.admin.disableTable(tableName); |
69 |
| - this.admin.deleteTable(tableName); |
| 69 | + admin.disableTable(tableName); |
| 70 | + admin.deleteTable(tableName); |
70 | 71 | return true;
|
71 | 72 | }
|
72 | 73 | return false;
|
73 | 74 | } catch (IOException e) {
|
74 | 75 | throw new HbaseSystemException(e);
|
| 76 | + } finally { |
| 77 | + adminFactory.releaseAdmin(admin); |
75 | 78 | }
|
76 | 79 | }
|
77 | 80 |
|
78 | 81 | public void dropTable(TableName tableName) {
|
| 82 | + Admin admin = adminFactory.getAdmin(); |
79 | 83 | try {
|
80 |
| - this.admin.disableTable(tableName); |
81 |
| - this.admin.deleteTable(tableName); |
82 |
| - } catch (IOException e) { |
83 |
| - throw new HbaseSystemException(e); |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - public void close() { |
88 |
| - try { |
89 |
| - this.admin.close(); |
90 |
| - this.connection.close(); |
| 84 | + admin.disableTable(tableName); |
| 85 | + admin.deleteTable(tableName); |
91 | 86 | } catch (IOException e) {
|
92 | 87 | throw new HbaseSystemException(e);
|
| 88 | + } finally { |
| 89 | + adminFactory.releaseAdmin(admin); |
93 | 90 | }
|
94 | 91 | }
|
95 | 92 | }
|
0 commit comments