Skip to content

Commit 064f5f1

Browse files
virajjasaniopeninx
authored andcommitted
HBASE-22743 : ClientUtils for Demo Client classes (apache#413)
1 parent 3318b4b commit 064f5f1

File tree

5 files changed

+149
-174
lines changed

5 files changed

+149
-174
lines changed

hbase-examples/src/main/java/org/apache/hadoop/hbase/rest/RESTDemoClient.java

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@
1919
package org.apache.hadoop.hbase.rest;
2020

2121
import java.security.PrivilegedExceptionAction;
22-
import java.util.HashMap;
23-
import java.util.Map;
2422

2523
import javax.security.auth.Subject;
26-
import javax.security.auth.login.AppConfigurationEntry;
27-
import javax.security.auth.login.Configuration;
2824
import javax.security.auth.login.LoginContext;
2925

3026
import org.apache.hadoop.hbase.Cell;
@@ -37,6 +33,7 @@
3733
import org.apache.hadoop.hbase.rest.client.Cluster;
3834
import org.apache.hadoop.hbase.rest.client.RemoteHTable;
3935
import org.apache.hadoop.hbase.util.Bytes;
36+
import org.apache.hadoop.hbase.util.ClientUtils;
4037
import org.apache.yetus.audience.InterfaceAudience;
4138
import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
4239

@@ -112,32 +109,7 @@ static Subject getSubject() throws Exception {
112109
return new Subject();
113110
}
114111

115-
/*
116-
* To authenticate the demo client, kinit should be invoked ahead. Here we try to get the
117-
* Kerberos credential from the ticket cache.
118-
*/
119-
LoginContext context = new LoginContext("", new Subject(), null, new Configuration() {
120-
@Override
121-
public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
122-
Map<String, String> options = new HashMap<>();
123-
options.put("useKeyTab", "false");
124-
options.put("storeKey", "false");
125-
options.put("doNotPrompt", "true");
126-
options.put("useTicketCache", "true");
127-
options.put("renewTGT", "true");
128-
options.put("refreshKrb5Config", "true");
129-
options.put("isInitiator", "true");
130-
String ticketCache = System.getenv("KRB5CCNAME");
131-
if (ticketCache != null) {
132-
options.put("ticketCache", ticketCache);
133-
}
134-
options.put("debug", "true");
135-
136-
return new AppConfigurationEntry[] {
137-
new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule",
138-
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options) };
139-
}
140-
});
112+
LoginContext context = ClientUtils.getLoginContext();
141113
context.login();
142114
return context.getSubject();
143115
}

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

Lines changed: 13 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,13 @@
1919
package org.apache.hadoop.hbase.thrift;
2020

2121
import java.nio.ByteBuffer;
22-
import java.nio.charset.CharacterCodingException;
23-
import java.nio.charset.Charset;
24-
import java.nio.charset.CharsetDecoder;
2522
import java.security.PrivilegedExceptionAction;
2623
import java.text.NumberFormat;
2724
import java.util.ArrayList;
2825
import java.util.HashMap;
2926
import java.util.List;
3027
import java.util.Map;
31-
import java.util.SortedMap;
32-
import java.util.TreeMap;
3328
import javax.security.auth.Subject;
34-
import javax.security.auth.login.AppConfigurationEntry;
35-
import javax.security.auth.login.Configuration;
3629
import javax.security.auth.login.LoginContext;
3730
import javax.security.sasl.Sasl;
3831
import org.apache.hadoop.hbase.thrift.generated.AlreadyExists;
@@ -42,6 +35,7 @@
4235
import org.apache.hadoop.hbase.thrift.generated.TCell;
4336
import org.apache.hadoop.hbase.thrift.generated.TRowResult;
4437
import org.apache.hadoop.hbase.util.Bytes;
38+
import org.apache.hadoop.hbase.util.ClientUtils;
4539
import org.apache.thrift.protocol.TBinaryProtocol;
4640
import org.apache.thrift.protocol.TProtocol;
4741
import 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
}

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

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020

