HBASE-22203 Reformatted DemoClient.java

This commit is contained in:
Jan Hentschel 2019-04-10 17:16:03 +02:00
parent cb2ef7a606
commit 5e5f0d241d
No known key found for this signature in database
GPG Key ID: 5CD818B19CC299A3
2 changed files with 299 additions and 277 deletions

View File

@ -42,4 +42,5 @@
<suppress checks="MethodLength" files="Branch1CoprocessorMethods.java"/> <suppress checks="MethodLength" files="Branch1CoprocessorMethods.java"/>
<suppress checks="IllegalImport" message="org\.apache\.htrace\.core"/> <suppress checks="IllegalImport" message="org\.apache\.htrace\.core"/>
<suppress checks="ImportOrder" message="Extra separation in import group before"/> <suppress checks="ImportOrder" message="Extra separation in import group before"/>
<suppress checks="MethodLength" files="DemoClient.java"/>
</suppressions> </suppressions>

View File

@ -55,60 +55,60 @@ import org.apache.yetus.audience.InterfaceAudience;
@InterfaceAudience.Private @InterfaceAudience.Private
public class DemoClient { public class DemoClient {
static protected int port; static protected int port;
static protected String host; static protected String host;
CharsetDecoder decoder = null; CharsetDecoder decoder = null;
private static boolean secure = false; private static boolean secure = false;
private static String serverPrincipal = "hbase"; private static String serverPrincipal = "hbase";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
if (args.length < 2 || args.length > 4 || (args.length > 2 && !isBoolean(args[2]))) {
System.out.println("Invalid arguments!");
System.out.println("Usage: DemoClient host port [secure=false [server-principal=hbase] ]");
if (args.length < 2 || args.length > 4 || (args.length > 2 && !isBoolean(args[2]))) { System.exit(-1);
System.out.println("Invalid arguments!");
System.out.println("Usage: DemoClient host port [secure=false [server-principal=hbase] ]");
System.exit(-1);
}
port = Integer.parseInt(args[1]);
host = args[0];
if (args.length > 2) {
secure = Boolean.parseBoolean(args[2]);
}
if (args.length == 4) {
serverPrincipal = args[3];
}
final DemoClient client = new DemoClient();
Subject.doAs(getSubject(),
new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
client.run();
return null;
}
});
} }
private static boolean isBoolean(String s){ port = Integer.parseInt(args[1]);
return Boolean.TRUE.toString().equalsIgnoreCase(s) || Boolean.FALSE.toString().equalsIgnoreCase(s); host = args[0];
if (args.length > 2) {
secure = Boolean.parseBoolean(args[2]);
} }
DemoClient() { if (args.length == 4) {
decoder = Charset.forName("UTF-8").newDecoder(); serverPrincipal = args[3];
} }
// Helper to translate byte[]'s to UTF8 strings final DemoClient client = new DemoClient();
private String utf8(byte[] buf) { Subject.doAs(getSubject(),
try { new PrivilegedExceptionAction<Void>() {
return decoder.decode(ByteBuffer.wrap(buf)).toString(); @Override
} catch (CharacterCodingException e) { public Void run() throws Exception {
return "[INVALID UTF-8]"; client.run();
return null;
} }
});
}
private static boolean isBoolean(String s){
return Boolean.TRUE.toString().equalsIgnoreCase(s) ||
Boolean.FALSE.toString().equalsIgnoreCase(s);
}
DemoClient() {
decoder = Charset.forName("UTF-8").newDecoder();
}
// Helper to translate byte[]'s to UTF8 strings
private String utf8(byte[] buf) {
try {
return decoder.decode(ByteBuffer.wrap(buf)).toString();
} catch (CharacterCodingException e) {
return "[INVALID UTF-8]";
} }
}
// Helper to translate strings to UTF8 bytes // Helper to translate strings to UTF8 bytes
private byte[] bytes(String s) { private byte[] bytes(String s) {
@ -120,271 +120,289 @@ public class DemoClient {
} }
} }
private void run() throws Exception { private void run() throws Exception {
TTransport transport = new TSocket(host, port); TTransport transport = new TSocket(host, port);
if (secure) { if (secure) {
Map<String, String> saslProperties = new HashMap<>(); Map<String, String> saslProperties = new HashMap<>();
saslProperties.put(Sasl.QOP, "auth-conf,auth-int,auth"); saslProperties.put(Sasl.QOP, "auth-conf,auth-int,auth");
/** /*
* The Thrift server the DemoClient is trying to connect to * The Thrift server the DemoClient is trying to connect to
* must have a matching principal, and support authentication. * must have a matching principal, and support authentication.
* *
* The HBase cluster must be secure, allow proxy user. * The HBase cluster must be secure, allow proxy user.
*/ */
transport = new TSaslClientTransport("GSSAPI", null, transport = new TSaslClientTransport("GSSAPI", null,
serverPrincipal, // Thrift server user name, should be an authorized proxy user. serverPrincipal, // Thrift server user name, should be an authorized proxy user.
host, // Thrift server domain host, // Thrift server domain
saslProperties, null, transport); saslProperties, null, transport);
}
transport.open();
TProtocol protocol = new TBinaryProtocol(transport, true, true);
Hbase.Client client = new Hbase.Client(protocol);
byte[] t = bytes("demo_table");
// Scan all tables, look for the demo table and delete it.
System.out.println("scanning tables...");
for (ByteBuffer name : client.getTableNames()) {
System.out.println(" found: " + utf8(name.array()));
if (utf8(name.array()).equals(utf8(t))) {
if (client.isTableEnabled(name)) {
System.out.println(" disabling table: " + utf8(name.array()));
client.disableTable(name);
} }
transport.open(); System.out.println(" deleting table: " + utf8(name.array()));
client.deleteTable(name);
}
}
TProtocol protocol = new TBinaryProtocol(transport, true, true); // Create the demo table with two column families, entry: and unused:
Hbase.Client client = new Hbase.Client(protocol); ArrayList<ColumnDescriptor> columns = new ArrayList<>(2);
ColumnDescriptor col;
col = new ColumnDescriptor();
col.name = ByteBuffer.wrap(bytes("entry:"));
col.timeToLive = Integer.MAX_VALUE;
col.maxVersions = 10;
columns.add(col);
col = new ColumnDescriptor();
col.name = ByteBuffer.wrap(bytes("unused:"));
col.timeToLive = Integer.MAX_VALUE;
columns.add(col);
byte[] t = bytes("demo_table"); System.out.println("creating table: " + utf8(t));
// try {
// Scan all tables, look for the demo table and delete it. client.createTable(ByteBuffer.wrap(t), columns);
// } catch (AlreadyExists ae) {
System.out.println("scanning tables..."); System.out.println("WARN: " + ae.message);
for (ByteBuffer name : client.getTableNames()) { }
System.out.println(" found: " + utf8(name.array()));
if (utf8(name.array()).equals(utf8(t))) {
if (client.isTableEnabled(name)) {
System.out.println(" disabling table: " + utf8(name.array()));
client.disableTable(name);
}
System.out.println(" deleting table: " + utf8(name.array()));
client.deleteTable(name);
}
}
// System.out.println("column families in " + utf8(t) + ": ");
// Create the demo table with two column families, entry: and unused: Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t));
//
ArrayList<ColumnDescriptor> columns = new ArrayList<>(2);
ColumnDescriptor col;
col = new ColumnDescriptor();
col.name = ByteBuffer.wrap(bytes("entry:"));
col.timeToLive = Integer.MAX_VALUE;
col.maxVersions = 10;
columns.add(col);
col = new ColumnDescriptor();
col.name = ByteBuffer.wrap(bytes("unused:"));
col.timeToLive = Integer.MAX_VALUE;
columns.add(col);
System.out.println("creating table: " + utf8(t)); for (ColumnDescriptor col2 : columnMap.values()) {
try { System.out.println(" column: " + utf8(col2.name.array()) + ", maxVer: " + col2.maxVersions);
client.createTable(ByteBuffer.wrap(t), columns); }
} catch (AlreadyExists ae) {
System.out.println("WARN: " + ae.message);
}
System.out.println("column families in " + utf8(t) + ": "); Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t)); boolean writeToWal = false;
for (ColumnDescriptor col2 : columnMap.values()) {
System.out.println(" column: " + utf8(col2.name.array()) + ", maxVer: " + Integer.toString(col2.maxVersions));
}
Map<ByteBuffer, ByteBuffer> dummyAttributes = null; // Test UTF-8 handling
boolean writeToWal = false; byte[] invalid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-',
//
// Test UTF-8 handling
//
byte[] invalid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-',
(byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1}; (byte) 0xfc, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1, (byte) 0xa1};
byte[] valid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', byte[] valid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-',
(byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83, (byte) 0xE7, (byte) 0x94, (byte) 0x9F, (byte) 0xE3, (byte) 0x83,
(byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3, (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, (byte) 0xE3,
(byte) 0x83, (byte) 0xAB}; (byte) 0x83, (byte) 0xAB};
ArrayList<Mutation> mutations; ArrayList<Mutation> mutations;
// non-utf8 is fine for data // non-utf8 is fine for data
mutations = new ArrayList<>(1); mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(invalid), writeToWal)); ByteBuffer.wrap(invalid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("foo")), client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("foo")),
mutations, dummyAttributes); mutations, dummyAttributes);
// this row name is valid utf8
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(valid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes);
// this row name is valid utf8 // non-utf8 is now allowed in row names because HBase stores values as binary
mutations = new ArrayList<>(1); mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(valid), writeToWal)); mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes); ByteBuffer.wrap(invalid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes);
// non-utf8 is now allowed in row names because HBase stores values as binary // Run a scanner on the rows we just created
ArrayList<ByteBuffer> columnNames = new ArrayList<>();
columnNames.add(ByteBuffer.wrap(bytes("entry:")));
mutations = new ArrayList<>(1); System.out.println("Starting scanner...");
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid), writeToWal)); int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames,
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes); dummyAttributes);
while (true) {
List<TRowResult> entry = client.scannerGet(scanner);
// Run a scanner on the rows we just created if (entry.isEmpty()) {
ArrayList<ByteBuffer> columnNames = new ArrayList<>(); break;
columnNames.add(ByteBuffer.wrap(bytes("entry:"))); }
System.out.println("Starting scanner..."); printRow(entry);
int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames, dummyAttributes);
while (true) {
List<TRowResult> entry = client.scannerGet(scanner);
if (entry.isEmpty()) {
break;
}
printRow(entry);
}
//
// Run some operations on a bunch of rows
//
for (int i = 100; i >= 0; --i) {
// format row keys as "00000" to "00100"
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumIntegerDigits(5);
nf.setGroupingUsed(false);
byte[] row = bytes(nf.format(i));
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")), ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes);
// sleep to force later timestamp
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// no-op
}
mutations = new ArrayList<>(2);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes("0")), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(bytes("FOO")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
Mutation m;
mutations = new ArrayList<>(2);
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:foo"));
m.isDelete = true;
mutations.add(m);
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:num"));
m.value = ByteBuffer.wrap(bytes("-1"));
mutations.add(m);
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
mutations = new ArrayList<>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")), ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
// sleep to force later timestamp
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// no-op
}
mutations.clear();
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:num"));
m.value= ByteBuffer.wrap(bytes("-999"));
mutations.add(m);
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:sqr"));
m.isDelete = true;
client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1, dummyAttributes); // shouldn't override latest
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
List<TCell> versions = client.getVer(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
printVersions(ByteBuffer.wrap(row), versions);
if (versions.isEmpty()) {
System.out.println("FATAL: wrong # of versions");
System.exit(-1);
}
List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
if (!result.isEmpty()) {
System.out.println("FATAL: shouldn't get here");
System.exit(-1);
}
System.out.println("");
}
// scan all rows/columnNames
columnNames.clear();
for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) {
System.out.println("column with name: " + new String(col2.name.array()));
System.out.println(col2.toString());
columnNames.add(col2.name);
}
System.out.println("Starting scanner...");
scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")), ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
while (true) {
List<TRowResult> entry = client.scannerGet(scanner);
if (entry.isEmpty()) {
System.out.println("Scanner finished");
break;
}
printRow(entry);
}
transport.close();
} }
private void printVersions(ByteBuffer row, List<TCell> versions) { // Run some operations on a bunch of rows
StringBuilder rowStr = new StringBuilder(); for (int i = 100; i >= 0; --i) {
for (TCell cell : versions) { // format row keys as "00000" to "00100"
rowStr.append(utf8(cell.value.array())); NumberFormat nf = NumberFormat.getInstance();
rowStr.append("; "); nf.setMinimumIntegerDigits(5);
} nf.setGroupingUsed(false);
System.out.println("row: " + utf8(row.array()) + ", values: " + rowStr); byte[] row = bytes(nf.format(i));
mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")),
ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes);
// sleep to force later timestamp
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// no-op
}
mutations = new ArrayList<>(2);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")),
ByteBuffer.wrap(bytes("0")), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(bytes("FOO")), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
Mutation m;
mutations = new ArrayList<>(2);
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:foo"));
m.isDelete = true;
mutations.add(m);
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:num"));
m.value = ByteBuffer.wrap(bytes("-1"));
mutations.add(m);
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
mutations = new ArrayList<>();
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")),
ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")),
ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
// sleep to force later timestamp
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// no-op
}
mutations.clear();
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:num"));
m.value= ByteBuffer.wrap(bytes("-999"));
mutations.add(m);
m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:sqr"));
m.isDelete = true;
client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1,
dummyAttributes); // shouldn't override latest
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
List<TCell> versions = client.getVer(ByteBuffer.wrap(t), ByteBuffer.wrap(row),
ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
printVersions(ByteBuffer.wrap(row), versions);
if (versions.isEmpty()) {
System.out.println("FATAL: wrong # of versions");
System.exit(-1);
}
List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row),
ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
if (!result.isEmpty()) {
System.out.println("FATAL: shouldn't get here");
System.exit(-1);
}
System.out.println("");
} }
private void printRow(TRowResult rowResult) { // scan all rows/columnNames
// copy values into a TreeMap to get them in sorted order columnNames.clear();
TreeMap<String, TCell> sorted = new TreeMap<>(); for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) {
for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) { System.out.println("column with name: " + new String(col2.name.array()));
sorted.put(utf8(column.getKey().array()), column.getValue()); System.out.println(col2.toString());
}
StringBuilder rowStr = new StringBuilder(); columnNames.add(col2.name);
for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) {
rowStr.append(entry.getKey());
rowStr.append(" => ");
rowStr.append(utf8(entry.getValue().value.array()));
rowStr.append("; ");
}
System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr);
} }
private void printRow(List<TRowResult> rows) { System.out.println("Starting scanner...");
for (TRowResult rowResult : rows) { scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")),
printRow(rowResult); ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
}
while (true) {
List<TRowResult> entry = client.scannerGet(scanner);
if (entry.isEmpty()) {
System.out.println("Scanner finished");
break;
}
printRow(entry);
} }
static Subject getSubject() throws Exception { transport.close();
if (!secure) return new Subject(); }
/* private void printVersions(ByteBuffer row, List<TCell> versions) {
* To authenticate the DemoClient, kinit should be invoked ahead. StringBuilder rowStr = new StringBuilder();
* Here we try to get the Kerberos credential from the ticket cache.
*/ for (TCell cell : versions) {
LoginContext context = new LoginContext("", new Subject(), null, rowStr.append(utf8(cell.value.array()));
rowStr.append("; ");
}
System.out.println("row: " + utf8(row.array()) + ", values: " + rowStr);
}
private void printRow(TRowResult rowResult) {
// copy values into a TreeMap to get them in sorted order
TreeMap<String, TCell> sorted = new TreeMap<>();
for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) {
sorted.put(utf8(column.getKey().array()), column.getValue());
}
StringBuilder rowStr = new StringBuilder();
for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) {
rowStr.append(entry.getKey());
rowStr.append(" => ");
rowStr.append(utf8(entry.getValue().value.array()));
rowStr.append("; ");
}
System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr);
}
private void printRow(List<TRowResult> rows) {
for (TRowResult rowResult : rows) {
printRow(rowResult);
}
}
static Subject getSubject() throws Exception {
if (!secure) {
return new Subject();
}
/*
* To authenticate the DemoClient, kinit should be invoked ahead.
* Here we try to get the Kerberos credential from the ticket cache.
*/
LoginContext context = new LoginContext("", new Subject(), null,
new Configuration() { new Configuration() {
@Override @Override
public AppConfigurationEntry[] getAppConfigurationEntry(String name) { public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
@ -397,9 +415,11 @@ public class DemoClient {
options.put("refreshKrb5Config", "true"); options.put("refreshKrb5Config", "true");
options.put("isInitiator", "true"); options.put("isInitiator", "true");
String ticketCache = System.getenv("KRB5CCNAME"); String ticketCache = System.getenv("KRB5CCNAME");
if (ticketCache != null) { if (ticketCache != null) {
options.put("ticketCache", ticketCache); options.put("ticketCache", ticketCache);
} }
options.put("debug", "true"); options.put("debug", "true");
return new AppConfigurationEntry[]{ return new AppConfigurationEntry[]{
@ -408,7 +428,8 @@ public class DemoClient {
options)}; options)};
} }
}); });
context.login();
return context.getSubject(); context.login();
} return context.getSubject();
}
} }