0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
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.thrift.TException;
0027
0028
0029
0030
0031
0032 public class ThriftCLIServiceClient extends CLIServiceClient {
0033 private final TCLIService.Iface cliService;
0034
0035 public ThriftCLIServiceClient(TCLIService.Iface cliService) {
0036 this.cliService = cliService;
0037 }
0038
0039 public void checkStatus(TStatus status) throws HiveSQLException {
0040 if (TStatusCode.ERROR_STATUS.equals(status.getStatusCode())) {
0041 throw new HiveSQLException(status);
0042 }
0043 }
0044
0045
0046
0047
0048 @Override
0049 public SessionHandle openSession(String username, String password,
0050 Map<String, String> configuration)
0051 throws HiveSQLException {
0052 try {
0053 TOpenSessionReq req = new TOpenSessionReq();
0054 req.setUsername(username);
0055 req.setPassword(password);
0056 req.setConfiguration(configuration);
0057 TOpenSessionResp resp = cliService.OpenSession(req);
0058 checkStatus(resp.getStatus());
0059 return new SessionHandle(resp.getSessionHandle(), resp.getServerProtocolVersion());
0060 } catch (HiveSQLException e) {
0061 throw e;
0062 } catch (Exception e) {
0063 throw new HiveSQLException(e);
0064 }
0065 }
0066
0067
0068
0069
0070 @Override
0071 public SessionHandle openSessionWithImpersonation(String username, String password,
0072 Map<String, String> configuration, String delegationToken) throws HiveSQLException {
0073 throw new HiveSQLException("open with impersonation operation is not supported in the client");
0074 }
0075
0076
0077
0078
0079 @Override
0080 public void closeSession(SessionHandle sessionHandle) throws HiveSQLException {
0081 try {
0082 TCloseSessionReq req = new TCloseSessionReq(sessionHandle.toTSessionHandle());
0083 TCloseSessionResp resp = cliService.CloseSession(req);
0084 checkStatus(resp.getStatus());
0085 } catch (HiveSQLException e) {
0086 throw e;
0087 } catch (Exception e) {
0088 throw new HiveSQLException(e);
0089 }
0090 }
0091
0092
0093
0094
0095 @Override
0096 public GetInfoValue getInfo(SessionHandle sessionHandle, GetInfoType infoType)
0097 throws HiveSQLException {
0098 try {
0099
0100 TGetInfoReq req = new TGetInfoReq(sessionHandle.toTSessionHandle(), infoType.toTGetInfoType());
0101 TGetInfoResp resp = cliService.GetInfo(req);
0102 checkStatus(resp.getStatus());
0103 return new GetInfoValue(resp.getInfoValue());
0104 } catch (HiveSQLException e) {
0105 throw e;
0106 } catch (Exception e) {
0107 throw new HiveSQLException(e);
0108 }
0109 }
0110
0111
0112
0113
0114 @Override
0115 public OperationHandle executeStatement(SessionHandle sessionHandle, String statement,
0116 Map<String, String> confOverlay)
0117 throws HiveSQLException {
0118 return executeStatementInternal(sessionHandle, statement, confOverlay, false);
0119 }
0120
0121
0122
0123
0124 @Override
0125 public OperationHandle executeStatementAsync(SessionHandle sessionHandle, String statement,
0126 Map<String, String> confOverlay)
0127 throws HiveSQLException {
0128 return executeStatementInternal(sessionHandle, statement, confOverlay, true);
0129 }
0130
0131 private OperationHandle executeStatementInternal(SessionHandle sessionHandle, String statement,
0132 Map<String, String> confOverlay, boolean isAsync)
0133 throws HiveSQLException {
0134 try {
0135 TExecuteStatementReq req =
0136 new TExecuteStatementReq(sessionHandle.toTSessionHandle(), statement);
0137 req.setConfOverlay(confOverlay);
0138 req.setRunAsync(isAsync);
0139 TExecuteStatementResp resp = cliService.ExecuteStatement(req);
0140 checkStatus(resp.getStatus());
0141 TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0142 return new OperationHandle(resp.getOperationHandle(), protocol);
0143 } catch (HiveSQLException e) {
0144 throw e;
0145 } catch (Exception e) {
0146 throw new HiveSQLException(e);
0147 }
0148 }
0149
0150
0151
0152
0153 @Override
0154 public OperationHandle getTypeInfo(SessionHandle sessionHandle) throws HiveSQLException {
0155 try {
0156 TGetTypeInfoReq req = new TGetTypeInfoReq(sessionHandle.toTSessionHandle());
0157 TGetTypeInfoResp resp = cliService.GetTypeInfo(req);
0158 checkStatus(resp.getStatus());
0159 TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0160 return new OperationHandle(resp.getOperationHandle(), protocol);
0161 } catch (HiveSQLException e) {
0162 throw e;
0163 } catch (Exception e) {
0164 throw new HiveSQLException(e);
0165 }
0166 }
0167
0168
0169
0170
0171 @Override
0172 public OperationHandle getCatalogs(SessionHandle sessionHandle) throws HiveSQLException {
0173 try {
0174 TGetCatalogsReq req = new TGetCatalogsReq(sessionHandle.toTSessionHandle());
0175 TGetCatalogsResp resp = cliService.GetCatalogs(req);
0176 checkStatus(resp.getStatus());
0177 TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0178 return new OperationHandle(resp.getOperationHandle(), protocol);
0179 } catch (HiveSQLException e) {
0180 throw e;
0181 } catch (Exception e) {
0182 throw new HiveSQLException(e);
0183 }
0184 }
0185
0186
0187
0188
0189 @Override
0190 public OperationHandle getSchemas(SessionHandle sessionHandle, String catalogName,
0191 String schemaName)
0192 throws HiveSQLException {
0193 try {
0194 TGetSchemasReq req = new TGetSchemasReq(sessionHandle.toTSessionHandle());
0195 req.setCatalogName(catalogName);
0196 req.setSchemaName(schemaName);
0197 TGetSchemasResp resp = cliService.GetSchemas(req);
0198 checkStatus(resp.getStatus());
0199 TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0200 return new OperationHandle(resp.getOperationHandle(), protocol);
0201 } catch (HiveSQLException e) {
0202 throw e;
0203 } catch (Exception e) {
0204 throw new HiveSQLException(e);
0205 }
0206 }
0207
0208
0209
0210
0211 @Override
0212 public OperationHandle getTables(SessionHandle sessionHandle, String catalogName,
0213 String schemaName, String tableName, List<String> tableTypes)
0214 throws HiveSQLException {
0215 try {
0216 TGetTablesReq req = new TGetTablesReq(sessionHandle.toTSessionHandle());
0217 req.setTableName(tableName);
0218 req.setTableTypes(tableTypes);
0219 req.setSchemaName(schemaName);
0220 TGetTablesResp resp = cliService.GetTables(req);
0221 checkStatus(resp.getStatus());
0222 TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0223 return new OperationHandle(resp.getOperationHandle(), protocol);
0224 } catch (HiveSQLException e) {
0225 throw e;
0226 } catch (Exception e) {
0227 throw new HiveSQLException(e);
0228 }
0229 }
0230
0231
0232
0233
0234 @Override
0235 public OperationHandle getTableTypes(SessionHandle sessionHandle) throws HiveSQLException {
0236 try {
0237 TGetTableTypesReq req = new TGetTableTypesReq(sessionHandle.toTSessionHandle());
0238 TGetTableTypesResp resp = cliService.GetTableTypes(req);
0239 checkStatus(resp.getStatus());
0240 TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0241 return new OperationHandle(resp.getOperationHandle(), protocol);
0242 } catch (HiveSQLException e) {
0243 throw e;
0244 } catch (Exception e) {
0245 throw new HiveSQLException(e);
0246 }
0247 }
0248
0249
0250
0251
0252 @Override
0253 public OperationHandle getColumns(SessionHandle sessionHandle,
0254 String catalogName, String schemaName, String tableName, String columnName)
0255 throws HiveSQLException {
0256 try {
0257 TGetColumnsReq req = new TGetColumnsReq();
0258 req.setSessionHandle(sessionHandle.toTSessionHandle());
0259 req.setCatalogName(catalogName);
0260 req.setSchemaName(schemaName);
0261 req.setTableName(tableName);
0262 req.setColumnName(columnName);
0263 TGetColumnsResp resp = cliService.GetColumns(req);
0264 checkStatus(resp.getStatus());
0265 TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0266 return new OperationHandle(resp.getOperationHandle(), protocol);
0267 } catch (HiveSQLException e) {
0268 throw e;
0269 } catch (Exception e) {
0270 throw new HiveSQLException(e);
0271 }
0272 }
0273
0274
0275
0276
0277 @Override
0278 public OperationHandle getFunctions(SessionHandle sessionHandle,
0279 String catalogName, String schemaName, String functionName) throws HiveSQLException {
0280 try {
0281 TGetFunctionsReq req = new TGetFunctionsReq(sessionHandle.toTSessionHandle(), functionName);
0282 req.setCatalogName(catalogName);
0283 req.setSchemaName(schemaName);
0284 TGetFunctionsResp resp = cliService.GetFunctions(req);
0285 checkStatus(resp.getStatus());
0286 TProtocolVersion protocol = sessionHandle.getProtocolVersion();
0287 return new OperationHandle(resp.getOperationHandle(), protocol);
0288 } catch (HiveSQLException e) {
0289 throw e;
0290 } catch (Exception e) {
0291 throw new HiveSQLException(e);
0292 }
0293 }
0294
0295
0296
0297
0298 @Override
0299 public OperationStatus getOperationStatus(OperationHandle opHandle) throws HiveSQLException {
0300 try {
0301 TGetOperationStatusReq req = new TGetOperationStatusReq(opHandle.toTOperationHandle());
0302 TGetOperationStatusResp resp = cliService.GetOperationStatus(req);
0303
0304 checkStatus(resp.getStatus());
0305 OperationState opState = OperationState.getOperationState(resp.getOperationState());
0306 HiveSQLException opException = null;
0307 if (opState == OperationState.ERROR) {
0308 opException = new HiveSQLException(resp.getErrorMessage(), resp.getSqlState(), resp.getErrorCode());
0309 }
0310 return new OperationStatus(opState, opException);
0311 } catch (HiveSQLException e) {
0312 throw e;
0313 } catch (Exception e) {
0314 throw new HiveSQLException(e);
0315 }
0316 }
0317
0318
0319
0320
0321 @Override
0322 public void cancelOperation(OperationHandle opHandle) throws HiveSQLException {
0323 try {
0324 TCancelOperationReq req = new TCancelOperationReq(opHandle.toTOperationHandle());
0325 TCancelOperationResp resp = cliService.CancelOperation(req);
0326 checkStatus(resp.getStatus());
0327 } catch (HiveSQLException e) {
0328 throw e;
0329 } catch (Exception e) {
0330 throw new HiveSQLException(e);
0331 }
0332 }
0333
0334
0335
0336
0337 @Override
0338 public void closeOperation(OperationHandle opHandle)
0339 throws HiveSQLException {
0340 try {
0341 TCloseOperationReq req = new TCloseOperationReq(opHandle.toTOperationHandle());
0342 TCloseOperationResp resp = cliService.CloseOperation(req);
0343 checkStatus(resp.getStatus());
0344 } catch (HiveSQLException e) {
0345 throw e;
0346 } catch (Exception e) {
0347 throw new HiveSQLException(e);
0348 }
0349 }
0350
0351
0352
0353
0354 @Override
0355 public TableSchema getResultSetMetadata(OperationHandle opHandle)
0356 throws HiveSQLException {
0357 try {
0358 TGetResultSetMetadataReq req = new TGetResultSetMetadataReq(opHandle.toTOperationHandle());
0359 TGetResultSetMetadataResp resp = cliService.GetResultSetMetadata(req);
0360 checkStatus(resp.getStatus());
0361 return new TableSchema(resp.getSchema());
0362 } catch (HiveSQLException e) {
0363 throw e;
0364 } catch (Exception e) {
0365 throw new HiveSQLException(e);
0366 }
0367 }
0368
0369 @Override
0370 public RowSet fetchResults(OperationHandle opHandle, FetchOrientation orientation, long maxRows,
0371 FetchType fetchType) throws HiveSQLException {
0372 try {
0373 TFetchResultsReq req = new TFetchResultsReq();
0374 req.setOperationHandle(opHandle.toTOperationHandle());
0375 req.setOrientation(orientation.toTFetchOrientation());
0376 req.setMaxRows(maxRows);
0377 req.setFetchType(fetchType.toTFetchType());
0378 TFetchResultsResp resp = cliService.FetchResults(req);
0379 checkStatus(resp.getStatus());
0380 return RowSetFactory.create(resp.getResults(), opHandle.getProtocolVersion());
0381 } catch (HiveSQLException e) {
0382 throw e;
0383 } catch (Exception e) {
0384 throw new HiveSQLException(e);
0385 }
0386 }
0387
0388
0389
0390
0391 @Override
0392 public RowSet fetchResults(OperationHandle opHandle) throws HiveSQLException {
0393
0394 return fetchResults(opHandle, FetchOrientation.FETCH_NEXT, 10000, FetchType.QUERY_OUTPUT);
0395 }
0396
0397 @Override
0398 public String getDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory,
0399 String owner, String renewer) throws HiveSQLException {
0400 TGetDelegationTokenReq req = new TGetDelegationTokenReq(
0401 sessionHandle.toTSessionHandle(), owner, renewer);
0402 try {
0403 TGetDelegationTokenResp tokenResp = cliService.GetDelegationToken(req);
0404 checkStatus(tokenResp.getStatus());
0405 return tokenResp.getDelegationToken();
0406 } catch (Exception e) {
0407 throw new HiveSQLException(e);
0408 }
0409 }
0410
0411 @Override
0412 public void cancelDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory,
0413 String tokenStr) throws HiveSQLException {
0414 TCancelDelegationTokenReq cancelReq = new TCancelDelegationTokenReq(
0415 sessionHandle.toTSessionHandle(), tokenStr);
0416 try {
0417 TCancelDelegationTokenResp cancelResp =
0418 cliService.CancelDelegationToken(cancelReq);
0419 checkStatus(cancelResp.getStatus());
0420 return;
0421 } catch (TException e) {
0422 throw new HiveSQLException(e);
0423 }
0424 }
0425
0426 @Override
0427 public void renewDelegationToken(SessionHandle sessionHandle, HiveAuthFactory authFactory,
0428 String tokenStr) throws HiveSQLException {
0429 TRenewDelegationTokenReq cancelReq = new TRenewDelegationTokenReq(
0430 sessionHandle.toTSessionHandle(), tokenStr);
0431 try {
0432 TRenewDelegationTokenResp renewResp =
0433 cliService.RenewDelegationToken(cancelReq);
0434 checkStatus(renewResp.getStatus());
0435 return;
0436 } catch (Exception e) {
0437 throw new HiveSQLException(e);
0438 }
0439 }
0440 }