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 */
018
019package org.apache.hadoop.hbase.rest;
020
021import java.security.PrivilegedExceptionAction;
022
023import javax.security.auth.Subject;
024import javax.security.auth.login.LoginContext;
025
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.CellUtil;
028import org.apache.hadoop.hbase.HBaseConfiguration;
029import org.apache.hadoop.hbase.client.Get;
030import org.apache.hadoop.hbase.client.Put;
031import org.apache.hadoop.hbase.client.Result;
032import org.apache.hadoop.hbase.rest.client.Client;
033import org.apache.hadoop.hbase.rest.client.Cluster;
034import org.apache.hadoop.hbase.rest.client.RemoteHTable;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.util.ClientUtils;
037import org.apache.yetus.audience.InterfaceAudience;
038import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
039
040@InterfaceAudience.Private
041public class RESTDemoClient {
042
043  private static String host = "localhost";
044  private static int port = 9090;
045  private static boolean secure = false;
046  private static org.apache.hadoop.conf.Configuration conf = null;
047
048  public static void main(String[] args) throws Exception {
049    System.out.println("REST Demo");
050    System.out.println("Usage: RESTDemoClient [host=localhost] [port=9090] [secure=false]");
051    System.out.println("This demo assumes you have a table called \"example\""
052        + " with a column family called \"family1\"");
053
054    // use passed in arguments instead of defaults
055    if (args.length >= 1) {
056      host = args[0];
057    }
058    if (args.length >= 2) {
059      port = Integer.parseInt(args[1]);
060    }
061    conf = HBaseConfiguration.create();
062    String principal = conf.get(Constants.REST_KERBEROS_PRINCIPAL);
063    if (principal != null) {
064      secure = true;
065    }
066    if (args.length >= 3) {
067      secure = Boolean.parseBoolean(args[2]);
068    }
069
070    final RESTDemoClient client = new RESTDemoClient();
071    Subject.doAs(getSubject(), new PrivilegedExceptionAction<Void>() {
072      @Override
073      public Void run() throws Exception {
074        client.run();
075        return null;
076      }
077    });
078  }
079
080  public void run() throws Exception {
081    Cluster cluster = new Cluster();
082    cluster.add(host, port);
083    Client restClient = new Client(cluster, conf.getBoolean(Constants.REST_SSL_ENABLED, false));
084    try (RemoteHTable remoteTable = new RemoteHTable(restClient, conf, "example")) {
085      // Write data to the table
086      String rowKey = "row1";
087      Put p = new Put(rowKey.getBytes());
088      p.addColumn("family1".getBytes(), "qualifier1".getBytes(), "value1".getBytes());
089      remoteTable.put(p);
090
091      // Get the data from the table
092      Get g = new Get(rowKey.getBytes());
093      Result result = remoteTable.get(g);
094
095      Preconditions.checkArgument(result != null,
096        Bytes.toString(remoteTable.getTableName()) + " should have a row with key as " + rowKey);
097      System.out.println("row = " + new String(result.getRow()));
098      for (Cell cell : result.rawCells()) {
099        System.out.print("family = " + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");
100        System.out.print("qualifier = " + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");
101        System.out.print("value = " + Bytes.toString(CellUtil.cloneValue(cell)) + "\t");
102        System.out.println("timestamp = " + Long.toString(cell.getTimestamp()));
103      }
104    }
105  }
106
107  static Subject getSubject() throws Exception {
108    if (!secure) {
109      return new Subject();
110    }
111
112    LoginContext context = ClientUtils.getLoginContext();
113    context.login();
114    return context.getSubject();
115  }
116}