1919package org .apache .hadoop .hbase .thrift ;
2020
2121import java .nio .ByteBuffer ;
22- import java .nio .charset .CharacterCodingException ;
23- import java .nio .charset .Charset ;
24- import java .nio .charset .CharsetDecoder ;
2522import java .security .PrivilegedExceptionAction ;
2623import java .text .NumberFormat ;
2724import java .util .ArrayList ;
2825import java .util .HashMap ;
2926import java .util .List ;
3027import java .util .Map ;
31- import java .util .SortedMap ;
32- import java .util .TreeMap ;
3328import javax .security .auth .Subject ;
34- import javax .security .auth .login .AppConfigurationEntry ;
35- import javax .security .auth .login .Configuration ;
3629import javax .security .auth .login .LoginContext ;
3730import javax .security .sasl .Sasl ;
3831import org .apache .hadoop .hbase .thrift .generated .AlreadyExists ;
4235import org .apache .hadoop .hbase .thrift .generated .TCell ;
4336import org .apache .hadoop .hbase .thrift .generated .TRowResult ;
4437import org .apache .hadoop .hbase .util .Bytes ;
38+ import org .apache .hadoop .hbase .util .ClientUtils ;
4539import org .apache .thrift .protocol .TBinaryProtocol ;
4640import org .apache .thrift .protocol .TProtocol ;
4741import org .apache .thrift .transport .TSaslClientTransport ;
@@ -60,7 +54,6 @@ public class DemoClient {
6054
6155 static protected int port ;
6256 static protected String host ;
63- CharsetDecoder decoder = null ;
6457
6558 private static boolean secure = false ;
6659 private static String serverPrincipal = "hbase" ;
@@ -101,16 +94,6 @@ private static boolean isBoolean(String s){
10194 }
10295
10396 DemoClient () {
104- decoder = Charset .forName ("UTF-8" ).newDecoder ();
105- }
106-
107- // Helper to translate byte[]'s to UTF8 strings
108- private String utf8 (byte [] buf ) {
109- try {
110- return decoder .decode (ByteBuffer .wrap (buf )).toString ();
111- } catch (CharacterCodingException e ) {
112- return "[INVALID UTF-8]" ;
113- }
11497 }
11598
11699 // Helper to translate strings to UTF8 bytes
@@ -146,15 +129,15 @@ private void run() throws Exception {
146129 System .out .println ("scanning tables..." );
147130
148131 for (ByteBuffer name : client .getTableNames ()) {
149- System .out .println (" found: " + utf8 (name .array ()));
132+ System .out .println (" found: " + ClientUtils . utf8 (name .array ()));
150133
151- if (utf8 (name .array ()).equals (utf8 (t ))) {
134+ if (ClientUtils . utf8 (name .array ()).equals (ClientUtils . utf8 (t ))) {
152135 if (client .isTableEnabled (name )) {
153- System .out .println (" disabling table: " + utf8 (name .array ()));
136+ System .out .println (" disabling table: " + ClientUtils . utf8 (name .array ()));
154137 client .disableTable (name );
155138 }
156139
157- System .out .println (" deleting table: " + utf8 (name .array ()));
140+ System .out .println (" deleting table: " + ClientUtils . utf8 (name .array ()));
158141 client .deleteTable (name );
159142 }
160143 }
@@ -172,19 +155,20 @@ private void run() throws Exception {
172155 col .timeToLive = Integer .MAX_VALUE ;
173156 columns .add (col );
174157
175- System .out .println ("creating table: " + utf8 (t ));
158+ System .out .println ("creating table: " + ClientUtils . utf8 (t ));
176159
177160 try {
178161 client .createTable (ByteBuffer .wrap (t ), columns );
179162 } catch (AlreadyExists ae ) {
180163 System .out .println ("WARN: " + ae .message );
181164 }
182165
183- System .out .println ("column families in " + utf8 (t ) + ": " );
166+ System .out .println ("column families in " + ClientUtils . utf8 (t ) + ": " );
184167 Map <ByteBuffer , ColumnDescriptor > columnMap = client .getColumnDescriptors (ByteBuffer .wrap (t ));
185168
186169 for (ColumnDescriptor col2 : columnMap .values ()) {
187- System .out .println (" column: " + utf8 (col2 .name .array ()) + ", maxVer: " + col2 .maxVersions );
170+ System .out .println (" column: " + ClientUtils .utf8 (col2 .name .array ()) + ", maxVer: "
171+ + col2 .maxVersions );
188172 }
189173
190174 Map <ByteBuffer , ByteBuffer > dummyAttributes = null ;
@@ -358,31 +342,15 @@ private void printVersions(ByteBuffer row, List<TCell> versions) {
358342 StringBuilder rowStr = new StringBuilder ();
359343
360344 for (TCell cell : versions ) {
361- rowStr .append (utf8 (cell .value .array ()));
345+ rowStr .append (ClientUtils . utf8 (cell .value .array ()));
362346 rowStr .append ("; " );
363347 }
364348
365- System .out .println ("row: " + utf8 (row .array ()) + ", values: " + rowStr );
349+ System .out .println ("row: " + ClientUtils . utf8 (row .array ()) + ", values: " + rowStr );
366350 }
367351
368352 private void printRow (TRowResult rowResult ) {
369- // copy values into a TreeMap to get them in sorted order
370- TreeMap <String , TCell > sorted = new TreeMap <>();
371-
372- for (Map .Entry <ByteBuffer , TCell > column : rowResult .columns .entrySet ()) {
373- sorted .put (utf8 (column .getKey ().array ()), column .getValue ());
374- }
375-
376- StringBuilder rowStr = new StringBuilder ();
377-
378- for (SortedMap .Entry <String , TCell > entry : sorted .entrySet ()) {
379- rowStr .append (entry .getKey ());
380- rowStr .append (" => " );
381- rowStr .append (utf8 (entry .getValue ().value .array ()));
382- rowStr .append ("; " );
383- }
384-
385- System .out .println ("row: " + utf8 (rowResult .row .array ()) + ", cols: " + rowStr );
353+ ClientUtils .printRow (rowResult );
386354 }
387355
388356 private void printRow (List <TRowResult > rows ) {
@@ -396,37 +364,7 @@ static Subject getSubject() throws Exception {
396364 return new Subject ();
397365 }
398366
399- /*
400- * To authenticate the DemoClient, kinit should be invoked ahead.
401- * Here we try to get the Kerberos credential from the ticket cache.
402- */
403- LoginContext context = new LoginContext ("" , new Subject (), null ,
404- new Configuration () {
405- @ Override
406- public AppConfigurationEntry [] getAppConfigurationEntry (String name ) {
407- Map <String , String > options = new HashMap <>();
408- options .put ("useKeyTab" , "false" );
409- options .put ("storeKey" , "false" );
410- options .put ("doNotPrompt" , "true" );
411- options .put ("useTicketCache" , "true" );
412- options .put ("renewTGT" , "true" );
413- options .put ("refreshKrb5Config" , "true" );
414- options .put ("isInitiator" , "true" );
415- String ticketCache = System .getenv ("KRB5CCNAME" );
416-
417- if (ticketCache != null ) {
418- options .put ("ticketCache" , ticketCache );
419- }
420-
421- options .put ("debug" , "true" );
422-
423- return new AppConfigurationEntry []{
424- new AppConfigurationEntry ("com.sun.security.auth.module.Krb5LoginModule" ,
425- AppConfigurationEntry .LoginModuleControlFlag .REQUIRED ,
426- options )};
427- }
428- });
429-
367+ LoginContext context = ClientUtils .getLoginContext ();
430368 context .login ();
431369 return context .getSubject ();
432370 }
0 commit comments