AWS Code Sample

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above. demonstrates how to stop a query and check its status.

package aws.example.athena; import; import; import; import; import; import; import; import; import; import; /** * StopQueryExecutionExample * ------------------------------------- * This code runs an example query, immediately stops the query, and checks the status of the query to * ensure that it was cancelled. */ public class StopQueryExecutionExample { public static void main(String[] args) throws Exception { // Build an Athena client AthenaClientFactory factory = new AthenaClientFactory(); AmazonAthena athenaClient = factory.createClient(); String sampleQueryExecutionId = submitAthenaQuery(athenaClient); // Submit the stop query Request StopQueryExecutionRequest stopQueryExecutionRequest = new StopQueryExecutionRequest() .withQueryExecutionId(sampleQueryExecutionId); StopQueryExecutionResult stopQueryExecutionResult = athenaClient.stopQueryExecution(stopQueryExecutionRequest); // Ensure that the query was stopped GetQueryExecutionRequest getQueryExecutionRequest = new GetQueryExecutionRequest() .withQueryExecutionId(sampleQueryExecutionId); GetQueryExecutionResult getQueryExecutionResult = athenaClient.getQueryExecution(getQueryExecutionRequest); if (getQueryExecutionResult.getQueryExecution().getStatus().getState().equals(QueryExecutionState.CANCELLED)) { // Query was cancelled. System.out.println("Query has been cancelled"); } } /** * Submits an example query and returns a query execution ID of a running query to stop. */ public static String submitAthenaQuery(AmazonAthena athenaClient) { QueryExecutionContext queryExecutionContext = new QueryExecutionContext().withDatabase(ExampleConstants.ATHENA_DEFAULT_DATABASE); ResultConfiguration resultConfiguration = new ResultConfiguration() .withOutputLocation(ExampleConstants.ATHENA_OUTPUT_BUCKET); StartQueryExecutionRequest startQueryExecutionRequest = new StartQueryExecutionRequest() .withQueryString(ExampleConstants.ATHENA_SAMPLE_QUERY) .withQueryExecutionContext(queryExecutionContext) .withResultConfiguration(resultConfiguration); StartQueryExecutionResult startQueryExecutionResult = athenaClient.startQueryExecution(startQueryExecutionRequest); return startQueryExecutionResult.getQueryExecutionId(); } }

Sample Details

Service: athena

Last tested: 2018-06-25

Author: soo-aws

Type: full-example

On this page: