Skip to content

Commit 31c99ff

Browse files
virajjasaniopeninx
authored andcommitted
HBASE-22743 : ClientUtils for Demo Client classes (#413)
1 parent e54c401 commit 31c99ff

File tree

5 files changed

+153
-177
lines changed

5 files changed

+153
-177
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: 14 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,13 @@
2020

2121
import java.io.UnsupportedEncodingException;
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.PrivilegedExceptionAction;
2724
import java.text.NumberFormat;
2825
import java.util.ArrayList;
2926
import java.util.HashMap;
3027
import java.util.List;
3128
import java.util.Map;
32-
import java.util.SortedMap;
33-
import java.util.TreeMap;
3429
import javax.security.auth.Subject;
35-
import javax.security.auth.login.AppConfigurationEntry;
36-
import javax.security.auth.login.Configuration;
3730
import javax.security.auth.login.LoginContext;
3831
import javax.security.sasl.Sasl;
3932
import org.apache.hadoop.hbase.thrift.generated.AlreadyExists;
@@ -42,6 +35,8 @@
4235
import org.apache.hadoop.hbase.thrift.generated.Mutation;
4336
import org.apache.hadoop.hbase.thrift.generated.TCell;
4437
import org.apache.hadoop.hbase.thrift.generated.TRowResult;
38+
import org.apache.hadoop.hbase.util.Bytes;
39+
import org.apache.hadoop.hbase.util.ClientUtils;
4540
import org.apache.thrift.protocol.TBinaryProtocol;
4641
import org.apache.thrift.protocol.TProtocol;
4742
import org.apache.thrift.transport.TSaslClientTransport;
@@ -57,7 +52,6 @@ public class DemoClient {
5752

5853
static protected int port;
5954
static protected String host;
60-
CharsetDecoder decoder = null;
6155

6256
private static boolean secure = false;
6357
private static String serverPrincipal = "hbase";
@@ -98,16 +92,6 @@ private static boolean isBoolean(String s){
9892
}
9993

10094
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-
}
11195
}
11296

11397
// Helper to translate strings to UTF8 bytes
@@ -148,15 +132,15 @@ private void run() throws Exception {
148132
System.out.println("scanning tables...");
149133

150134
for (ByteBuffer name : client.getTableNames()) {
151-
System.out.println(" found: " + utf8(name.array()));
135+
System.out.println(" found: " + ClientUtils.utf8(name.array()));
152136

153-
if (utf8(name.array()).equals(utf8(t))) {
137+
if (ClientUtils.utf8(name.array()).equals(ClientUtils.utf8(t))) {
154138
if (client.isTableEnabled(name)) {
155-
System.out.println(" disabling table: " + utf8(name.array()));
139+
System.out.println(" disabling table: " + ClientUtils.utf8(name.array()));
156140
client.disableTable(name);
157141
}
158142

159-
System.out.println(" deleting table: " + utf8(name.array()));
143+
System.out.println(" deleting table: " + ClientUtils.utf8(name.array()));
160144
client.deleteTable(name);
161145
}
162146
}
@@ -174,19 +158,20 @@ private void run() throws Exception {
174158
col.timeToLive = Integer.MAX_VALUE;
175159
columns.add(col);
176160

177-
System.out.println("creating table: " + utf8(t));
161+
System.out.println("creating table: " + ClientUtils.utf8(t));
178162

179163
try {
180164
client.createTable(ByteBuffer.wrap(t), columns);
181165
} catch (AlreadyExists ae) {
182166
System.out.println("WARN: " + ae.message);
183167
}
184168

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

188172
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);
190175
}
191176

192177
Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
@@ -360,31 +345,15 @@ private void printVersions(ByteBuffer row, List<TCell> versions) {
360345
StringBuilder rowStr = new StringBuilder();
361346

362347
for (TCell cell : versions) {
363-
rowStr.append(utf8(cell.value.array()));
348+
rowStr.append(ClientUtils.utf8(cell.value.array()));
364349
rowStr.append("; ");
365350
}
366351

367-
System.out.println("row: " + utf8(row.array()) + ", values: " + rowStr);
352+
System.out.println("row: " + ClientUtils.utf8(row.array()) + ", values: " + rowStr);
368353
}
369354

370355
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);
388357
}
389358

390359
private void printRow(List<TRowResult> rows) {
@@ -397,38 +366,7 @@ static Subject getSubject() throws Exception {
397366
if (!secure) {
398367
return new Subject();
399368
}
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();
432370
context.login();
433371
return context.getSubject();
434372
}

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

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@
2020

2121
import java.io.UnsupportedEncodingException;
2222
import java.nio.ByteBuffer;
23-
import java.nio.charset.CharacterCodingException;
24-
import java.nio.charset.Charset;
25-
import java.nio.charset.CharsetDecoder;
23+
import java.security.Principal;
2624
import java.security.PrivilegedExceptionAction;
2725
import java.util.ArrayList;
2826
import java.util.Base64;
2927
import java.util.HashMap;
3028
import java.util.List;
3129
import java.util.Map;
32-
import java.util.SortedMap;
33-
import java.util.TreeMap;
30+
import java.util.Set;
3431
import javax.security.auth.Subject;
3532
import javax.security.auth.login.AppConfigurationEntry;
3633
import javax.security.auth.login.Configuration;
@@ -41,6 +38,7 @@
4138
import org.apache.hadoop.hbase.thrift.generated.TCell;
4239
import org.apache.hadoop.hbase.thrift.generated.TRowResult;
4340
import org.apache.hadoop.hbase.util.Bytes;
41+
import org.apache.hadoop.hbase.util.ClientUtils;
4442
import org.apache.thrift.protocol.TBinaryProtocol;
4543
import org.apache.thrift.protocol.TProtocol;
4644
import org.apache.thrift.transport.THttpClient;
@@ -62,7 +60,6 @@ public class HttpDoAsClient {
6260

6361
static protected int port;
6462
static protected String host;
65-
CharsetDecoder decoder = null;
6663
private static boolean secure = false;
6764
static protected String doAsUser = null;
6865
static protected String principal = null;
@@ -96,16 +93,6 @@ public Void run() throws Exception {
9693
}
9794

9895
HttpDoAsClient() {
99-
decoder = Charset.forName("UTF-8").newDecoder();
100-
}
101-
102-
// Helper to translate byte[]'s to UTF8 strings
103-
private String utf8(byte[] buf) {
104-
try {
105-
return decoder.decode(ByteBuffer.wrap(buf)).toString();
106-
} catch (CharacterCodingException e) {
107-
return "[INVALID UTF-8]";
108-
}
10996
}
11097

11198
// Helper to translate strings to UTF8 bytes
@@ -135,13 +122,13 @@ private void run() throws Exception {
135122
//
136123
System.out.println("scanning tables...");
137124
for (ByteBuffer name : refresh(client, httpClient).getTableNames()) {
138-
System.out.println(" found: " + utf8(name.array()));
139-
if (utf8(name.array()).equals(utf8(t))) {
125+
System.out.println(" found: " + ClientUtils.utf8(name.array()));
126+
if (ClientUtils.utf8(name.array()).equals(ClientUtils.utf8(t))) {
140127
if (refresh(client, httpClient).isTableEnabled(name)) {
141-
System.out.println(" disabling table: " + utf8(name.array()));
128+
System.out.println(" disabling table: " + ClientUtils.utf8(name.array()));
142129
refresh(client, httpClient).disableTable(name);
143130
}
144-
System.out.println(" deleting table: " + utf8(name.array()));
131+
System.out.println(" deleting table: " + ClientUtils.utf8(name.array()));
145132
refresh(client, httpClient).deleteTable(name);
146133
}
147134
}
@@ -163,19 +150,20 @@ private void run() throws Exception {
163150
col.timeToLive = Integer.MAX_VALUE;
164151
columns.add(col);
165152

166-
System.out.println("creating table: " + utf8(t));
153+
System.out.println("creating table: " + ClientUtils.utf8(t));
167154
try {
168155

169156
refresh(client, httpClient).createTable(ByteBuffer.wrap(t), columns);
170157
} catch (AlreadyExists ae) {
171158
System.out.println("WARN: " + ae.message);
172159
}
173160

174-
System.out.println("column families in " + utf8(t) + ": ");
161+
System.out.println("column families in " + ClientUtils.utf8(t) + ": ");
175162
Map<ByteBuffer, ColumnDescriptor> columnMap = refresh(client, httpClient)
176163
.getColumnDescriptors(ByteBuffer.wrap(t));
177164
for (ColumnDescriptor col2 : columnMap.values()) {
178-
System.out.println(" column: " + utf8(col2.name.array()) + ", maxVer: " + Integer.toString(col2.maxVersions));
165+
System.out.println(" column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: "
166+
+ col2.maxVersions);
179167
}
180168

181169
transport.close();
@@ -227,28 +215,14 @@ private String generateTicket() throws GSSException {
227215
private void printVersions(ByteBuffer row, List<TCell> versions) {
228216
StringBuilder rowStr = new StringBuilder();
229217
for (TCell cell : versions) {
230-
rowStr.append(utf8(cell.value.array()));
218+
rowStr.append(ClientUtils.utf8(cell.value.array()));
231219
rowStr.append("; ");
232220
}
233-
System.out.println("row: " + utf8(row.array()) + ", values: " + rowStr);
221+
System.out.println("row: " + ClientUtils.utf8(row.array()) + ", values: " + rowStr);
234222
}
235223

236224
private void printRow(TRowResult rowResult) {
237-
// copy values into a TreeMap to get them in sorted order
238-
239-
TreeMap<String, TCell> sorted = new TreeMap<>();
240-
for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) {
241-
sorted.put(utf8(column.getKey().array()), column.getValue());
242-
}
243-
244-
StringBuilder rowStr = new StringBuilder();
245-
for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) {
246-
rowStr.append(entry.getKey());
247-
rowStr.append(" => ");
248-
rowStr.append(utf8(entry.getValue().value.array()));
249-
rowStr.append("; ");
250-
}
251-
System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr);
225+
ClientUtils.printRow(rowResult);
252226
}
253227

254228
static Subject getSubject() throws Exception {

0 commit comments

Comments
 (0)