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    ArrayList<ColumnDescriptor> columns = new ArrayList<>(2);
143    ColumnDescriptor col;
144    col = new ColumnDescriptor();
145    col.name = ByteBuffer.wrap(bytes("entry:"));
146    col.timeToLive = Integer.MAX_VALUE;
147    col.maxVersions = 10;
148    columns.add(col);
149    col = new ColumnDescriptor();
150    col.name = ByteBuffer.wrap(bytes("unused:"));
151    col.timeToLive = Integer.MAX_VALUE;
152    columns.add(col);
153
154    System.out.println("creating table: " + ClientUtils.utf8(demoTable.array()));
155
156    try {
157      client.createTable(demoTable, columns);
158      client.createTable(disabledTable, columns);
159    } catch (AlreadyExists ae) {
160      System.out.println("WARN: " + ae.message);
161    }
162
163    System.out.println("column families in " + ClientUtils.utf8(demoTable.array()) + ": ");
164    Map<ByteBuffer, ColumnDescriptor> columnMap = client.getColumnDescriptors(demoTable);
165
166    for (ColumnDescriptor col2 : columnMap.values()) {
167      System.out.println(
168        "  column: " + ClientUtils.utf8(col2.name.array()) + ", maxVer: " + col2.maxVersions);
169    }
170
171    if (client.isTableEnabled(disabledTable)) {
172      System.out.println("disabling table: " + ClientUtils.utf8(disabledTable.array()));
173      client.disableTable(disabledTable);
174    }
175
176    System.out.println("list tables with enabled statuses : ");
177    Map<ByteBuffer, Boolean> statusMap = client.getTableNamesWithIsTableEnabled();
178    for (Map.Entry<ByteBuffer, Boolean> entry : statusMap.entrySet()) {
179      System.out.println(" Table: " + ClientUtils.utf8(entry.getKey().array()) + ", is enabled: "
180        + entry.getValue());
181    }
182
183    Map<ByteBuffer, ByteBuffer> dummyAttributes = null;
184    boolean writeToWal = false;
185
186    // Test UTF-8 handling
187    byte[] invalid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xfc, (byte) 0xa1,
188      (byte) 0xa1, (byte) 0xa1, (byte) 0xa1 };
189    byte[] valid = { (byte) 'f', (byte) 'o', (byte) 'o', (byte) '-', (byte) 0xE7, (byte) 0x94,
190      (byte) 0x9F, (byte) 0xE3, (byte) 0x83, (byte) 0x93, (byte) 0xE3, (byte) 0x83, (byte) 0xBC,
191      (byte) 0xE3, (byte) 0x83, (byte) 0xAB };
192
193    ArrayList<Mutation> mutations;
194    // non-utf8 is fine for data
195    mutations = new ArrayList<>(1);
196    mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid),
197      writeToWal));
198    client.mutateRow(demoTable, ByteBuffer.wrap(bytes("foo")), mutations, dummyAttributes);
199
200    // this row name is valid utf8
201    mutations = new ArrayList<>(1);
202    mutations.add(
203      new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(valid), writeToWal));
204    client.mutateRow(demoTable, ByteBuffer.wrap(valid), mutations, dummyAttributes);
205
206    // non-utf8 is now allowed in row names because HBase stores values as binary
207    mutations = new ArrayList<>(1);
208    mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")), ByteBuffer.wrap(invalid),
209      writeToWal));
210    client.mutateRow(demoTable, ByteBuffer.wrap(invalid), mutations, dummyAttributes);
211
212    // Run a scanner on the rows we just created
213    ArrayList<ByteBuffer> columnNames = new ArrayList<>();
214    columnNames.add(ByteBuffer.wrap(bytes("entry:")));
215
216    System.out.println("Starting scanner...");
217    int scanner =
218      client.scannerOpen(demoTable, ByteBuffer.wrap(bytes("")), columnNames, dummyAttributes);
219
220    while (true) {
221      List<TRowResult> entry = client.scannerGet(scanner);
222
223      if (entry.isEmpty()) {
224        break;
225      }
226
227      printRow(entry);
228    }
229
230    // Run some operations on a bunch of rows
231    for (int i = 100; i >= 0; --i) {
232      // format row keys as "00000" to "00100"
233      NumberFormat nf = NumberFormat.getInstance();
234      nf.setMinimumIntegerDigits(5);
235      nf.setGroupingUsed(false);
236      byte[] row = bytes(nf.format(i));
237
238      mutations = new ArrayList<>(1);
239      mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("unused:")),
240        ByteBuffer.wrap(bytes("DELETE_ME")), writeToWal));
241      client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
242      printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
243      client.deleteAllRow(demoTable, ByteBuffer.wrap(row), dummyAttributes);
244
245      // sleep to force later timestamp
246      try {
247        Thread.sleep(50);
248      } catch (InterruptedException e) {
249        // no-op
250      }
251
252      mutations = new ArrayList<>(2);
253      mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")),
254        ByteBuffer.wrap(bytes("0")), writeToWal));
255      mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:foo")),
256        ByteBuffer.wrap(bytes("FOO")), writeToWal));
257      client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
258      printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
259
260      Mutation m;
261      mutations = new ArrayList<>(2);
262      m = new Mutation();
263      m.column = ByteBuffer.wrap(bytes("entry:foo"));
264      m.isDelete = true;
265      mutations.add(m);
266      m = new Mutation();
267      m.column = ByteBuffer.wrap(bytes("entry:num"));
268      m.value = ByteBuffer.wrap(bytes("-1"));
269      mutations.add(m);
270      client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
271      printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
272
273      mutations = new ArrayList<>();
274      mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:num")),
275        ByteBuffer.wrap(bytes(Integer.toString(i))), writeToWal));
276      mutations.add(new Mutation(false, ByteBuffer.wrap(bytes("entry:sqr")),
277        ByteBuffer.wrap(bytes(Integer.toString(i * i))), writeToWal));
278      client.mutateRow(demoTable, ByteBuffer.wrap(row), mutations, dummyAttributes);
279      printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
280
281      // sleep to force later timestamp
282      try {
283        Thread.sleep(50);
284      } catch (InterruptedException e) {
285        // no-op
286      }
287
288      mutations.clear();
289      m = new Mutation();
290      m.column = ByteBuffer.wrap(bytes("entry:num"));
291      m.value = ByteBuffer.wrap(bytes("-999"));
292      mutations.add(m);
293      m = new Mutation();
294      m.column = ByteBuffer.wrap(bytes("entry:sqr"));
295      m.isDelete = true;
296      // shouldn't override latest
297      client.mutateRowTs(demoTable, ByteBuffer.wrap(row), mutations, 1, dummyAttributes);
298      printRow(client.getRow(demoTable, ByteBuffer.wrap(row), dummyAttributes));
299
300      List<TCell> versions = client.getVer(demoTable, ByteBuffer.wrap(row),
301        ByteBuffer.wrap(bytes("entry:num")), 10, dummyAttributes);
302      printVersions(ByteBuffer.wrap(row), versions);
303
304      if (versions.isEmpty()) {
305        System.out.println("FATAL: wrong # of versions");
306        System.exit(-1);
307      }
308
309      List<TCell> result = client.get(demoTable, ByteBuffer.wrap(row),
310        ByteBuffer.wrap(bytes("entry:foo")), dummyAttributes);
311
312      if (!result.isEmpty()) {
313        System.out.println("FATAL: shouldn't get here");
314        System.exit(-1);
315      }
316
317      System.out.println("");
318    }
319
320    // scan all rows/columnNames
321    columnNames.clear();
322
323    for (ColumnDescriptor col2 : client.getColumnDescriptors(demoTable).values()) {
324      System.out.println("column with name: " + ClientUtils.utf8(col2.name));
325      System.out.println(col2.toString());
326
327      columnNames.add(col2.name);
328    }
329
330    System.out.println("Starting scanner...");
331    scanner = client.scannerOpenWithStop(demoTable, ByteBuffer.wrap(bytes("00020")),
332      ByteBuffer.wrap(bytes("00040")), columnNames, dummyAttributes);
333
334    while (true) {
335      List<TRowResult> entry = client.scannerGet(scanner);
336
337      if (entry.isEmpty()) {
338        System.out.println("Scanner finished");
339        break;
340      }
341
342      printRow(entry);
343    }
344
345    transport.close();
346  }
347
348  private void printVersions(ByteBuffer row, List<TCell> versions) {
349    StringBuilder rowStr = new StringBuilder();
350
351    for (TCell cell : versions) {
352      rowStr.append(ClientUtils.utf8(cell.value.array()));
353      rowStr.append("; ");
354    }
355
356    System.out.println("row: " + ClientUtils.utf8(row) + ", values: " + rowStr);
357  }
358
359  private void printRow(TRowResult rowResult) {
360    ClientUtils.printRow(rowResult);
361  }
362
363  private void printRow(List<TRowResult> rows) {
364    for (TRowResult rowResult : rows) {
365      printRow(rowResult);
366    }
367  }
368
369  static Subject getSubject() throws Exception {
370    if (!secure) {
371      return new Subject();
372    }
373
374    LoginContext context = ClientUtils.getLoginContext();
375    context.login();
376    return context.getSubject();
377  }
378}