001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.thrift; 019 020import java.nio.ByteBuffer; 021import java.security.PrivilegedExceptionAction; 022import java.text.NumberFormat; 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027import javax.security.auth.Subject; 028import javax.security.auth.login.LoginContext; 029import javax.security.sasl.Sasl; 030import org.apache.hadoop.hbase.thrift.generated.AlreadyExists; 031import org.apache.hadoop.hbase.thrift.generated.ColumnDescriptor; 032import org.apache.hadoop.hbase.thrift.generated.Hbase; 033import org.apache.hadoop.hbase.thrift.generated.Mutation; 034import org.apache.hadoop.hbase.thrift.generated.TCell; 035import org.apache.hadoop.hbase.thrift.generated.TRowResult; 036import org.apache.hadoop.hbase.util.Bytes; 037import org.apache.hadoop.hbase.util.ClientUtils; 038import org.apache.thrift.protocol.TBinaryProtocol; 039import org.apache.thrift.protocol.TProtocol; 040import org.apache.thrift.transport.TSaslClientTransport; 041import org.apache.thrift.transport.TSocket; 042import org.apache.thrift.transport.TTransport; 043import org.apache.yetus.audience.InterfaceAudience; 044 045/** 046 * See the instructions under hbase-examples/README.txt 047 */ 048@InterfaceAudience.Private 049public class DemoClient { 050 051 static protected int port; 052 static protected String host; 053 054 private static boolean secure = false; 055 private static String serverPrincipal = "hbase"; 056 057 public static void main(String[] args) throws Exception { 058 if (args.length < 2 || args.length > 4 || (args.length > 2 && !isBoolean(args[2]))) { 059 System.out.println("Invalid arguments!"); 060 System.out.println("Usage: DemoClient host port [secure=false [server-principal=hbase] ]"); 061 062 System.exit(-1); 063 } 064 065 port = Integer.parseInt(args[1]); 066 host = args[0]; 067 068 if (args.length > 2) { 069 secure = Boolean.parseBoolean(args[2]); 070 } 071 072 if (args.length == 4) { 073 serverPrincipal = args[3]; 074 } 075 076 final DemoClient client = new DemoClient(); 077 Subject.doAs(getSubject(), new PrivilegedExceptionAction<Void>() { 078 @Override 079 public Void run() throws Exception { 080 client.run(); 081 return null; 082 } 083 }); 084 } 085 086 private static boolean isBoolean(String s) { 087 return Boolean.TRUE.toString().equalsIgnoreCase(s) 088 || Boolean.FALSE.toString().equalsIgnoreCase(s); 089 } 090 091 DemoClient() { 092 } 093 094 // Helper to translate strings to UTF8 bytes 095 private byte[] bytes(String s) { 096 return Bytes.toBytes(s); 097 } 098 099 private void run() throws Exception { 100 TTransport transport = new TSocket(host, port); 101 if (secure) { 102 Map<String, String> saslProperties = new HashMap<>(); 103 saslProperties.put(Sasl.QOP, "auth-conf,auth-int,auth"); 104 /* 105 * The Thrift server the DemoClient is trying to connect to must have a matching principal, 106 * and support authentication. The HBase cluster must be secure, allow proxy user. 107 */ 108 transport = new TSaslClientTransport("GSSAPI", null, serverPrincipal, // Thrift server user 109 // name, should be an 110 // authorized proxy 111 // user. 112 host, // Thrift server domain 113 saslProperties, null, transport); 114 } 115 116 transport.open(); 117 118 TProtocol protocol = new TBinaryProtocol(transport, true, true); 119 Hbase.Client client = new Hbase.Client(protocol); 120 121 ByteBuffer demoTable = ByteBuffer.wrap(bytes("demo_table")); 122 ByteBuffer disabledTable = ByteBuffer.wrap(bytes("disabled_table")); 123 124 // Scan all tables, look for the demo table and delete it. 125 System.out.println("scanning tables..."); 126 127 for (ByteBuffer name : client.getTableNames()) { 128 System.out.println(" found: " + ClientUtils.utf8(name)); 129 130 if (name.equals(demoTable) || name.equals(disabledTable)) { 131 if (client.isTableEnabled(name)) { 132 System.out.println(" disabling table: " + ClientUtils.utf8(name)); 133 client.disableTable(name); 134 } 135 136 System.out.println(" deleting table: " + ClientUtils.utf8(name)); 137 client.deleteTable(name); 138 } 139 } 140 141 // Create the demo table with two column families, entry: and unused: 142 List<ColumnDescriptor> columns = new ArrayList<>(2); 143 ColumnDescriptor col = new ColumnDescriptor(); 144 col.name = ByteBuffer.wrap(bytes("entry:")); 145 col.timeToLive = Integer.MAX_VALUE; 146 col.maxVersions = 10; 147 columns.add(col); 148 col = new ColumnDescriptor(); 149 col.name = ByteBuffer.wrap(bytes("unused:")); 150 col.timeToLive = Integer.MAX_VALUE; 151 columns.add(col); 152 153 System.out.println("creating table: " + ClientUtils.utf8(demoTable.array())); 154 try { 155 client.createTable(demoTable, columns); 156 client.createTable(disabledTable, columns); 157 } catch (AlreadyExists ae) { 158 System.out.println("WARN: " + ae.message); 159 } 160 161 System.out.println("column families in " + ClientUtils.utf8(demoTable.array()) + ": "); 162 Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(demoTable); 163 for (ColumnDescriptor col2 : columnMap.values()) { 164 System.out.println( 165 " column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: " + col2.maxVersions); 166 } 167 168 if (client.isTableEnabled(disabledTable)) { 169 System.out.println("disabling table: " + ClientUtils.utf8(disabledTable.array())); 170 client.disableTable(disabledTable); 171 } 172 173 System.out.println("list tables with enabled statuses : "); 174 Map<ByteBuffer, Boolean> statusMap = client.getTableNamesWithIsTableEnabled(); 175 for (Map.Entry<ByteBuffer, Boolean> entry : statusMap.entrySet()) { 176 System.out.println(" Table: " + ClientUtils.utf8(entry.getKey().array()) + ", is enabled: " 177 + entry.getValue()); 178 } 179 180 Map<ByteBuffer, ByteBuffer> dummyAttributes = null; 181 boolean writeToWal = false; 182 183 // Test UTF-8 handling 184 byte[] invalid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xfc, (byte) 0xa1, 185 (byte) 0xa1, (byte) 0xa1, (byte) 0xa1 }; 186 byte[] valid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xE7, (byte) 0x94, 187 (byte) 0x9F, (byte) 0xE3, (byte) 0x83, (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC, 188 (byte) 0xE3, (byte) 0x83, (byte) 0xAB }; 189 190 ArrayList<Mutation> mutations; 191 // non-utf8 is fine for data 192 mutations = new ArrayList<>(1); 193 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid), 194 writeToWal)); 195 client.mutateRow(demoTable, ByteBuffer.wrap(bytes("foo")), mutations, dummyAttributes); 196 197 // this row name is valid utf8 198 mutations = new ArrayList<>(1); 199 mutations.add( 200 new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(valid), writeToWal)); 201 client.mutateRow(demoTable, ByteBuffer.wrap(valid), mutations, dummyAttributes); 202 203 // non-utf8 is now allowed in row names because HBase stores values as binary 204 mutations = new ArrayList<>(1); 205 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid), 206 writeToWal)); 207 client.mutateRow(demoTable, ByteBuffer.wrap(invalid), mutations, dummyAttributes); 208 209 // Run a scanner on the rows we just created 210 List<ByteBuffer> columnNames = new ArrayList<>(); 211 columnNames.add(ByteBuffer.wrap(bytes("entry"))); 212 213 System.out.println("Starting scanner..."); 214 int scanner = 215 client.scannerOpen(demoTable, ByteBuffer.wrap(bytes("")), columnNames, dummyAttributes); 216 217 while (true) { 218 List<TRowResult> entry = client.scannerGet(scanner); 219 220 if (entry.isEmpty()) { 221 break; 222 } 223 224 printRow(entry); 225 } 226 System.out.println("Scanner finished..."); 227 228 // Run some operations on a bunch of rows 229 for (int i = 100; i >= 0; --i) { 230 // format row keys as "00000" to "00100" 231 NumberFormat nf = NumberFormat.getInstance(); 232 nf.setMinimumIntegerDigits(5); 233 nf.setGroupingUsed(false); 234 byte[] row = bytes(nf.format(i)); 235 236 mutations = new ArrayList<>(1); 237 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")), 238 ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal)); 239 client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes); 240 printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes)); 241 client.deleteAllRow(demoTable, ByteBuffer.wrap(row), dummyAttributes); 242 243 // sleep to force later timestamp 244 try { 245 Thread.sleep(50); 246 } catch (InterruptedException e) { 247 // no-op 248 } 249 250 mutations = new ArrayList<>(2); 251 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), 252 ByteBuffer.wrap(bytes("0")), writeToWal)); 253 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), 254 ByteBuffer.wrap(bytes("FOO")), writeToWal)); 255 client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes); 256 printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes)); 257 258 mutations = new ArrayList<>(2); 259 Mutation m = new Mutation(); 260 m.column = ByteBuffer.wrap(bytes("entry:foo")); 261 m.isDelete = true; 262 mutations.add(m); 263 m = new Mutation(); 264 m.column = ByteBuffer.wrap(bytes("entry:num")); 265 m.value = ByteBuffer.wrap(bytes("-1")); 266 mutations.add(m); 267 client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes); 268 printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes)); 269 270 mutations = new ArrayList<>(); 271 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")), 272 ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal)); 273 mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")), 274 ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal)); 275 client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes); 276 printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes)); 277 278 // sleep to force later timestamp 279 try { 280 Thread.sleep(50); 281 } catch (InterruptedException e) { 282 // no-op 283 } 284 285 mutations.clear(); 286 m = new Mutation(); 287 m.column = ByteBuffer.wrap(bytes("entry:num")); 288 m.value = ByteBuffer.wrap(bytes("-999")); 289 mutations.add(m); 290 m = new Mutation(); 291 m.column = ByteBuffer.wrap(bytes("entry:sqr")); 292 m.isDelete = true; 293 // shouldn't override latest 294 client.mutateRowTs(demoTable, ByteBuffer.wrap(row), mutations, 1, dummyAttributes); 295 printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes)); 296 297 List<TCell> versions = client.getVer(demoTable, ByteBuffer.wrap(row), 298 ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes); 299 printVersions(ByteBuffer.wrap(row), versions); 300 301 if (versions.isEmpty()) { 302 System.out.println("FATAL: wrong # of versions"); 303 System.exit(-1); 304 } 305 306 List<TCell> result = client.get(demoTable, ByteBuffer.wrap(row), 307 ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes); 308 309 if (!result.isEmpty()) { 310 System.out.println("FATAL: shouldn't get here"); 311 System.exit(-1); 312 } 313 314 System.out.println(); 315 } 316 317 // scan all rows/columnNames 318 columnNames.clear(); 319 320 for (ColumnDescriptor col2 : client.getColumnDescriptors(demoTable).values()) { 321 System.out.println("column with name: " + ClientUtils.utf8(col2.name)); 322 System.out.println(col2); 323 // remove the trailing ':' from the family name 324 col2.name.limit(col2.name.limit() - 1); 325 columnNames.add(col2.name.slice()); 326 } 327 328 System.out.println("Starting scanner..."); 329 scanner = client.scannerOpenWithStop(demoTable, ByteBuffer.wrap(bytes("00020")), 330 ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes); 331 332 while (true) { 333 List<TRowResult> entry = client.scannerGet(scanner); 334 335 if (entry.isEmpty()) { 336 System.out.println("Scanner finished..."); 337 break; 338 } 339 340 printRow(entry); 341 } 342 343 transport.close(); 344 } 345 346 private void printVersions(ByteBuffer row, List<TCell> versions) { 347 StringBuilder rowStr = new StringBuilder(); 348 349 for (TCell cell : versions) { 350 rowStr.append(ClientUtils.utf8(cell.value.array())); 351 rowStr.append("; "); 352 } 353 354 System.out.println("row: " + ClientUtils.utf8(row) + ", values: " + rowStr); 355 } 356 357 private void printRow(TRowResult rowResult) { 358 ClientUtils.printRow(rowResult); 359 } 360 361 private void printRow(List<TRowResult> rows) { 362 for (TRowResult rowResult : rows) { 363 printRow(rowResult); 364 } 365 } 366 367 static Subject getSubject() throws Exception { 368 if (!secure) { 369 return new Subject(); 370 } 371 372 LoginContext context = ClientUtils.getLoginContext(); 373 context.login(); 374 return context.getSubject(); 375 } 376}