Reputation: 123
Is there anyway to get the Athena query ID
for a query I submit Athena JDBC driver? Any API calls to the status of a Query (with its query ID?)
Any way to get the query history via API call?
Upvotes: 7
Views: 2876
Reputation: 7603
just to add my 2 cents and complete the answer. The question was to get the ID and the status of the query. To get the ID I used the example ListQueryExecutionsExample.java from the was docs. But to get the complete status of the query you can retrieve the QueryExecution object that contains the QueryExecutionStatus. This example below retrieves more detailed information:
import com.amazonaws.services.athena.AmazonAthena;
import com.amazonaws.services.athena.model.BatchGetQueryExecutionRequest;
import com.amazonaws.services.athena.model.BatchGetQueryExecutionResult;
import com.amazonaws.services.athena.model.ListQueryExecutionsRequest;
import com.amazonaws.services.athena.model.ListQueryExecutionsResult;
import com.amazonaws.services.athena.model.QueryExecution;
import java.util.List;
import java.util.function.Predicate;
public class ListQueryExecutionsExample {
public static void main(String[] args) throws Exception {
// Build an Athena client
AthenaClientFactory factory = new AthenaClientFactory();
AmazonAthena athenaClient = factory.createClient();
// Build the request
ListQueryExecutionsRequest listQueryExecutionsRequest = new ListQueryExecutionsRequest();
// Get the list results.
ListQueryExecutionsResult listQueryExecutionsResult =
athenaClient.listQueryExecutions(listQueryExecutionsRequest);
// Process the results.
boolean hasMoreResults = true;
while (hasMoreResults) {
List<String> queryExecutionIds = listQueryExecutionsResult.getQueryExecutionIds();
// System.out.println(queryExecutionIds);
BatchGetQueryExecutionRequest batchGetQueryExecutionRequest =
new BatchGetQueryExecutionRequest().withQueryExecutionIds(queryExecutionIds);
BatchGetQueryExecutionResult batchGetQueryExecutionResult =
athenaClient.batchGetQueryExecution(batchGetQueryExecutionRequest);
// QueryExecution has all detail information about the queries.
List<QueryExecution> queryExecutionList = batchGetQueryExecutionResult.getQueryExecutions();
queryExecutionList.stream().forEach(System.out::println);
//If nextToken is not null, then there are more results. Get the next page of results.
if (listQueryExecutionsResult.getNextToken() != null) {
listQueryExecutionsResult = athenaClient.listQueryExecutions(
listQueryExecutionsRequest.withNextToken(listQueryExecutionsResult.getNextToken()));
} else {
hasMoreResults = false;
}
}
}
}
Upvotes: 2
Reputation: 423
With the JDBC driver (SimbaAthenaJDBC_2.0.13) you can do this:
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM table");
String queryId = stmt.unwrap(IStatementQueryInfoProvider.class).getQueryId();
Upvotes: 1
Reputation: 3361
I am using the following piece of code to retrieve the query id:
AthenaResultSet rs = (AthenaResultSet) statement.executeQuery(query);
Object queryId = ((AthenaStatementClient)rs.getClient()).getQueryExecutionId();
Upvotes: 3
Reputation: 311
Yes, it is possible to register the query IDS such as log messages. Those messages will be stored in a log file that you may specify while connecting with the JDBC driver.
Upvotes: 2