2121
import java.io.File;
2222
import java.nio.ByteBuffer;
23-
import java.nio.charset.CharacterCodingException;
24-
import java.nio.charset.Charset;
25-
import java.nio.charset.CharsetDecoder;
2623
import java.security.Principal;
2724
import java.security.PrivilegedExceptionAction;
2825
import java.util.ArrayList;
@@ -32,8 +29,6 @@
3229
import java.util.List;
3330
import java.util.Map;
3431
import java.util.Set;
35-
import java.util.SortedMap;
36-
import java.util.TreeMap;
3732
import javax.security.auth.Subject;
3833
import javax.security.auth.kerberos.KerberosPrincipal;
3934
import javax.security.auth.login.AppConfigurationEntry;
@@ -45,6 +40,7 @@
4540
import org.apache.hadoop.hbase.thrift.generated.TCell;
4641
import org.apache.hadoop.hbase.thrift.generated.TRowResult;
4742
import org.apache.hadoop.hbase.util.Bytes;
43+
import org.apache.hadoop.hbase.util.ClientUtils;
4844
import org.apache.thrift.protocol.TBinaryProtocol;
4945
import org.apache.thrift.protocol.TProtocol;
5046
import org.apache.thrift.transport.THttpClient;
@@ -69,7 +65,6 @@ public class HttpDoAsClient {
6965

7066
static protected int port;
7167
static protected String host;
72-
CharsetDecoder decoder = null;
7368
private static boolean secure = false;
7469
static protected String doAsUser = null;
7570
static protected String principal = null;
@@ -112,16 +107,6 @@ public Void run() throws Exception {
112107
}
113108

114109
HttpDoAsClient() {
115-
decoder = Charset.forName("UTF-8").newDecoder();
116-
}
117-
118-
// Helper to translate byte[]'s to UTF8 strings
119-
private String utf8(byte[] buf) {
120-
try {
121-
return decoder.decode(ByteBuffer.wrap(buf)).toString();
122-
} catch (CharacterCodingException e) {
123-
return "[INVALID UTF-8]";
124-
}
125110
}
126111

127112
// Helper to translate strings to UTF8 bytes
@@ -146,13 +131,13 @@ private void run() throws Exception {
146131
//
147132
System.out.println("scanning tables...");
148133
for (ByteBuffer name : refresh(client, httpClient).getTableNames()) {
149-
System.out.println(" found: " + utf8(name.array()));
150-
if (utf8(name.array()).equals(utf8(t))) {
134+
System.out.println(" found: " + ClientUtils.utf8(name.array()));
135+
if (ClientUtils.utf8(name.array()).equals(ClientUtils.utf8(t))) {
151136
if (refresh(client, httpClient).isTableEnabled(name)) {
152-
System.out.println(" disabling table: " + utf8(name.array()));
137+
System.out.println(" disabling table: " + ClientUtils.utf8(name.array()));
153138
refresh(client, httpClient).disableTable(name);
154139
}
155-
System.out.println(" deleting table: " + utf8(name.array()));
140+
System.out.println(" deleting table: " + ClientUtils.utf8(name.array()));
156141
refresh(client, httpClient).deleteTable(name);
157142
}
158143
}
@@ -172,19 +157,20 @@ private void run() throws Exception {
172157
col.timeToLive = Integer.MAX_VALUE;
173158
columns.add(col);
174159

175-
System.out.println("creating table: " + utf8(t));
160+
System.out.println("creating table: " + ClientUtils.utf8(t));
176161
try {
177162

178163
refresh(client, httpClient).createTable(ByteBuffer.wrap(t), columns);
179164
} catch (AlreadyExists ae) {
180165
System.out.println("WARN: " + ae.message);
181166
}
182167

183-
System.out.println("column families in " + utf8(t) + ": ");
168+
System.out.println("column families in " + ClientUtils.utf8(t) + ": ");
184169
Map<ByteBuffer, ColumnDescriptor> columnMap = refresh(client, httpClient)
185170
.getColumnDescriptors(ByteBuffer.wrap(t));
186171
for (ColumnDescriptor col2 : columnMap.values()) {
187-
System.out.println(" column: " + utf8(col2.name.array()) + ", maxVer: " + col2.maxVersions);
172+
System.out.println(" column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: "
173+
+ col2.maxVersions);
188174
}
189175

190176
transport.close();
@@ -236,27 +222,14 @@ private String generateTicket() throws GSSException {
236222
private void printVersions(ByteBuffer row, List<TCell> versions) {
237223
StringBuilder rowStr = new StringBuilder();
238224
for (TCell cell : versions) {
239-
rowStr.append(utf8(cell.value.array()));
225+
rowStr.append(ClientUtils.utf8(cell.value.array()));
240226
rowStr.append("; ");
241227
}
242-
System.out.println("row: " + utf8(row.array()) + ", values: " + rowStr);
228+
System.out.println("row: " + ClientUtils.utf8(row.array()) + ", values: " + rowStr);
243229
}
244230

245231
private void printRow(TRowResult rowResult) {
246-
// copy values into a TreeMap to get them in sorted order
247-
TreeMap<String, TCell> sorted = new TreeMap<>();
248-
for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) {
249-
sorted.put(utf8(column.getKey().array()), column.getValue());
250-
}
251-
252-
StringBuilder rowStr = new StringBuilder();
253-
for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) {
254-
rowStr.append(entry.getKey());
255-
rowStr.append(" => ");
256-
rowStr.append(utf8(entry.getValue().value.array()));
257-
rowStr.append("; ");
258-
}
259-
System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr);
232+
ClientUtils.printRow(rowResult);
260233
}
261234

262235
static Subject getSubject() throws Exception {

0 commit comments

Comments
 (0)