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

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

Introduction

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

Prototype

StartQueryExecutionResult startQueryExecution(StartQueryExecutionRequest startQueryExecutionRequest);

Source Link

Document

Runs the SQL query statements contained in the Query.

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 ww .j  a v a  2  s . 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));
}