use of org.apache.hive.service.cli.OperationStatus in project hive by apache.
the class ThriftCLIServiceTest method testExecuteStatementAsync.
/**
* Test asynchronous query execution and error reporting to the client
* @throws Exception
*/
@Test
public void testExecuteStatementAsync() throws Exception {
Map<String, String> opConf = new HashMap<String, String>();
// Open a new client session
SessionHandle sessHandle = client.openSession(USERNAME, PASSWORD, opConf);
// Session handle should not be null
assertNotNull("Session handle should not be null", sessHandle);
OperationHandle opHandle;
OperationStatus opStatus;
OperationState state = null;
// Change lock manager to embedded mode
String queryString = "SET hive.lock.manager=" + "org.apache.hadoop.hive.ql.lockmgr.EmbeddedLockManager";
client.executeStatement(sessHandle, queryString, opConf);
// Drop the table if it exists
queryString = "DROP TABLE IF EXISTS TEST_EXEC_ASYNC_THRIFT";
client.executeStatement(sessHandle, queryString, opConf);
// Create a test table
queryString = "CREATE TABLE TEST_EXEC_ASYNC_THRIFT(ID STRING)";
client.executeStatement(sessHandle, queryString, opConf);
// Execute another query
queryString = "SELECT ID+1 FROM TEST_EXEC_ASYNC_THRIFT";
System.out.println("Will attempt to execute: " + queryString);
opHandle = client.executeStatementAsync(sessHandle, queryString, opConf);
assertNotNull(opHandle);
// Poll on the operation status till the query is completed
boolean isQueryRunning = true;
long pollTimeout = System.currentTimeMillis() + 100000;
while (isQueryRunning) {
// Break if polling times out
if (System.currentTimeMillis() > pollTimeout) {
System.out.println("Polling timed out");
break;
}
opStatus = client.getOperationStatus(opHandle, false);
assertNotNull(opStatus);
state = opStatus.getState();
System.out.println("Current state: " + state);
if (state == OperationState.CANCELED || state == OperationState.CLOSED || state == OperationState.FINISHED || state == OperationState.ERROR) {
isQueryRunning = false;
}
Thread.sleep(1000);
}
// Expect query to be successfully completed now
assertEquals("Query should be finished", OperationState.FINISHED, state);
// Execute a malformed query
// This query will give a runtime error
queryString = "CREATE TABLE NON_EXISTING_TAB (ID STRING) location 'hdfs://localhost:10000/a/b/c'";
System.out.println("Will attempt to execute: " + queryString);
opHandle = client.executeStatementAsync(sessHandle, queryString, opConf);
assertNotNull(opHandle);
opStatus = client.getOperationStatus(opHandle, false);
assertNotNull(opStatus);
isQueryRunning = true;
pollTimeout = System.currentTimeMillis() + 100000;
while (isQueryRunning) {
// Break if polling times out
if (System.currentTimeMillis() > pollTimeout) {
System.out.println("Polling timed out");
break;
}
state = opStatus.getState();
System.out.println("Current state: " + state);
if (state == OperationState.CANCELED || state == OperationState.CLOSED || state == OperationState.FINISHED || state == OperationState.ERROR) {
isQueryRunning = false;
}
Thread.sleep(1000);
opStatus = client.getOperationStatus(opHandle, false);
}
// Expect query to return an error state
assertEquals("Operation should be in error state", OperationState.ERROR, state);
// sqlState, errorCode should be set to appropriate values
assertEquals(opStatus.getOperationException().getSQLState(), "08S01");
assertEquals(opStatus.getOperationException().getErrorCode(), 1);
// Cleanup
queryString = "DROP TABLE TEST_EXEC_ASYNC_THRIFT";
client.executeStatement(sessHandle, queryString, opConf);
client.closeSession(sessHandle);
}
use of org.apache.hive.service.cli.OperationStatus in project hive by apache.
the class TestOperationLoggingAPIWithMr method testFetchResultsOfLogAsync.
@Test
public void testFetchResultsOfLogAsync() throws Exception {
// verify whether the sql operation log is generated and fetch correctly in async mode.
OperationHandle operationHandle = client.executeStatementAsync(sessionHandle, sql, null);
// Poll on the operation status till the query is completed
boolean isQueryRunning = true;
long pollTimeout = System.currentTimeMillis() + 100000;
OperationStatus opStatus;
OperationState state = null;
RowSet rowSetAccumulated = null;
StringBuilder logs = new StringBuilder();
while (isQueryRunning) {
// Break if polling times out
if (System.currentTimeMillis() > pollTimeout) {
break;
}
opStatus = client.getOperationStatus(operationHandle, false);
Assert.assertNotNull(opStatus);
state = opStatus.getState();
rowSetAccumulated = client.fetchResults(operationHandle, FetchOrientation.FETCH_NEXT, 2000, FetchType.LOG);
for (Object[] row : rowSetAccumulated) {
logs.append(row[0]);
}
if (state == OperationState.CANCELED || state == OperationState.CLOSED || state == OperationState.FINISHED || state == OperationState.ERROR) {
isQueryRunning = false;
}
Thread.sleep(10);
}
// The sql should be completed now.
Assert.assertEquals("Query should be finished", OperationState.FINISHED, state);
// Verify the accumulated logs
verifyFetchedLogPost(logs.toString(), expectedLogsVerbose, true);
// Verify the fetched logs from the beginning of the log file
RowSet rowSet = client.fetchResults(operationHandle, FetchOrientation.FETCH_FIRST, 2000, FetchType.LOG);
verifyFetchedLog(rowSet, expectedLogsVerbose);
}
use of org.apache.hive.service.cli.OperationStatus in project cdap by caskdata.
the class Hive12CDH5ExploreService method doFetchStatus.
@Override
protected QueryStatus doFetchStatus(OperationHandle handle) throws HiveSQLException, ExploreException, HandleNotFoundException {
OperationStatus operationStatus = getCliService().getOperationStatus(handle);
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") HiveSQLException hiveExn = operationStatus.getOperationException();
if (hiveExn != null) {
return new QueryStatus(hiveExn.getMessage(), hiveExn.getSQLState());
}
return new QueryStatus(QueryStatus.OpStatus.valueOf(operationStatus.getState().toString()), handle.hasResultSet());
}
use of org.apache.hive.service.cli.OperationStatus in project cdap by caskdata.
the class Hive13ExploreService method doFetchStatus.
@Override
protected QueryStatus doFetchStatus(OperationHandle operationHandle) throws HiveSQLException, ExploreException, HandleNotFoundException {
OperationStatus operationStatus = getCliService().getOperationStatus(operationHandle);
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") HiveSQLException hiveExn = operationStatus.getOperationException();
if (hiveExn != null) {
return new QueryStatus(hiveExn.getMessage(), hiveExn.getSQLState());
}
return new QueryStatus(QueryStatus.OpStatus.valueOf(operationStatus.getState().toString()), operationHandle.hasResultSet());
}
Aggregations