Example usage for com.amazonaws.services.athena.model StartQueryExecutionRequest StartQueryExecutionRequest

List of usage examples for com.amazonaws.services.athena.model StartQueryExecutionRequest StartQueryExecutionRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.athena.model StartQueryExecutionRequest StartQueryExecutionRequest.

Prototype

StartQueryExecutionRequest

Source Link

Usage

From source file:com.nike.cerberus.lambda.waf.AthenaQuery.java

License:Apache License

/**
 * Executes an Athena query and waits for it to finish returning the results
 *//*from  w w  w  . j  av a 2s . c  o  m*/
private GetQueryResultsResult executeAthenaQuery(String query) throws InterruptedException {

    logger.info("QUERY: " + query);

    AmazonAthena athena = AmazonAthenaClient.builder().withRegion(Regions.US_WEST_2)
            .withCredentials(DefaultAWSCredentialsProviderChain.getInstance()).build();

    StartQueryExecutionResult result = athena.startQueryExecution(new StartQueryExecutionRequest()
            .withQueryString(query).withResultConfiguration(new ResultConfiguration().withOutputLocation(
                    "s3://aws-athena-query-results-933764306573-us-west-2/ip-address-translator")));

    String id = result.getQueryExecutionId();

    String state;
    do {
        state = athena.getQueryExecution(new GetQueryExecutionRequest().withQueryExecutionId(id))
                .getQueryExecution().getStatus().getState();
        logger.info(String.format("Polling for query to finish: current status: %s", state));
        Thread.sleep(1000);
    } while (state.equals("RUNNING"));

    logger.info(String.format("The query: %s is in state: %s, fetching results", id, state));

    return athena.getQueryResults(new GetQueryResultsRequest().withQueryExecutionId(id));
}