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.thrift2;
019
020import java.nio.ByteBuffer;
021import java.security.PrivilegedExceptionAction;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import javax.security.auth.Subject;
027import javax.security.auth.login.LoginContext;
028import javax.security.sasl.Sasl;
029import org.apache.hadoop.hbase.HBaseConfiguration;
030import org.apache.hadoop.hbase.thrift2.generated.TColumnValue;
031import org.apache.hadoop.hbase.thrift2.generated.TGet;
032import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
033import org.apache.hadoop.hbase.thrift2.generated.TPut;
034import org.apache.hadoop.hbase.thrift2.generated.TResult;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.util.ClientUtils;
037import org.apache.thrift.TConfiguration;
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.thrift.transport.layered.TFramedTransport;
044import org.apache.yetus.audience.InterfaceAudience;
045
046@InterfaceAudience.Private
047public class DemoClient {
048  private static String host = "localhost";
049  private static int port = 9090;
050  private static boolean secure = false;
051  private static String user = null;
052
053  public static void main(String[] args) throws Exception {
054    System.out.println("Thrift2 Demo");
055    System.out.println("Usage: DemoClient [host=localhost] [port=9090] [secure=false]");
056    System.out.println("This demo assumes you have a table called \"example\" with a column "
057      + "family called \"family1\"");
058
059    // use passed in arguments instead of defaults
060    if (args.length >= 1) {
061      host = args[0];
062    }
063    if (args.length >= 2) {
064      port = Integer.parseInt(args[1]);
065    }
066    org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
067    String principal = conf.get("hbase.thrift.kerberos.principal");
068    if (principal != null) {
069      secure = true;
070      int slashIdx = principal.indexOf("/");
071      int atIdx = principal.indexOf("@");
072      int idx = slashIdx != -1 ? slashIdx : atIdx != -1 ? atIdx : principal.length();
073      user = principal.substring(0, idx);
074    }
075    if (args.length >= 3) {
076      secure = Boolean.parseBoolean(args[2]);
077    }
078
079    final DemoClient client = new DemoClient();
080    Subject.doAs(getSubject(), new PrivilegedExceptionAction<Void>() {
081      @Override
082      public Void run() throws Exception {
083        client.run();
084        return null;
085      }
086    });
087  }
088
089  public void run() throws Exception {
090    int timeout = 10000;
091    boolean framed = false;
092
093    TTransport transport = new TSocket(new TConfiguration(), host, port, timeout);
094    if (framed) {
095      transport = new TFramedTransport(transport);
096    } else if (secure) {
097      /*
098       * The Thrift server the DemoClient is trying to connect to must have a matching principal,
099       * and support authentication. The HBase cluster must be secure, allow proxy user.
100       */
101      Map<String, String> saslProperties = new HashMap<>();
102      saslProperties.put(Sasl.QOP, "auth-conf,auth-int,auth");
103      transport = new TSaslClientTransport("GSSAPI", null, user != null ? user : "hbase", // Thrift
104                                                                                          // server
105                                                                                          // user
106                                                                                          // name,
107                                                                                          // should
108                                                                                          // be an
109                                                                                          // authorized
110                                                                                          // proxy
111                                                                                          // user
112        host, // Thrift server domain
113        saslProperties, null, transport);
114    }
115
116    TProtocol protocol = new TBinaryProtocol(transport);
117    // This is our thrift client.
118    THBaseService.Iface client = new THBaseService.Client(protocol);
119
120    // open the transport
121    transport.open();
122
123    ByteBuffer table = ByteBuffer.wrap(Bytes.toBytes("example"));
124
125    TPut put = new TPut();
126    put.setRow(Bytes.toBytes("row1"));
127
128    TColumnValue columnValue = new TColumnValue();
129    columnValue.setFamily(Bytes.toBytes("family1"));
130    columnValue.setQualifier(Bytes.toBytes("qualifier1"));
131    columnValue.setValue(Bytes.toBytes("value1"));
132    List<TColumnValue> columnValues = new ArrayList<>(1);
133    columnValues.add(columnValue);
134    put.setColumnValues(columnValues);
135
136    client.put(table, put);
137
138    TGet get = new TGet();
139    get.setRow(Bytes.toBytes("row1"));
140
141    TResult result = client.get(table, get);
142
143    System.out.print("row = " + ClientUtils.utf8(result.getRow()));
144    for (TColumnValue resultColumnValue : result.getColumnValues()) {
145      System.out.print("family = " + ClientUtils.utf8(resultColumnValue.getFamily()));
146      System.out.print("qualifier = " + ClientUtils.utf8(resultColumnValue.getFamily()));
147      System.out.print("value = " + ClientUtils.utf8(resultColumnValue.getValue()));
148      System.out.print("timestamp = " + resultColumnValue.getTimestamp());
149    }
150
151    transport.close();
152  }
153
154  static Subject getSubject() throws Exception {
155    if (!secure) {
156      return new Subject();
157    }
158
159    LoginContext context = ClientUtils.getLoginContext();
160    context.login();
161    return context.getSubject();
162  }
163}