1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.rest.client;
21
22 import java.io.IOException;
23
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.classification.InterfaceStability;
26 import org.apache.hadoop.conf.Configuration;
27
28 import org.apache.hadoop.hbase.HTableDescriptor;
29 import org.apache.hadoop.hbase.rest.Constants;
30 import org.apache.hadoop.hbase.rest.model.TableSchemaModel;
31 import org.apache.hadoop.hbase.util.Bytes;
32
33 @InterfaceAudience.Public
34 @InterfaceStability.Stable
35 public class RemoteAdmin {
36
37 final Client client;
38 final Configuration conf;
39 final String accessToken;
40 final int maxRetries;
41 final long sleepTime;
42
43
44
45
46
47
48 public RemoteAdmin(Client client, Configuration conf) {
49 this(client, conf, null);
50 }
51
52
53
54
55
56
57
58 public RemoteAdmin(Client client, Configuration conf, String accessToken) {
59 this.client = client;
60 this.conf = conf;
61 this.accessToken = accessToken;
62 this.maxRetries = conf.getInt("hbase.rest.client.max.retries", 10);
63 this.sleepTime = conf.getLong("hbase.rest.client.sleep", 1000);
64 }
65
66
67
68
69
70
71 public boolean isTableAvailable(String tableName) throws IOException {
72 return isTableAvailable(Bytes.toBytes(tableName));
73 }
74
75
76
77
78
79
80 public boolean isTableAvailable(byte[] tableName) throws IOException {
81 StringBuilder sb = new StringBuilder();
82 sb.append('/');
83 if (accessToken != null) {
84 sb.append(accessToken);
85 sb.append('/');
86 }
87 sb.append(Bytes.toStringBinary(tableName));
88 sb.append('/');
89 sb.append("exists");
90 int code = 0;
91 for (int i = 0; i < maxRetries; i++) {
92 Response response = client.get(sb.toString());
93 code = response.getCode();
94 switch (code) {
95 case 200:
96 return true;
97 case 404:
98 return false;
99 case 509:
100 try {
101 Thread.sleep(sleepTime);
102 } catch (InterruptedException e) { }
103 break;
104 default:
105 throw new IOException("exists request returned " + code);
106 }
107 }
108 throw new IOException("exists request timed out");
109 }
110
111
112
113
114
115
116 public void createTable(HTableDescriptor desc)
117 throws IOException {
118 TableSchemaModel model = new TableSchemaModel(desc);
119 StringBuilder sb = new StringBuilder();
120 sb.append('/');
121 if (accessToken != null) {
122 sb.append(accessToken);
123 sb.append('/');
124 }
125 sb.append(Bytes.toStringBinary(desc.getName()));
126 sb.append('/');
127 sb.append("schema");
128 int code = 0;
129 for (int i = 0; i < maxRetries; i++) {
130 Response response = client.put(sb.toString(), Constants.MIMETYPE_PROTOBUF,
131 model.createProtobufOutput());
132 code = response.getCode();
133 switch (code) {
134 case 201:
135 return;
136 case 509:
137 try {
138 Thread.sleep(sleepTime);
139 } catch (InterruptedException e) { }
140 break;
141 default:
142 throw new IOException("create request returned " + code);
143 }
144 }
145 throw new IOException("create request timed out");
146 }
147
148
149
150
151
152
153 public void deleteTable(final String tableName) throws IOException {
154 deleteTable(Bytes.toBytes(tableName));
155 }
156
157
158
159
160
161
162 public void deleteTable(final byte [] tableName) throws IOException {
163 StringBuilder sb = new StringBuilder();
164 sb.append('/');
165 if (accessToken != null) {
166 sb.append(accessToken);
167 sb.append('/');
168 }
169 sb.append(Bytes.toStringBinary(tableName));
170 sb.append('/');
171 sb.append("schema");
172 int code = 0;
173 for (int i = 0; i < maxRetries; i++) {
174 Response response = client.delete(sb.toString());
175 code = response.getCode();
176 switch (code) {
177 case 200:
178 return;
179 case 509:
180 try {
181 Thread.sleep(sleepTime);
182 } catch (InterruptedException e) { }
183 break;
184 default:
185 throw new IOException("delete request returned " + code);
186 }
187 }
188 throw new IOException("delete request timed out");
189 }
190
191 }