20
20
21
21
import java .io .UnsupportedEncodingException ;
22
22
import java .nio .ByteBuffer ;
23
- import java .nio .charset .CharacterCodingException ;
24
- import java .nio .charset .Charset ;
25
- import java .nio .charset .CharsetDecoder ;
26
23
import java .security .PrivilegedExceptionAction ;
27
24
import java .text .NumberFormat ;
28
25
import java .util .ArrayList ;
29
26
import java .util .HashMap ;
30
27
import java .util .List ;
31
28
import java .util .Map ;
32
- import java .util .SortedMap ;
33
- import java .util .TreeMap ;
34
29
import javax .security .auth .Subject ;
35
- import javax .security .auth .login .AppConfigurationEntry ;
36
- import javax .security .auth .login .Configuration ;
37
30
import javax .security .auth .login .LoginContext ;
38
31
import javax .security .sasl .Sasl ;
39
32
import org .apache .hadoop .hbase .thrift .generated .AlreadyExists ;
42
35
import org .apache .hadoop .hbase .thrift .generated .Mutation ;
43
36
import org .apache .hadoop .hbase .thrift .generated .TCell ;
44
37
import org .apache .hadoop .hbase .thrift .generated .TRowResult ;
38
+ import org .apache .hadoop .hbase .util .Bytes ;
39
+ import org .apache .hadoop .hbase .util .ClientUtils ;
45
40
import org .apache .thrift .protocol .TBinaryProtocol ;
46
41
import org .apache .thrift .protocol .TProtocol ;
47
42
import org .apache .thrift .transport .TSaslClientTransport ;
@@ -57,7 +52,6 @@ public class DemoClient {
57
52
58
53
static protected int port ;
59
54
static protected String host ;
60
- CharsetDecoder decoder = null ;
61
55
62
56
private static boolean secure = false ;
63
57
private static String serverPrincipal = "hbase" ;
@@ -98,16 +92,6 @@ private static boolean isBoolean(String s){
98
92
}
99
93
100
94
DemoClient () {
101
- decoder = Charset .forName ("UTF-8" ).newDecoder ();
102
- }
103
-
104
- // Helper to translate byte[]'s to UTF8 strings
105
- private String utf8 (byte [] buf ) {
106
- try {
107
- return decoder .decode (ByteBuffer .wrap (buf )).toString ();
108
- } catch (CharacterCodingException e ) {
109
- return "[INVALID UTF-8]" ;
110
- }
111
95
}
112
96
113
97
// Helper to translate strings to UTF8 bytes
@@ -148,15 +132,15 @@ private void run() throws Exception {
148
132
System .out .println ("scanning tables..." );
149
133
150
134
for (ByteBuffer name : client .getTableNames ()) {
151
- System .out .println (" found: " + utf8 (name .array ()));
135
+ System .out .println (" found: " + ClientUtils . utf8 (name .array ()));
152
136
153
- if (utf8 (name .array ()).equals (utf8 (t ))) {
137
+ if (ClientUtils . utf8 (name .array ()).equals (ClientUtils . utf8 (t ))) {
154
138
if (client .isTableEnabled (name )) {
155
- System .out .println (" disabling table: " + utf8 (name .array ()));
139
+ System .out .println (" disabling table: " + ClientUtils . utf8 (name .array ()));
156
140
client .disableTable (name );
157
141
}
158
142
159
- System .out .println (" deleting table: " + utf8 (name .array ()));
143
+ System .out .println (" deleting table: " + ClientUtils . utf8 (name .array ()));
160
144
client .deleteTable (name );
161
145
}
162
146
}
@@ -174,19 +158,20 @@ private void run() throws Exception {
174
158
col .timeToLive = Integer .MAX_VALUE ;
175
159
columns .add (col );
176
160
177
- System .out .println ("creating table: " + utf8 (t ));
161
+ System .out .println ("creating table: " + ClientUtils . utf8 (t ));
178
162
179
163
try {
180
164
client .createTable (ByteBuffer .wrap (t ), columns );
181
165
} catch (AlreadyExists ae ) {
182
166
System .out .println ("WARN: " + ae .message );
183
167
}
184
168
185
- System .out .println ("column families in " + utf8 (t ) + ": " );
169
+ System .out .println ("column families in " + ClientUtils . utf8 (t ) + ": " );
186
170
Map <ByteBuffer , ColumnDescriptor > columnMap = client .getColumnDescriptors (ByteBuffer .wrap (t ));
187
171
188
172
for (ColumnDescriptor col2 : columnMap .values ()) {
189
- System .out .println (" column: " + utf8 (col2 .name .array ()) + ", maxVer: " + col2 .maxVersions );
173
+ System .out .println (" column: " + ClientUtils .utf8 (col2 .name .array ()) + ", maxVer: "
174
+ + col2 .maxVersions );
190
175
}
191
176
192
177
Map <ByteBuffer , ByteBuffer > dummyAttributes = null ;
@@ -360,31 +345,15 @@ private void printVersions(ByteBuffer row, List<TCell> versions) {
360
345
StringBuilder rowStr = new StringBuilder ();
361
346
362
347
for (TCell cell : versions ) {
363
- rowStr .append (utf8 (cell .value .array ()));
348
+ rowStr .append (ClientUtils . utf8 (cell .value .array ()));
364
349
rowStr .append ("; " );
365
350
}
366
351
367
- System .out .println ("row: " + utf8 (row .array ()) + ", values: " + rowStr );
352
+ System .out .println ("row: " + ClientUtils . utf8 (row .array ()) + ", values: " + rowStr );
368
353
}
369
354
370
355
private void printRow (TRowResult rowResult ) {
371
- // copy values into a TreeMap to get them in sorted order
372
- TreeMap <String , TCell > sorted = new TreeMap <>();
373
-
374
- for (Map .Entry <ByteBuffer , TCell > column : rowResult .columns .entrySet ()) {
375
- sorted .put (utf8 (column .getKey ().array ()), column .getValue ());
376
- }
377
-
378
- StringBuilder rowStr = new StringBuilder ();
379
-
380
- for (SortedMap .Entry <String , TCell > entry : sorted .entrySet ()) {
381
- rowStr .append (entry .getKey ());
382
- rowStr .append (" => " );
383
- rowStr .append (utf8 (entry .getValue ().value .array ()));
384
- rowStr .append ("; " );
385
- }
386
-
387
- System .out .println ("row: " + utf8 (rowResult .row .array ()) + ", cols: " + rowStr );
356
+ ClientUtils .printRow (rowResult );
388
357
}
389
358
390
359
private void printRow (List <TRowResult > rows ) {
@@ -397,38 +366,7 @@ static Subject getSubject() throws Exception {
397
366
if (!secure ) {
398
367
return new Subject ();
399
368
}
400
-
401
- /*
402
- * To authenticate the DemoClient, kinit should be invoked ahead.
403
- * Here we try to get the Kerberos credential from the ticket cache.
404
- */
405
- LoginContext context = new LoginContext ("" , new Subject (), null ,
406
- new Configuration () {
407
- @ Override
408
- public AppConfigurationEntry [] getAppConfigurationEntry (String name ) {
409
- Map <String , String > options = new HashMap <>();
410
- options .put ("useKeyTab" , "false" );
411
- options .put ("storeKey" , "false" );
412
- options .put ("doNotPrompt" , "true" );
413
- options .put ("useTicketCache" , "true" );
414
- options .put ("renewTGT" , "true" );
415
- options .put ("refreshKrb5Config" , "true" );
416
- options .put ("isInitiator" , "true" );
417
- String ticketCache = System .getenv ("KRB5CCNAME" );
418
-
419
- if (ticketCache != null ) {
420
- options .put ("ticketCache" , ticketCache );
421
- }
422
-
423
- options .put ("debug" , "true" );
424
-
425
- return new AppConfigurationEntry []{
426
- new AppConfigurationEntry ("com.sun.security.auth.module.Krb5LoginModule" ,
427
- AppConfigurationEntry .LoginModuleControlFlag .REQUIRED ,
428
- options )};
429
- }
430
- });
431
-
369
+ LoginContext context = ClientUtils .getLoginContext ();
432
370
context .login ();
433
371
return context .getSubject ();
434
372
}
0 commit comments