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