Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements.  See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership.  The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License.  You may obtain a copy of the License at
0009  *
0010  *     http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
0017  */
0018 
0019 package org.apache.hive.service.cli.thrift;
0020 
0021 import java.util.List;
0022 import java.util.Map;
0023 
0024 import org.apache.hive.service.auth.HiveAuthFactory;
0025 import org.apache.hive.service.cli.*;
0026 import org.apache.hive.service.rpc.thrift.*;
0027 import org.apache.thrift.TException;
0028 
0029 /**
0030  * ThriftCLIServiceClient.
0031  *
0032  */
0033 public class ThriftCLIServiceClient extends CLIServiceClient {
0034   private final TCLIService.Iface cliService;
0035 
0036   public ThriftCLIServiceClient(TCLIService.Iface cliService) {
0037     this.cliService = cliService;
0038   }
0039 
0040   public void checkStatus(TStatus status) throws HiveSQLException {
0041     if (TStatusCode.ERROR_STATUS.equals(status.getStatusCode())) {
0042       throw new HiveSQLException(status);
0043     }
0044   }
0045 
0046   /* (non-Javadoc)
0047    * @see org.apache.hive.service.cli.ICLIService#openSession(java.lang.String, java.lang.String, java.util.Map)
0048    */
0049   @Override
0050   public SessionHandle openSession(String username, String password,
0051       Map<String, String> configuration)
0052           throws HiveSQLException {
0053     try {
0054       TOpenSessionReq req = new TOpenSessionReq();
0055       req.setUsername(username);
0056       req.setPassword(password);
0057       req.setConfiguration(configuration);
0058       TOpenSessionResp resp = cliService.OpenSession(req);
0059       checkStatus(resp.getStatus());
0060       return new SessionHandle(resp.getSessionHandle(), resp.getServerProtocolVersion());
0061     } catch (HiveSQLException e) {
0062       throw e;
0063     } catch (Exception e) {
0064       throw new HiveSQLException(e);
0065     }
0066   }
0067 
0068   /* (non-Javadoc)
0069    * @see org.apache.hive.service.cli.ICLIService#closeSession(org.apache.hive.service.cli.SessionHandle)
0070    */
0071   @Override
0072   public SessionHandle openSessionWithImpersonation(String username, String password,
0073       Map<String, String> configuration, String delegationToken) throws HiveSQLException {
0074     throw new HiveSQLException("open with impersonation operation is not supported in the client");
0075   }
0076 
0077   /* (non-Javadoc)
0078    * @see org.apache.hive.service.cli.ICLIService#closeSession(org.apache.hive.service.cli.SessionHandle)
0079    */
0080   @Override
0081   public void closeSession(SessionHandle sessionHandle) throws HiveSQLException {
0082     try {
0083       TCloseSessionReq req = new TCloseSessionReq(sessionHandle.toTSessionHandle());
0084       TCloseSessionResp resp = cliService.CloseSession(req);
0085       checkStatus(resp.getStatus());
0086     } catch (HiveSQLException e) {
0087       throw e;
0088     } catch (Exception e) {
0089       throw new HiveSQLException(e);
0090     }
0091   }
0092 
0093   /* (non-Javadoc)
0094    * @see org.apache.hive.service.cli.ICLIService#getInfo(org.apache.hive.service.cli.SessionHandle, java.util.List)
0095    */
0096   @Override
0097   public GetInfoValue getInfo(SessionHandle sessionHandle, GetInfoType infoType)
0098       throws HiveSQLException {
0099     try {
0100       // FIXME extract the right info type
0101       TGetInfoReq req = new TGetInfoReq(sessionHandle.toTSessionHandle(), infoType.toTGetInfoType());
0102       TGetInfoResp resp = cliService.GetInfo(req);
0103       checkStatus(resp.getStatus());
0104       return new GetInfoValue(resp.getInfoValue());
0105     } catch (HiveSQLException e) {
0106       throw e;
0107     } catch (Exception e) {
0108       throw new HiveSQLException(e);
0109     }
0110   }
0111 
0112   /* (non-Javadoc)
0113    * @see org.apache.hive.service.cli.ICLIService#executeStatement(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.util.Map)
0114    */
0115   @Override
0116   public OperationHandle executeStatement(SessionHandle sessionHandle, String statement,
0117       Map<String, String> confOverlay) throws HiveSQLException {
0118     return executeStatementInternal(sessionHandle, statement, confOverlay, false, 0);
0119   }
0120 
0121   @Override
0122   public OperationHandle executeStatement(SessionHandle sessionHandle, String statement,
0123       Map<String, String> confOverlay, long queryTimeout) throws HiveSQLException {
0124     return executeStatementInternal(sessionHandle, statement, confOverlay, false, queryTimeout);
0125   }
0126 
0127   @Override
0128   public OperationHandle executeStatementAsync(SessionHandle sessionHandle, String statement,
0129       Map<String, String> confOverlay) throws HiveSQLException {
0130     return executeStatementInternal(sessionHandle, statement, confOverlay, true, 0);
0131   }
0132 
0133   /* (non-Javadoc)
0134    * @see org.apache.hive.service.cli.ICLIService#executeStatementAsync(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.util.Map)
0135    */
0136   @Override
0137   public OperationHandle executeStatementAsync(SessionHandle sessionHandle, String statement,
0138       Map<String, String> confOverlay, long queryTimeout) throws HiveSQLException {
0139     return executeStatementInternal(sessionHandle, statement, confOverlay, true, queryTimeout);
0140   }
0141 
0142   private OperationHandle executeStatementInternal(SessionHandle sessionHandle, String statement,
0143       Map<String, String> confOverlay, boolean isAsync, long queryTimeout) throws HiveSQLException {
0144     try {
0145       TExecuteStatementReq req =
0146           new TExecuteStatementReq(sessionHandle.toTSessionHandle(), statement);
0147       req.setConfOverlay(confOverlay);
0148       req.setRunAsync(isAsync);
0149       req.setQueryTimeout(queryTimeout);
0150       TExecuteStatementResp resp = cliService.ExecuteStatement(req);
0151       checkStatus(resp.getStatus());
0152       TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0153       return new OperationHandle(resp.getOperationHandle(), protocol);
0154     } catch (HiveSQLException e) {
0155       throw e;
0156     } catch (Exception e) {
0157       throw new HiveSQLException(e);
0158     }
0159   }
0160 
0161   /* (non-Javadoc)
0162    * @see org.apache.hive.service.cli.ICLIService#getTypeInfo(org.apache.hive.service.cli.SessionHandle)
0163    */
0164   @Override
0165   public OperationHandle getTypeInfo(SessionHandle sessionHandle) throws HiveSQLException {
0166     try {
0167       TGetTypeInfoReq req = new TGetTypeInfoReq(sessionHandle.toTSessionHandle());
0168       TGetTypeInfoResp resp = cliService.GetTypeInfo(req);
0169       checkStatus(resp.getStatus());
0170       TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0171       return new OperationHandle(resp.getOperationHandle(), protocol);
0172     } catch (HiveSQLException e) {
0173       throw e;
0174     } catch (Exception e) {
0175       throw new HiveSQLException(e);
0176     }
0177   }
0178 
0179   /* (non-Javadoc)
0180    * @see org.apache.hive.service.cli.ICLIService#getCatalogs(org.apache.hive.service.cli.SessionHandle)
0181    */
0182   @Override
0183   public OperationHandle getCatalogs(SessionHandle sessionHandle) throws HiveSQLException {
0184     try {
0185       TGetCatalogsReq req = new TGetCatalogsReq(sessionHandle.toTSessionHandle());
0186       TGetCatalogsResp resp = cliService.GetCatalogs(req);
0187       checkStatus(resp.getStatus());
0188       TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0189       return new OperationHandle(resp.getOperationHandle(), protocol);
0190     } catch (HiveSQLException e) {
0191       throw e;
0192     } catch (Exception e) {
0193       throw new HiveSQLException(e);
0194     }
0195   }
0196 
0197   /* (non-Javadoc)
0198    * @see org.apache.hive.service.cli.ICLIService#getSchemas(org.apache.hive.service.cli.SessionHandle, java.lang.String, java.lang.String)
0199    */
0200   @Override
0201   public OperationHandle getSchemas(SessionHandle sessionHandle, String catalogName,
0202       String schemaName)
0203           throws HiveSQLException {
0204     try {
0205       TGetSchemasReq req = new TGetSchemasReq(sessionHandle.toTSessionHandle());
0206       req.setCatalogName(catalogName);
0207       req.setSchemaName(schemaName);
0208       TGetSchemasResp resp = cliService.GetSchemas(req);
0209       checkStatus(resp.getStatus());
0210       TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0211       return new OperationHandle(resp.getOperationHandle(), protocol);
0212     } catch (HiveSQLException e) {
0213       throw e;
0214     } catch (Exception e) {
0215       throw new HiveSQLException(e);
0216     }
0217   }
0218 
0219   /* (non-Javadoc)
0220    * @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)
0221    */
0222   @Override
0223   public OperationHandle getTables(SessionHandle sessionHandle, String catalogName,
0224       String schemaName, String tableName, List<String> tableTypes)
0225           throws HiveSQLException {
0226     try {
0227       TGetTablesReq req = new TGetTablesReq(sessionHandle.toTSessionHandle());
0228       req.setTableName(tableName);
0229       req.setTableTypes(tableTypes);
0230       req.setSchemaName(schemaName);
0231       TGetTablesResp resp = cliService.GetTables(req);
0232       checkStatus(resp.getStatus());
0233       TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0234       return new OperationHandle(resp.getOperationHandle(), protocol);
0235     } catch (HiveSQLException e) {
0236       throw e;
0237     } catch (Exception e) {
0238       throw new HiveSQLException(e);
0239     }
0240   }
0241 
0242   /* (non-Javadoc)
0243    * @see org.apache.hive.service.cli.ICLIService#getTableTypes(org.apache.hive.service.cli.SessionHandle)
0244    */
0245   @Override
0246   public OperationHandle getTableTypes(SessionHandle sessionHandle) throws HiveSQLException {
0247     try {
0248       TGetTableTypesReq req = new TGetTableTypesReq(sessionHandle.toTSessionHandle());
0249       TGetTableTypesResp resp = cliService.GetTableTypes(req);
0250       checkStatus(resp.getStatus());
0251       TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0252       return new OperationHandle(resp.getOperationHandle(), protocol);
0253     } catch (HiveSQLException e) {
0254       throw e;
0255     } catch (Exception e) {
0256       throw new HiveSQLException(e);
0257     }
0258   }
0259 
0260   /* (non-Javadoc)
0261    * @see org.apache.hive.service.cli.ICLIService#getColumns(org.apache.hive.service.cli.SessionHandle)
0262    */
0263   @Override
0264   public OperationHandle getColumns(SessionHandle sessionHandle,
0265       String catalogName, String schemaName, String tableName, String columnName)
0266           throws HiveSQLException {
0267     try {
0268       TGetColumnsReq req = new TGetColumnsReq();
0269       req.setSessionHandle(sessionHandle.toTSessionHandle());
0270       req.setCatalogName(catalogName);
0271       req.setSchemaName(schemaName);
0272       req.setTableName(tableName);
0273       req.setColumnName(columnName);
0274       TGetColumnsResp resp = cliService.GetColumns(req);
0275       checkStatus(resp.getStatus());
0276       TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0277       return new OperationHandle(resp.getOperationHandle(), protocol);
0278     } catch (HiveSQLException e) {
0279       throw e;
0280     } catch (Exception e) {
0281       throw new HiveSQLException(e);
0282     }
0283   }
0284 
0285   /* (non-Javadoc)
0286    * @see org.apache.hive.service.cli.ICLIService#getFunctions(org.apache.hive.service.cli.SessionHandle)
0287    */
0288   @Override
0289   public OperationHandle getFunctions(SessionHandle sessionHandle,
0290       String catalogName, String schemaName, String functionName) throws HiveSQLException {
0291     try {
0292       TGetFunctionsReq req = new TGetFunctionsReq(sessionHandle.toTSessionHandle(), functionName);
0293       req.setCatalogName(catalogName);
0294       req.setSchemaName(schemaName);
0295       TGetFunctionsResp resp = cliService.GetFunctions(req);
0296       checkStatus(resp.getStatus());
0297       TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0298       return new OperationHandle(resp.getOperationHandle(), protocol);
0299     } catch (HiveSQLException e) {
0300       throw e;
0301     } catch (Exception e) {
0302       throw new HiveSQLException(e);
0303     }
0304   }
0305 
0306   /* (non-Javadoc)
0307    * @see org.apache.hive.service.cli.ICLIService#getOperationStatus(org.apache.hive.service.cli.OperationHandle)
0308    */
0309   @Override
0310   public OperationStatus getOperationStatus(OperationHandle opHandle) throws HiveSQLException {
0311     try {
0312       TGetOperationStatusReq req = new TGetOperationStatusReq(opHandle.toTOperationHandle());
0313       TGetOperationStatusResp resp = cliService.GetOperationStatus(req);
0314       // Checks the status of the RPC call, throws an exception in case of error
0315       checkStatus(resp.getStatus());
0316       OperationState opState = OperationState.getOperationState(resp.getOperationState());
0317       HiveSQLException opException = null;
0318       if (opState == OperationState.ERROR) {
0319         opException = new HiveSQLException(resp.getErrorMessage(), resp.getSqlState(), resp.getErrorCode());
0320       }
0321       return new OperationStatus(opState, opException);
0322     } catch (HiveSQLException e) {
0323       throw e;
0324     } catch (Exception e) {
0325       throw new HiveSQLException(e);
0326     }
0327   }
0328 
0329   /* (non-Javadoc)
0330    * @see org.apache.hive.service.cli.ICLIService#cancelOperation(org.apache.hive.service.cli.OperationHandle)
0331    */
0332   @Override
0333   public void cancelOperation(OperationHandle opHandle) throws HiveSQLException {
0334     try {
0335       TCancelOperationReq req = new TCancelOperationReq(opHandle.toTOperationHandle());
0336       TCancelOperationResp resp = cliService.CancelOperation(req);
0337       checkStatus(resp.getStatus());
0338     } catch (HiveSQLException e) {
0339       throw e;
0340     } catch (Exception e) {
0341       throw new HiveSQLException(e);
0342     }
0343   }
0344 
0345   /* (non-Javadoc)
0346    * @see org.apache.hive.service.cli.ICLIService#closeOperation(org.apache.hive.service.cli.OperationHandle)
0347    */
0348   @Override
0349   public void closeOperation(OperationHandle opHandle)
0350       throws HiveSQLException {
0351     try {
0352       TCloseOperationReq req  = new TCloseOperationReq(opHandle.toTOperationHandle());
0353       TCloseOperationResp resp = cliService.CloseOperation(req);
0354       checkStatus(resp.getStatus());
0355     } catch (HiveSQLException e) {
0356       throw e;
0357     } catch (Exception e) {
0358       throw new HiveSQLException(e);
0359     }
0360   }
0361 
0362   /* (non-Javadoc)
0363    * @see org.apache.hive.service.cli.ICLIService#getResultSetMetadata(org.apache.hive.service.cli.OperationHandle)
0364    */
0365   @Override
0366   public TableSchema getResultSetMetadata(OperationHandle opHandle)
0367       throws HiveSQLException {
0368     try {
0369       TGetResultSetMetadataReq req = new TGetResultSetMetadataReq(opHandle.toTOperationHandle());
0370       TGetResultSetMetadataResp resp = cliService.GetResultSetMetadata(req);
0371       checkStatus(resp.getStatus());
0372       return new TableSchema(resp.getSchema());
0373     } catch (HiveSQLException e) {
0374       throw e;
0375     } catch (Exception e) {
0376       throw new HiveSQLException(e);
0377     }
0378   }
0379 
0380   @Override
0381   public RowSet fetchResults(OperationHandle opHandle, FetchOrientation orientation, long maxRows,
0382       FetchType fetchType) throws HiveSQLException {
0383     try {
0384       TFetchResultsReq req = new TFetchResultsReq();
0385       req.setOperationHandle(opHandle.toTOperationHandle());
0386       req.setOrientation(orientation.toTFetchOrientation());
0387       req.setMaxRows(maxRows);
0388       req.setFetchType(fetchType.toTFetchType());
0389       TFetchResultsResp resp = cliService.FetchResults(req);
0390       checkStatus(resp.getStatus());
0391       return RowSetFactory.create(resp.getResults(), opHandle.getProtocolVersion());
0392     } catch (HiveSQLException e) {
0393       throw e;
0394     } catch (Exception e) {
0395       throw new HiveSQLException(e);
0396     }
0397   }
0398 
0399   /* (non-Javadoc)
0400    * @see org.apache.hive.service.cli.ICLIService#fetchResults(org.apache.hive.service.cli.OperationHandle)
0401    */
0402   @Override
0403   public RowSet fetchResults(OperationHandle opHandle) throws HiveSQLException {
0404     // TODO: set the correct default fetch size
0405     return fetchResults(opHandle, FetchOrientation.FETCH_NEXT, 10000, FetchType.QUERY_OUTPUT);
0406   }
0407 
0408   @Override
0409   public String getDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory,
0410       String owner, String renewer) throws HiveSQLException {
0411     TGetDelegationTokenReq req = new TGetDelegationTokenReq(
0412         sessionHandle.toTSessionHandle(), owner, renewer);
0413     try {
0414       TGetDelegationTokenResp tokenResp = cliService.GetDelegationToken(req);
0415       checkStatus(tokenResp.getStatus());
0416       return tokenResp.getDelegationToken();
0417     } catch (Exception e) {
0418       throw new HiveSQLException(e);
0419     }
0420   }
0421 
0422   @Override
0423   public void cancelDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory,
0424       String tokenStr) throws HiveSQLException {
0425     TCancelDelegationTokenReq cancelReq = new TCancelDelegationTokenReq(
0426           sessionHandle.toTSessionHandle(), tokenStr);
0427     try {
0428       TCancelDelegationTokenResp cancelResp =
0429         cliService.CancelDelegationToken(cancelReq);
0430       checkStatus(cancelResp.getStatus());
0431       return;
0432     } catch (TException e) {
0433       throw new HiveSQLException(e);
0434     }
0435   }
0436 
0437   @Override
0438   public void renewDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory,
0439       String tokenStr) throws HiveSQLException {
0440     TRenewDelegationTokenReq cancelReq = new TRenewDelegationTokenReq(
0441         sessionHandle.toTSessionHandle(), tokenStr);
0442     try {
0443       TRenewDelegationTokenResp renewResp =
0444         cliService.RenewDelegationToken(cancelReq);
0445       checkStatus(renewResp.getStatus());
0446       return;
0447     } catch (Exception e) {
0448       throw new HiveSQLException(e);
0449     }
0450   }
0451 
0452   @Override
0453   public OperationHandle getPrimaryKeys(SessionHandle sessionHandle,
0454       String catalog, String schema, String table) throws HiveSQLException {
0455     try {
0456       TGetPrimaryKeysReq req = new TGetPrimaryKeysReq(sessionHandle.toTSessionHandle());
0457       req.setCatalogName(catalog);
0458       req.setSchemaName(schema);
0459       req.setTableName(table);
0460       TGetPrimaryKeysResp resp = cliService.GetPrimaryKeys(req);
0461       checkStatus(resp.getStatus());
0462       TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0463       return new OperationHandle(resp.getOperationHandle(), protocol);
0464     } catch (HiveSQLException e) {
0465       throw e;
0466     } catch (Exception e) {
0467       throw new HiveSQLException(e);
0468     }
0469   }
0470 
0471   @Override
0472   public OperationHandle getCrossReference(SessionHandle sessionHandle,
0473       String primaryCatalog, String primarySchema, String primaryTable,
0474       String foreignCatalog, String foreignSchema, String foreignTable) throws HiveSQLException {
0475     try {
0476       TGetCrossReferenceReq req = new TGetCrossReferenceReq(sessionHandle.toTSessionHandle());
0477       req.setParentCatalogName(primaryCatalog);
0478       req.setParentSchemaName(primarySchema);
0479       req.setParentTableName(primaryTable);
0480       req.setForeignCatalogName(foreignCatalog);
0481       req.setForeignSchemaName(foreignSchema);
0482       req.setForeignTableName(foreignTable);
0483       TGetCrossReferenceResp resp = cliService.GetCrossReference(req);
0484       checkStatus(resp.getStatus());
0485       TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0486       return new OperationHandle(resp.getOperationHandle(), protocol);
0487     } catch (HiveSQLException e) {
0488       throw e;
0489     } catch (Exception e) {
0490       throw new HiveSQLException(e);
0491     }
0492   }
0493 }