Skip to content

Commit dd40fd7

Browse files
horvathdorawchevreuil
authored andcommitted
HBASE-26284 Add HBase Thrift API to get all table names along with wh… (#3693) (#3745)
Signed-off-by: Wellington Chevreuil <wchevreuil@apache.org> (cherry picked from commit 6d0777a)
1 parent 34f9b3e commit dd40fd7

File tree

5 files changed

+3408
-2395
lines changed

5 files changed

+3408
-2395
lines changed

hbase-examples/src/main/java/org/apache/hadoop/hbase/thrift/DemoClient.java

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,16 @@ private void run() throws Exception {
126126
TProtocol protocol = new TBinaryProtocol(transport, true, true);
127127
Hbase.Client client = new Hbase.Client(protocol);
128128

129-
byte[] t = bytes("demo_table");
129+
ByteBuffer demoTable = ByteBuffer.wrap(bytes("demo_table"));
130+
ByteBuffer disabledTable = ByteBuffer.wrap(bytes("disabled_table"));
130131

131132
// Scan all tables, look for the demo table and delete it.
132133
System.out.println("scanning tables...");
133134

134135
for (ByteBuffer name : client.getTableNames()) {
135136
System.out.println(" found: " + ClientUtils.utf8(name.array()));
136137

137-
if (ClientUtils.utf8(name.array()).equals(ClientUtils.utf8(t))) {
138+
if (name.equals(demoTable) || name.equals(disabledTable)) {
138139
if (client.isTableEnabled(name)) {
139140
System.out.println(" disabling table: " + ClientUtils.utf8(name.array()));
140141
client.disableTable(name);
@@ -158,22 +159,35 @@ private void run() throws Exception {
158159
col.timeToLive = Integer.MAX_VALUE;
159160
columns.add(col);
160161

161-
System.out.println("creating table: " + ClientUtils.utf8(t));
162+
System.out.println("creating table: " + ClientUtils.utf8(demoTable.array()));
162163

163164
try {
164-
client.createTable(ByteBuffer.wrap(t), columns);
165+
client.createTable(demoTable, columns);
166+
client.createTable(disabledTable, columns);
165167
} catch (AlreadyExists ae) {
166168
System.out.println("WARN: " + ae.message);
167169
}
168170

169-
System.out.println("column families in " + ClientUtils.utf8(t) + ": ");
170-
Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t));
171+
System.out.println("column families in " + ClientUtils.utf8(demoTable.array()) + ": ");
172+
Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(demoTable);
171173

172174
for (ColumnDescriptor col2 : columnMap.values()) {
173175
System.out.println(" column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: "
174176
+ col2.maxVersions);
175177
}
176178

179+
if (client.isTableEnabled(disabledTable)){
180+
System.out.println("disabling table: " + ClientUtils.utf8(disabledTable.array()));
181+
client.disableTable(disabledTable);
182+
}
183+
184+
System.out.println("list tables with enabled statuses : ");
185+
Map<ByteBuffer, Boolean> statusMap = client.getTableNamesWithIsTableEnabled();
186+
for (Map.Entry<ByteBuffer, Boolean> entry : statusMap.entrySet()) {
187+
System.out.println(" Table: " + ClientUtils.utf8(entry.getKey().array()) +
188+
", is enabled: " + entry.getValue());
189+
}
190+
177191
Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
178192
boolean writeToWal = false;
179193

@@ -190,27 +204,27 @@ private void run() throws Exception {
190204
mutations = new ArrayList<>(1);
191205
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
192206
ByteBuffer.wrap(invalid), writeToWal));
193-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("foo")),
207+
client.mutateRow(demoTable, ByteBuffer.wrap(bytes("foo")),
194208
mutations, dummyAttributes);
195209

196210
// this row name is valid utf8
197211
mutations = new ArrayList<>(1);
198212
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
199213
ByteBuffer.wrap(valid), writeToWal));
200-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes);
214+
client.mutateRow(demoTable, ByteBuffer.wrap(valid), mutations, dummyAttributes);
201215

202216
// non-utf8 is now allowed in row names because HBase stores values as binary
203217
mutations = new ArrayList<>(1);
204218
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
205219
ByteBuffer.wrap(invalid), writeToWal));
206-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes);
220+
client.mutateRow(demoTable, ByteBuffer.wrap(invalid), mutations, dummyAttributes);
207221

208222
// Run a scanner on the rows we just created
209223
ArrayList<ByteBuffer> columnNames = new ArrayList<>();
210224
columnNames.add(ByteBuffer.wrap(bytes("entry:")));
211225

