Example usage for com.amazonaws.services.athena AmazonAthena getQueryResults

List of usage examples for com.amazonaws.services.athena AmazonAthena getQueryResults

Introduction

In this page you can find the example usage for com.amazonaws.services.athena AmazonAthena getQueryResults.

Prototype

GetQueryResultsResult getQueryResults(GetQueryResultsRequest getQueryResultsRequest);

Source Link

Document

Streams the results of a single query execution specified by QueryExecutionId from the Athena query results location in Amazon S3.

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
 *//*w ww  . j  a va  2 s.co 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));
}