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