212226
System.out.println("Starting scanner...");
213-
int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames,
227+
int scanner = client.scannerOpen(demoTable, ByteBuffer.wrap(bytes("")), columnNames,
214228
dummyAttributes);
215229

216230
while (true) {
@@ -234,9 +248,9 @@ private void run() throws Exception {
234248
mutations = new ArrayList<>(1);
235249
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")),
236250
ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
237-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
238-
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
239-
client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes);
251+
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
252+
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
253+
client.deleteAllRow(demoTable, ByteBuffer.wrap(row), dummyAttributes);
240254

241255
// sleep to force later timestamp
242256
try {
@@ -250,8 +264,8 @@ private void run() throws Exception {
250264
ByteBuffer.wrap(bytes("0")), writeToWal));
251265
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
252266
ByteBuffer.wrap(bytes("FOO")), writeToWal));
253-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
254-
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
267+
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
268+
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
255269

256270
Mutation m;
257271
mutations = new ArrayList<>(2);
@@ -263,16 +277,16 @@ private void run() throws Exception {
263277
m.column = ByteBuffer.wrap(bytes("entry:num"));
264278
m.value = ByteBuffer.wrap(bytes("-1"));
265279
mutations.add(m);
266-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
267-
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
280+
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
281+
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
268282

269283
mutations = new ArrayList<>();
270284
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")),
271285
ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
272286
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")),
273287
ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
274-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
275-
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
288+
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
289+
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
276290

277291
// sleep to force later timestamp
278292
try {
@@ -289,11 +303,11 @@ private void run() throws Exception {
289303
m = new Mutation();
290304
m.column = ByteBuffer.wrap(bytes("entry:sqr"));
291305
m.isDelete = true;
292-
client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1,
306+
client.mutateRowTs(demoTable, ByteBuffer.wrap(row), mutations, 1,
293307
dummyAttributes); // shouldn't override latest
294-
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
308+
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
295309

296-
List<TCell> versions = client.getVer(ByteBuffer.wrap(t), ByteBuffer.wrap(row),
310+
List<TCell> versions = client.getVer(demoTable, ByteBuffer.wrap(row),
297311
ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
298312
printVersions(ByteBuffer.wrap(row), versions);
299313

@@ -302,7 +316,7 @@ private void run() throws Exception {
302316
System.exit(-1);
303317
}
304318

305-
List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row),
319+
List<TCell> result = client.get(demoTable, ByteBuffer.wrap(row),
306320
ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
307321

308322
if (!result.isEmpty()) {
@@ -316,15 +330,15 @@ private void run() throws Exception {
316330
// scan all rows/columnNames
317331
columnNames.clear();
318332

319-
for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) {
333+
for (ColumnDescriptor col2 : client.getColumnDescriptors(demoTable).values()) {
320334
System.out.println("column with name: " + new String(col2.name.array()));
321335
System.out.println(col2.toString());
322336

323337
columnNames.add(col2.name);
324338
}
325339

326340
System.out.println("Starting scanner...");
327-
scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")),
341+
scanner = client.scannerOpenWithStop(demoTable, ByteBuffer.wrap(bytes("00020")),
328342
ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
329343

330344
while (true) {

hbase-thrift/src/main/java/org/apache/hadoop/hbase/thrift/ThriftHBaseServiceHandler.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.ByteBuffer;
2929
import java.util.ArrayList;
3030
import java.util.Collections;
31+
import java.util.HashMap;
3132
import java.util.List;
3233
import java.util.Map;
3334
import java.util.TreeMap;
@@ -202,6 +203,20 @@ public boolean isTableEnabled(ByteBuffer tableName) throws IOError {
202203
}
203204
}
204205

206+
@Override
207+
public Map<ByteBuffer, Boolean> getTableNamesWithIsTableEnabled() throws IOError {
208+
try {
209+
HashMap<ByteBuffer, Boolean> tables = new HashMap<>();
210+
for (ByteBuffer tableName: this.getTableNames()) {
211+
tables.put(tableName, this.isTableEnabled(tableName));
212+
}
213+
return tables;
214+
} catch (IOError e) {
215+
LOG.warn(e.getMessage(), e);
216+
throw getIOError(e);
217+
}
218+
}
219+
205220
// ThriftServerRunner.compact should be deprecated and replaced with methods specific to
206221
// table and region.
207222
@Override

0 commit comments

Comments
 (0)