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