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