Skip to content

Commit cbde899

Browse files
author
dorahorvath
committed
refactor DemoClient class
1 parent b2bde2d commit cbde899

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void run() throws Exception {
123123
TProtocol protocol = new TBinaryProtocol(transport, true, true);
124124
Hbase.Client client = new Hbase.Client(protocol);
125125

126-
byte[] t = bytes("demo_table");
126+
ByteBuffer demoTable = ByteBuffer.wrap(bytes("demo_table"));
127127
ByteBuffer disabledTable = ByteBuffer.wrap(bytes("disabled_table"));
128128

129129
// Scan all tables, look for the demo table and delete it.
@@ -132,7 +132,7 @@ private void run() throws Exception {
132132
for (ByteBuffer name : client.getTableNames()) {
133133
System.out.println(" found: " + ClientUtils.utf8(name.array()));
134134

135-
if (ClientUtils.utf8(name.array()).equals(ClientUtils.utf8(t)) || name.equals(disabledTable)) {
135+
if (name.equals(demoTable) || name.equals(disabledTable)) {
136136
if (client.isTableEnabled(name)) {
137137
System.out.println(" disabling table: " + ClientUtils.utf8(name.array()));
138138
client.disableTable(name);
@@ -156,29 +156,30 @@ private void run() throws Exception {
156156
col.timeToLive = Integer.MAX_VALUE;
157157
columns.add(col);
158158

159-
System.out.println("creating table: " + ClientUtils.utf8(t));
159+
System.out.println("creating table: " + ClientUtils.utf8(demoTable.array()));
160160

161161
try {
162-
client.createTable(ByteBuffer.wrap(t), columns);
162+
client.createTable(demoTable, columns);
163163
client.createTable(disabledTable, columns);
164164
} catch (AlreadyExists ae) {
165165
System.out.println("WARN: " + ae.message);
166166
}
167167

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

171171
for (ColumnDescriptor col2 : columnMap.values()) {
172172
System.out.println(" column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: "
173173
+ col2.maxVersions);
174174
}
175175

176176
if (client.isTableEnabled(disabledTable)){
177+
System.out.println("disabling table: " + ClientUtils.utf8(disabledTable.array()));
177178
client.disableTable(disabledTable);
178179
}
180+
179181
System.out.println("list tables with enabled statuses : ");
180182
Map<ByteBuffer, Boolean> statusMap = client.getTableNamesWithIsTableEnabled();
181-
182183
for (Map.Entry<ByteBuffer, Boolean> entry : statusMap.entrySet()) {
183184
System.out.println(" Table: " + ClientUtils.utf8(entry.getKey().array()) +
184185
", is enabled: " + entry.getValue());
@@ -200,27 +201,27 @@ private void run() throws Exception {
200201
mutations = new ArrayList<>(1);
201202
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
202203
ByteBuffer.wrap(invalid), writeToWal));
203-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("foo")),
204+
client.mutateRow(demoTable, ByteBuffer.wrap(bytes("foo")),
204205
mutations, dummyAttributes);
205206

206207
// this row name is valid utf8
207208
mutations = new ArrayList<>(1);
208209
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
209210
ByteBuffer.wrap(valid), writeToWal));
210-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes);
211+
client.mutateRow(demoTable, ByteBuffer.wrap(valid), mutations, dummyAttributes);
211212

212213
// non-utf8 is now allowed in row names because HBase stores values as binary
213214
mutations = new ArrayList<>(1);
214215
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
215216
ByteBuffer.wrap(invalid), writeToWal));
216-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes);
217+
client.mutateRow(demoTable, ByteBuffer.wrap(invalid), mutations, dummyAttributes);
217218

218219
// Run a scanner on the rows we just created
219220
ArrayList<ByteBuffer> columnNames = new ArrayList<>();
220221
columnNames.add(ByteBuffer.wrap(bytes("entry:")));
221222

222223
System.out.println("Starting scanner...");
223-
int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames,
224+
int scanner = client.scannerOpen(demoTable, ByteBuffer.wrap(bytes("")), columnNames,
224225
dummyAttributes);
225226

226227
while (true) {
@@ -244,9 +245,9 @@ private void run() throws Exception {
244245
mutations = new ArrayList<>(1);
245246
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")),
246247
ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
247-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
248-
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
249-
client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes);
248+
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
249+
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
250+
client.deleteAllRow(demoTable, ByteBuffer.wrap(row), dummyAttributes);
250251

251252
// sleep to force later timestamp
252253
try {
@@ -260,8 +261,8 @@ private void run() throws Exception {
260261
ByteBuffer.wrap(bytes("0")), writeToWal));
261262
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
262263
ByteBuffer.wrap(bytes("FOO")), writeToWal));
263-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
264-
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
264+
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
265+
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
265266

266267
Mutation m;
267268
mutations = new ArrayList<>(2);
@@ -273,16 +274,16 @@ private void run() throws Exception {
273274
m.column = ByteBuffer.wrap(bytes("entry:num"));
274275
m.value = ByteBuffer.wrap(bytes("-1"));
275276
mutations.add(m);
276-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
277-
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
277+
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
278+
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
278279

279280
mutations = new ArrayList<>();
280281
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")),
281282
ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
282283
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")),
283284
ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
284-
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
285-
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
285+
client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
286+
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
286287

287288
// sleep to force later timestamp
288289
try {
@@ -299,11 +300,11 @@ private void run() throws Exception {
299300
m = new Mutation();
300301
m.column = ByteBuffer.wrap(bytes("entry:sqr"));
301302
m.isDelete = true;
302-
client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1,
303+
client.mutateRowTs(demoTable, ByteBuffer.wrap(row), mutations, 1,
303304
dummyAttributes); // shouldn't override latest
304-
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
305+
printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
305306

306-
List<TCell> versions = client.getVer(ByteBuffer.wrap(t), ByteBuffer.wrap(row),
307+
List<TCell> versions = client.getVer(demoTable, ByteBuffer.wrap(row),
307308
ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
308309
printVersions(ByteBuffer.wrap(row), versions);
309310

@@ -312,7 +313,7 @@ private void run() throws Exception {
312313
System.exit(-1);
313314
}
314315

315-
List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row),
316+
List<TCell> result = client.get(demoTable, ByteBuffer.wrap(row),
316317
ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
317318

318319
if (!result.isEmpty()) {
@@ -326,15 +327,15 @@ private void run() throws Exception {
326327
// scan all rows/columnNames
327328
columnNames.clear();
328329

329-
for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) {
330+
for (ColumnDescriptor col2 : client.getColumnDescriptors(demoTable).values()) {
330331
System.out.println("column with name: " + new String(col2.name.array()));
331332
System.out.println(col2.toString());
332333

333334
columnNames.add(col2.name);
334335
}
335336

336337
System.out.println("Starting scanner...");
337-
scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")),
338+
scanner = client.scannerOpenWithStop(demoTable, ByteBuffer.wrap(bytes("00020")),
338339
ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
339340

340341
while (true) {

0 commit comments

Comments
 (0)