1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.hive.service.cli.thrift; 20 21 import java.util.List; 22 import java.util.Map; 23 24 import org.apache.hive.service.auth.HiveAuthFactory; 25 import org.apache.hive.service.cli.*; 26 import org.apache.thrift.TException; 27 28 /** 29 * ThriftCLIServiceClient. 30 * 31 */ 32 public class ThriftCLIServiceClient extends CLIServiceClient { 33 private final TCLIService.Iface cliService; 34 ThriftCLIServiceClient(TCLIService.Iface cliService)35 public ThriftCLIServiceClient(TCLIService.Iface cliService) { 36 this.cliService = cliService; 37 } 38 checkStatus(TStatus status)39 public void checkStatus(TStatus status) throws HiveSQLException { 40 if (TStatusCode.ERROR_STATUS.equals(status.getStatusCode())) { 41 throw new HiveSQLException(status); 42 } 43 } 44 45 /* (non-Javadoc) 46 * @see org.apache.hive.service.cli.ICLIService#openSession(java.lang.String, java.lang.String, java.util.Map) 47 */ 48 @Override openSession(String username, String password, Map<String, String> configuration)49 public SessionHandle openSession(String username, String password, 50 Map<String, String> configuration) 51 throws HiveSQLException { 52 try { 53 TOpenSessionReq req = new TOpenSessionReq(); 54 req.setUsername(username); 55 req.setPassword(password); 56 req.setConfiguration(configuration); 57 TOpenSessionResp resp = cliService.OpenSession(req); 58 checkStatus(resp.getStatus()); 59 return new SessionHandle(resp.getSessionHandle(), resp.getServerProtocolVersion()); 60 } catch (HiveSQLException e) { 61 throw e; 62 } catch (Exception e) { 63 throw new HiveSQLException(e); 64 } 65 } 66 67 /* (non-Javadoc) 68 * @see org.apache.hive.service.cli.ICLIService#closeSession(org.apache.hive.service.cli.SessionHandle) 69 */ 70 @Override openSessionWithImpersonation(String username, String password, Map<String, String> configuration, String delegationToken)71 public SessionHandle openSessionWithImpersonation(String username, String password, 72 Map<String, String> configuration, String delegationToken) throws HiveSQLException { 73 throw new HiveSQLException("open with impersonation operation is not supported in the client"); 74 } 75 76 /* (non-Javadoc) 77 * @see org.apache.hive.service.cli.ICLIService#closeSession(org.apache.hive.service.cli.SessionHandle) 78 */ 79 @Override closeSession(SessionHandle sessionHandle)80 public void closeSession(SessionHandle sessionHandle) throws HiveSQLException { 81 try { 82 TCloseSessionReq req = new TCloseSessionReq(sessionHandle.toTSessionHandle()); 83 TCloseSessionResp resp = cliService.CloseSession(req); 84 checkStatus(resp.getStatus()); 85 } catch (HiveSQLException e) { 86 throw e; 87 } catch (Exception e) { 88 throw new HiveSQLException(e); 89 } 90 } 91 92 /* (non-Javadoc) 93 * @see org.apache.hive.service.cli.ICLIService#getInfo(org.apache.hive.service.cli.SessionHandle, java.util.List) 94 */ 95 @Override getInfo(SessionHandle sessionHandle, GetInfoType infoType)96 public GetInfoValue getInfo(SessionHandle sessionHandle, GetInfoType infoType) 97 throws HiveSQLException { 98 try { 99 // FIXME extract the right info type 100 TGetInfoReq req = new TGetInfoReq(sessionHandle.toTSessionHandle(), infoType.toTGetInfoType()); 101 TGetInfoResp resp = cliService.GetInfo(req); 102 checkStatus(resp.getStatus()); 103 return new GetInfoValue(resp.getInfoValue()); 104 } catch (HiveSQLException e) { 105 throw e; 106 } catch (Exception e) { 107 throw new HiveSQLException(e); 108 } 109 } 110 111 /* (non-Javadoc) 112 * @see org.apache.hive.service.cli.ICLIService#executeStatement(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.util.Map) 113 */ 114 @Override executeStatement(SessionHandle sessionHandle, String statement, Map<String, String> confOverlay)115 public OperationHandle executeStatement(SessionHandle sessionHandle, String statement, 116 Map<String, String> confOverlay) 117 throws HiveSQLException { 118 return executeStatementInternal(sessionHandle, statement, confOverlay, false); 119 } 120 121 /* (non-Javadoc) 122 * @see org.apache.hive.service.cli.ICLIService#executeStatementAsync(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.util.Map) 123 */ 124 @Override executeStatementAsync(SessionHandle sessionHandle, String statement, Map<String, String> confOverlay)125 public OperationHandle executeStatementAsync(SessionHandle sessionHandle, String statement, 126 Map<String, String> confOverlay) 127 throws HiveSQLException { 128 return executeStatementInternal(sessionHandle, statement, confOverlay, true); 129 } 130 executeStatementInternal(SessionHandle sessionHandle, String statement, Map<String, String> confOverlay, boolean isAsync)131 private OperationHandle executeStatementInternal(SessionHandle sessionHandle, String statement, 132 Map<String, String> confOverlay, boolean isAsync) 133 throws HiveSQLException { 134 try { 135 TExecuteStatementReq req = 136 new TExecuteStatementReq(sessionHandle.toTSessionHandle(), statement); 137 req.setConfOverlay(confOverlay); 138 req.setRunAsync(isAsync); 139 TExecuteStatementResp resp = cliService.ExecuteStatement(req); 140 checkStatus(resp.getStatus()); 141 TProtocolVersion protocol = sessionHandle.getProtocolVersion(); 142 return new OperationHandle(resp.getOperationHandle(), protocol); 143 } catch (HiveSQLException e) { 144 throw e; 145 } catch (Exception e) { 146 throw new HiveSQLException(e); 147 } 148 } 149 150 /* (non-Javadoc) 151 * @see org.apache.hive.service.cli.ICLIService#getTypeInfo(org.apache.hive.service.cli.SessionHandle) 152 */ 153 @Override getTypeInfo(SessionHandle sessionHandle)154 public OperationHandle getTypeInfo(SessionHandle sessionHandle) throws HiveSQLException { 155 try { 156 TGetTypeInfoReq req = new TGetTypeInfoReq(sessionHandle.toTSessionHandle()); 157 TGetTypeInfoResp resp = cliService.GetTypeInfo(req); 158 checkStatus(resp.getStatus()); 159 TProtocolVersion protocol = sessionHandle.getProtocolVersion(); 160 return new OperationHandle(resp.getOperationHandle(), protocol); 161 } catch (HiveSQLException e) { 162 throw e; 163 } catch (Exception e) { 164 throw new HiveSQLException(e); 165 } 166 } 167 168 /* (non-Javadoc) 169 * @see org.apache.hive.service.cli.ICLIService#getCatalogs(org.apache.hive.service.cli.SessionHandle) 170 */ 171 @Override getCatalogs(SessionHandle sessionHandle)172 public OperationHandle getCatalogs(SessionHandle sessionHandle) throws HiveSQLException { 173 try { 174 TGetCatalogsReq req = new TGetCatalogsReq(sessionHandle.toTSessionHandle()); 175 TGetCatalogsResp resp = cliService.GetCatalogs(req); 176 checkStatus(resp.getStatus()); 177 TProtocolVersion protocol = sessionHandle.getProtocolVersion(); 178 return new OperationHandle(resp.getOperationHandle(), protocol); 179 } catch (HiveSQLException e) { 180 throw e; 181 } catch (Exception e) { 182 throw new HiveSQLException(e); 183 } 184 } 185 186 /* (non-Javadoc) 187 * @see org.apache.hive.service.cli.ICLIService#getSchemas(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.lang.String) 188 */ 189 @Override getSchemas(SessionHandle sessionHandle, String catalogName, String schemaName)190 public OperationHandle getSchemas(SessionHandle sessionHandle, String catalogName, 191 String schemaName) 192 throws HiveSQLException { 193 try { 194 TGetSchemasReq req = new TGetSchemasReq(sessionHandle.toTSessionHandle()); 195 req.setCatalogName(catalogName); 196 req.setSchemaName(schemaName); 197 TGetSchemasResp resp = cliService.GetSchemas(req); 198 checkStatus(resp.getStatus()); 199 TProtocolVersion protocol = sessionHandle.getProtocolVersion(); 200 return new OperationHandle(resp.getOperationHandle(), protocol); 201 } catch (HiveSQLException e) { 202 throw e; 203 } catch (Exception e) { 204 throw new HiveSQLException(e); 205 } 206 } 207 208 /* (non-Javadoc) 209 * @see org.apache.hive.service.cli.ICLIService#getTables(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.lang.String, java.lang.String, java.util.List) 210 */ 211 @Override getTables(SessionHandle sessionHandle, String catalogName, String schemaName, String tableName, List<String> tableTypes)212 public OperationHandle getTables(SessionHandle sessionHandle, String catalogName, 213 String schemaName, String tableName, List<String> tableTypes) 214 throws HiveSQLException { 215 try { 216 TGetTablesReq req = new TGetTablesReq(sessionHandle.toTSessionHandle()); 217 req.setTableName(tableName); 218 req.setTableTypes(tableTypes); 219 req.setSchemaName(schemaName); 220 TGetTablesResp resp = cliService.GetTables(req); 221 checkStatus(resp.getStatus()); 222 TProtocolVersion protocol = sessionHandle.getProtocolVersion(); 223 return new OperationHandle(resp.getOperationHandle(), protocol); 224 } catch (HiveSQLException e) { 225 throw e; 226 } catch (Exception e) { 227 throw new HiveSQLException(e); 228 } 229 } 230 231 /* (non-Javadoc) 232 * @see org.apache.hive.service.cli.ICLIService#getTableTypes(org.apache.hive.service.cli.SessionHandle) 233 */ 234 @Override getTableTypes(SessionHandle sessionHandle)235 public OperationHandle getTableTypes(SessionHandle sessionHandle) throws HiveSQLException { 236 try { 237 TGetTableTypesReq req = new TGetTableTypesReq(sessionHandle.toTSessionHandle()); 238 TGetTableTypesResp resp = cliService.GetTableTypes(req); 239 checkStatus(resp.getStatus()); 240 TProtocolVersion protocol = sessionHandle.getProtocolVersion(); 241 return new OperationHandle(resp.getOperationHandle(), protocol); 242 } catch (HiveSQLException e) { 243 throw e; 244 } catch (Exception e) { 245 throw new HiveSQLException(e); 246 } 247 } 248 249 /* (non-Javadoc) 250 * @see org.apache.hive.service.cli.ICLIService#getColumns(org.apache.hive.service.cli.SessionHandle) 251 */ 252 @Override getColumns(SessionHandle sessionHandle, String catalogName, String schemaName, String tableName, String columnName)253 public OperationHandle getColumns(SessionHandle sessionHandle, 254 String catalogName, String schemaName, String tableName, String columnName) 255 throws HiveSQLException { 256 try { 257 TGetColumnsReq req = new TGetColumnsReq(); 258 req.setSessionHandle(sessionHandle.toTSessionHandle()); 259 req.setCatalogName(catalogName); 260 req.setSchemaName(schemaName); 261 req.setTableName(tableName); 262 req.setColumnName(columnName); 263 TGetColumnsResp resp = cliService.GetColumns(req); 264 checkStatus(resp.getStatus()); 265 TProtocolVersion protocol = sessionHandle.getProtocolVersion(); 266 return new OperationHandle(resp.getOperationHandle(), protocol); 267 } catch (HiveSQLException e) { 268 throw e; 269 } catch (Exception e) { 270 throw new HiveSQLException(e); 271 } 272 } 273 274 /* (non-Javadoc) 275 * @see org.apache.hive.service.cli.ICLIService#getFunctions(org.apache.hive.service.cli.SessionHandle) 276 */ 277 @Override getFunctions(SessionHandle sessionHandle, String catalogName, String schemaName, String functionName)278 public OperationHandle getFunctions(SessionHandle sessionHandle, 279 String catalogName, String schemaName, String functionName) throws HiveSQLException { 280 try { 281 TGetFunctionsReq req = new TGetFunctionsReq(sessionHandle.toTSessionHandle(), functionName); 282 req.setCatalogName(catalogName); 283 req.setSchemaName(schemaName); 284 TGetFunctionsResp resp = cliService.GetFunctions(req); 285 checkStatus(resp.getStatus()); 286 TProtocolVersion protocol = sessionHandle.getProtocolVersion(); 287 return new OperationHandle(resp.getOperationHandle(), protocol); 288 } catch (HiveSQLException e) { 289 throw e; 290 } catch (Exception e) { 291 throw new HiveSQLException(e); 292 } 293 } 294 295 /* (non-Javadoc) 296 * @see org.apache.hive.service.cli.ICLIService#getOperationStatus(org.apache.hive.service.cli.OperationHandle) 297 */ 298 @Override getOperationStatus(OperationHandle opHandle)299 public OperationStatus getOperationStatus(OperationHandle opHandle) throws HiveSQLException { 300 try { 301 TGetOperationStatusReq req = new TGetOperationStatusReq(opHandle.toTOperationHandle()); 302 TGetOperationStatusResp resp = cliService.GetOperationStatus(req); 303 // Checks the status of the RPC call, throws an exception in case of error 304 checkStatus(resp.getStatus()); 305 OperationState opState = OperationState.getOperationState(resp.getOperationState()); 306 HiveSQLException opException = null; 307 if (opState == OperationState.ERROR) { 308 opException = new HiveSQLException(resp.getErrorMessage(), resp.getSqlState(), resp.getErrorCode()); 309 } 310 return new OperationStatus(opState, opException); 311 } catch (HiveSQLException e) { 312 throw e; 313 } catch (Exception e) { 314 throw new HiveSQLException(e); 315 } 316 } 317 318 /* (non-Javadoc) 319 * @see org.apache.hive.service.cli.ICLIService#cancelOperation(org.apache.hive.service.cli.OperationHandle) 320 */ 321 @Override cancelOperation(OperationHandle opHandle)322 public void cancelOperation(OperationHandle opHandle) throws HiveSQLException { 323 try { 324 TCancelOperationReq req = new TCancelOperationReq(opHandle.toTOperationHandle()); 325 TCancelOperationResp resp = cliService.CancelOperation(req); 326 checkStatus(resp.getStatus()); 327 } catch (HiveSQLException e) { 328 throw e; 329 } catch (Exception e) { 330 throw new HiveSQLException(e); 331 } 332 } 333 334 /* (non-Javadoc) 335 * @see org.apache.hive.service.cli.ICLIService#closeOperation(org.apache.hive.service.cli.OperationHandle) 336 */ 337 @Override closeOperation(OperationHandle opHandle)338 public void closeOperation(OperationHandle opHandle) 339 throws HiveSQLException { 340 try { 341 TCloseOperationReq req = new TCloseOperationReq(opHandle.toTOperationHandle()); 342 TCloseOperationResp resp = cliService.CloseOperation(req); 343 checkStatus(resp.getStatus()); 344 } catch (HiveSQLException e) { 345 throw e; 346 } catch (Exception e) { 347 throw new HiveSQLException(e); 348 } 349 } 350 351 /* (non-Javadoc) 352 * @see org.apache.hive.service.cli.ICLIService#getResultSetMetadata(org.apache.hive.service.cli.OperationHandle) 353 */ 354 @Override getResultSetMetadata(OperationHandle opHandle)355 public TableSchema getResultSetMetadata(OperationHandle opHandle) 356 throws HiveSQLException { 357 try { 358 TGetResultSetMetadataReq req = new TGetResultSetMetadataReq(opHandle.toTOperationHandle()); 359 TGetResultSetMetadataResp resp = cliService.GetResultSetMetadata(req); 360 checkStatus(resp.getStatus()); 361 return new TableSchema(resp.getSchema()); 362 } catch (HiveSQLException e) { 363 throw e; 364 } catch (Exception e) { 365 throw new HiveSQLException(e); 366 } 367 } 368 369 @Override fetchResults(OperationHandle opHandle, FetchOrientation orientation, long maxRows, FetchType fetchType)370 public RowSet fetchResults(OperationHandle opHandle, FetchOrientation orientation, long maxRows, 371 FetchType fetchType) throws HiveSQLException { 372 try { 373 TFetchResultsReq req = new TFetchResultsReq(); 374 req.setOperationHandle(opHandle.toTOperationHandle()); 375 req.setOrientation(orientation.toTFetchOrientation()); 376 req.setMaxRows(maxRows); 377 req.setFetchType(fetchType.toTFetchType()); 378 TFetchResultsResp resp = cliService.FetchResults(req); 379 checkStatus(resp.getStatus()); 380 return RowSetFactory.create(resp.getResults(), opHandle.getProtocolVersion()); 381 } catch (HiveSQLException e) { 382 throw e; 383 } catch (Exception e) { 384 throw new HiveSQLException(e); 385 } 386 } 387 388 /* (non-Javadoc) 389 * @see org.apache.hive.service.cli.ICLIService#fetchResults(org.apache.hive.service.cli.OperationHandle) 390 */ 391 @Override fetchResults(OperationHandle opHandle)392 public RowSet fetchResults(OperationHandle opHandle) throws HiveSQLException { 393 // TODO: set the correct default fetch size 394 return fetchResults(opHandle, FetchOrientation.FETCH_NEXT, 10000, FetchType.QUERY_OUTPUT); 395 } 396 397 @Override getDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory, String owner, String renewer)398 public String getDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory, 399 String owner, String renewer) throws HiveSQLException { 400 TGetDelegationTokenReq req = new TGetDelegationTokenReq( 401 sessionHandle.toTSessionHandle(), owner, renewer); 402 try { 403 TGetDelegationTokenResp tokenResp = cliService.GetDelegationToken(req); 404 checkStatus(tokenResp.getStatus()); 405 return tokenResp.getDelegationToken(); 406 } catch (Exception e) { 407 throw new HiveSQLException(e); 408 } 409 } 410 411 @Override cancelDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory, String tokenStr)412 public void cancelDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory, 413 String tokenStr) throws HiveSQLException { 414 TCancelDelegationTokenReq cancelReq = new TCancelDelegationTokenReq( 415 sessionHandle.toTSessionHandle(), tokenStr); 416 try { 417 TCancelDelegationTokenResp cancelResp = 418 cliService.CancelDelegationToken(cancelReq); 419 checkStatus(cancelResp.getStatus()); 420 return; 421 } catch (TException e) { 422 throw new HiveSQLException(e); 423 } 424 } 425 426 @Override renewDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory, String tokenStr)427 public void renewDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory, 428 String tokenStr) throws HiveSQLException { 429 TRenewDelegationTokenReq cancelReq = new TRenewDelegationTokenReq( 430 sessionHandle.toTSessionHandle(), tokenStr); 431 try { 432 TRenewDelegationTokenResp renewResp = 433 cliService.RenewDelegationToken(cancelReq); 434 checkStatus(renewResp.getStatus()); 435 return; 436 } catch (Exception e) { 437 throw new HiveSQLException(e); 438 } 439 } 440 } 441