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

@ -63,9 +63,7 @@ public class DemoClient {
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]))) { if (args.length < 2 || args.length > 4 || (args.length > 2 && !isBoolean(args[2]))) {
System.out.println("Invalid arguments!"); System.out.println("Invalid arguments!");
System.out.println("Usage: DemoClient host port [secure=false [server-principal=hbase] ]"); System.out.println("Usage: DemoClient host port [secure=false [server-principal=hbase] ]");
@ -74,6 +72,7 @@ public class DemoClient {
port = Integer.parseInt(args[1]); port = Integer.parseInt(args[1]);
host = args[0]; host = args[0];
if (args.length > 2) { if (args.length > 2) {
secure = Boolean.parseBoolean(args[2]); secure = Boolean.parseBoolean(args[2]);
} }
@ -94,7 +93,8 @@ public class DemoClient {
} }
private static boolean isBoolean(String s){ private static boolean isBoolean(String s){
return Boolean.TRUE.toString().equalsIgnoreCase(s) || Boolean.FALSE.toString().equalsIgnoreCase(s); return Boolean.TRUE.toString().equalsIgnoreCase(s) ||
Boolean.FALSE.toString().equalsIgnoreCase(s);
} }
DemoClient() { DemoClient() {
@ -125,7 +125,7 @@ public class DemoClient {
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.
* *
@ -144,25 +144,24 @@ public class DemoClient {
byte[] t = bytes("demo_table"); byte[] t = bytes("demo_table");
//
// Scan all tables, look for the demo table and delete it. // Scan all tables, look for the demo table and delete it.
//
System.out.println("scanning tables..."); System.out.println("scanning tables...");
for (ByteBuffer name : client.getTableNames()) { for (ByteBuffer name : client.getTableNames()) {
System.out.println(" found: " + utf8(name.array())); System.out.println(" found: " + utf8(name.array()));
if (utf8(name.array()).equals(utf8(t))) { if (utf8(name.array()).equals(utf8(t))) {
if (client.isTableEnabled(name)) { if (client.isTableEnabled(name)) {
System.out.println(" disabling table: " + utf8(name.array())); System.out.println(" disabling table: " + utf8(name.array()));
client.disableTable(name); client.disableTable(name);
} }
System.out.println(" deleting table: " + utf8(name.array())); System.out.println(" deleting table: " + utf8(name.array()));
client.deleteTable(name); client.deleteTable(name);
} }
} }
//
// Create the demo table with two column families, entry: and unused: // Create the demo table with two column families, entry: and unused:
//
ArrayList<ColumnDescriptor> columns = new ArrayList<>(2); ArrayList<ColumnDescriptor> columns = new ArrayList<>(2);
ColumnDescriptor col; ColumnDescriptor col;
col = new ColumnDescriptor(); col = new ColumnDescriptor();
@ -176,6 +175,7 @@ public class DemoClient {
columns.add(col); columns.add(col);
System.out.println("creating table: " + utf8(t)); System.out.println("creating table: " + utf8(t));
try { try {
client.createTable(ByteBuffer.wrap(t), columns); client.createTable(ByteBuffer.wrap(t), columns);
} catch (AlreadyExists ae) { } catch (AlreadyExists ae) {
@ -184,16 +184,15 @@ public class DemoClient {
System.out.println("column families in " + utf8(t) + ": "); System.out.println("column families in " + utf8(t) + ": ");
Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t)); Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(ByteBuffer.wrap(t));
for (ColumnDescriptor col2 : columnMap.values()) { for (ColumnDescriptor col2 : columnMap.values()) {
System.out.println(" column: " + utf8(col2.name.array()) + ", maxVer: " + Integer.toString(col2.maxVersions)); System.out.println(" column: " + utf8(col2.name.array()) + ", maxVer: " + col2.maxVersions);
} }
Map<ByteBuffer, ByteBuffer> dummyAttributes = null; Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
boolean writeToWal = false; boolean writeToWal = false;
//
// Test UTF-8 handling // Test UTF-8 handling
//
byte[] invalid = {(byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', 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) '-',
@ -209,37 +208,37 @@ public class DemoClient {
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 // this row name is valid utf8
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")),
ByteBuffer.wrap(valid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes); client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(valid), mutations, dummyAttributes);
// non-utf8 is now allowed in row names because HBase stores values as binary // 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(invalid), writeToWal)); mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
ByteBuffer.wrap(invalid), writeToWal));
client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes); client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(invalid), mutations, dummyAttributes);
// Run a scanner on the rows we just created // Run a scanner on the rows we just created
ArrayList<ByteBuffer> columnNames = new ArrayList<>(); ArrayList<ByteBuffer> columnNames = new ArrayList<>();
columnNames.add(ByteBuffer.wrap(bytes("entry:"))); columnNames.add(ByteBuffer.wrap(bytes("entry:")));
System.out.println("Starting scanner..."); System.out.println("Starting scanner...");
int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames, dummyAttributes); int scanner = client.scannerOpen(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("")), columnNames,
dummyAttributes);
while (true) { while (true) {
List<TRowResult> entry = client.scannerGet(scanner); List<TRowResult> entry = client.scannerGet(scanner);
if (entry.isEmpty()) { if (entry.isEmpty()) {
break; break;
} }
printRow(entry); printRow(entry);
} }
//
// Run some operations on a bunch of rows // Run some operations on a bunch of rows
//
for (int i = 100; i >= 0; --i) { for (int i = 100; i >= 0; --i) {
// format row keys as "00000" to "00100" // format row keys as "00000" to "00100"
NumberFormat nf = NumberFormat.getInstance(); NumberFormat nf = NumberFormat.getInstance();
@ -248,7 +247,8 @@ public class DemoClient {
byte[] row = bytes(nf.format(i)); byte[] row = bytes(nf.format(i));
mutations = new ArrayList<>(1); mutations = new ArrayList<>(1);
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")), ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal)); 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); client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes)); printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes); client.deleteAllRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes);
@ -261,8 +261,10 @@ public class DemoClient {
} }
mutations = new ArrayList<>(2); 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:num")),
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(bytes("FOO")), writeToWal)); 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); client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes)); printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
@ -280,8 +282,10 @@ public class DemoClient {
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes)); printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
mutations = new ArrayList<>(); 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:num")),
mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")), ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal)); 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); client.mutateRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, dummyAttributes);
printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes)); printRow(client.getRow(ByteBuffer.wrap(t), ByteBuffer.wrap(row), dummyAttributes));
@ -300,17 +304,22 @@ public class DemoClient {
m = new Mutation(); m = new Mutation();
m.column = ByteBuffer.wrap(bytes("entry:sqr")); m.column = ByteBuffer.wrap(bytes("entry:sqr"));
m.isDelete = true; m.isDelete = true;
client.mutateRowTs(ByteBuffer.wrap(t), ByteBuffer.wrap(row), mutations, 1, dummyAttributes); // shouldn't override latest 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)); 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); List<TCell> versions = client.getVer(ByteBuffer.wrap(t), ByteBuffer.wrap(row),
ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
printVersions(ByteBuffer.wrap(row), versions); printVersions(ByteBuffer.wrap(row), versions);
if (versions.isEmpty()) { if (versions.isEmpty()) {
System.out.println("FATAL: wrong # of versions"); System.out.println("FATAL: wrong # of versions");
System.exit(-1); System.exit(-1);
} }
List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row), ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes); List<TCell> result = client.get(ByteBuffer.wrap(t), ByteBuffer.wrap(row),
ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
if (!result.isEmpty()) { if (!result.isEmpty()) {
System.out.println("FATAL: shouldn't get here"); System.out.println("FATAL: shouldn't get here");
System.exit(-1); System.exit(-1);
@ -320,8 +329,8 @@ public class DemoClient {
} }
// scan all rows/columnNames // scan all rows/columnNames
columnNames.clear(); columnNames.clear();
for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) { for (ColumnDescriptor col2 : client.getColumnDescriptors(ByteBuffer.wrap(t)).values()) {
System.out.println("column with name: " + new String(col2.name.array())); System.out.println("column with name: " + new String(col2.name.array()));
System.out.println(col2.toString()); System.out.println(col2.toString());
@ -330,14 +339,17 @@ public class DemoClient {
} }
System.out.println("Starting scanner..."); System.out.println("Starting scanner...");
scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")), ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes); scanner = client.scannerOpenWithStop(ByteBuffer.wrap(t), ByteBuffer.wrap(bytes("00020")),
ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
while (true) { while (true) {
List<TRowResult> entry = client.scannerGet(scanner); List<TRowResult> entry = client.scannerGet(scanner);
if (entry.isEmpty()) { if (entry.isEmpty()) {
System.out.println("Scanner finished"); System.out.println("Scanner finished");
break; break;
} }
printRow(entry); printRow(entry);
} }
@ -346,28 +358,32 @@ public class DemoClient {
private void printVersions(ByteBuffer row, List<TCell> versions) { private void printVersions(ByteBuffer row, List<TCell> versions) {
StringBuilder rowStr = new StringBuilder(); StringBuilder rowStr = new StringBuilder();
for (TCell cell : versions) { for (TCell cell : versions) {
rowStr.append(utf8(cell.value.array())); rowStr.append(utf8(cell.value.array()));
rowStr.append("; "); rowStr.append("; ");
} }
System.out.println("row: " + utf8(row.array()) + ", values: " + rowStr); System.out.println("row: " + utf8(row.array()) + ", values: " + rowStr);
} }
private void printRow(TRowResult rowResult) { private void printRow(TRowResult rowResult) {
// copy values into a TreeMap to get them in sorted order // copy values into a TreeMap to get them in sorted order
TreeMap<String, TCell> sorted = new TreeMap<>(); TreeMap<String, TCell> sorted = new TreeMap<>();
for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) { for (Map.Entry<ByteBuffer, TCell> column : rowResult.columns.entrySet()) {
sorted.put(utf8(column.getKey().array()), column.getValue()); sorted.put(utf8(column.getKey().array()), column.getValue());
} }
StringBuilder rowStr = new StringBuilder(); StringBuilder rowStr = new StringBuilder();
for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) { for (SortedMap.Entry<String, TCell> entry : sorted.entrySet()) {
rowStr.append(entry.getKey()); rowStr.append(entry.getKey());
rowStr.append(" => "); rowStr.append(" => ");
rowStr.append(utf8(entry.getValue().value.array())); rowStr.append(utf8(entry.getValue().value.array()));
rowStr.append("; "); rowStr.append("; ");
} }
System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr); System.out.println("row: " + utf8(rowResult.row.array()) + ", cols: " + rowStr);
} }
@ -378,7 +394,9 @@ public class DemoClient {
} }
static Subject getSubject() throws Exception { static Subject getSubject() throws Exception {
if (!secure) return new Subject(); if (!secure) {
return new Subject();
}
/* /*
* To authenticate the DemoClient, kinit should be invoked ahead. * To authenticate the DemoClient, kinit should be invoked ahead.
@ -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,6 +428,7 @@ public class DemoClient {
options)}; options)};
} }
}); });
context.login(); context.login();
return context.getSubject(); return context.getSubject();
} }