Search in sources :

Example 1 with StartQueryExecutionRequest

use of com.amazonaws.services.athena.model.StartQueryExecutionRequest in project aws-athena-query-federation by awslabs.

the class IntegrationTestBase method startQueryExecution.

/**
 * Sends a DB query via Athena and returns the query results.
 * @param query - The query string to be processed by Athena.
 * @return The query results object containing the metadata and row information.
 * @throws RuntimeException The Query is cancelled or has failed.
 */
public GetQueryResultsResult startQueryExecution(String query) throws RuntimeException {
    StartQueryExecutionRequest startQueryExecutionRequest = new StartQueryExecutionRequest().withWorkGroup(athenaWorkgroup).withQueryString(query);
    String queryExecutionId = sendAthenaQuery(startQueryExecutionRequest);
    logger.info("Query: [{}], Query Id: [{}]", query, queryExecutionId);
    waitForAthenaQueryResults(queryExecutionId);
    GetQueryResultsResult getQueryResultsResult = getAthenaQueryResults(queryExecutionId);
    return getQueryResultsResult;
}
Also used : StartQueryExecutionRequest(com.amazonaws.services.athena.model.StartQueryExecutionRequest) GetQueryResultsResult(com.amazonaws.services.athena.model.GetQueryResultsResult)

Example 2 with StartQueryExecutionRequest

use of com.amazonaws.services.athena.model.StartQueryExecutionRequest in project aws-doc-sdk-examples by awsdocs.

the class StartQueryExample method submitAthenaQuery.

/**
 * Submits a sample query to Athena and returns the execution ID of the query.
 */
private static String submitAthenaQuery(AmazonAthena athenaClient) {
    // The QueryExecutionContext allows us to set the Database.
    QueryExecutionContext queryExecutionContext = new QueryExecutionContext().withDatabase(ExampleConstants.ATHENA_DEFAULT_DATABASE);
    // The result configuration specifies where the results of the query should go in S3 and encryption options
    ResultConfiguration resultConfiguration = new ResultConfiguration().withOutputLocation(ExampleConstants.ATHENA_OUTPUT_BUCKET);
    // Create the StartQueryExecutionRequest to send to Athena which will start the query.
    StartQueryExecutionRequest startQueryExecutionRequest = new StartQueryExecutionRequest().withQueryString(ExampleConstants.ATHENA_SAMPLE_QUERY).withQueryExecutionContext(queryExecutionContext).withResultConfiguration(resultConfiguration);
    StartQueryExecutionResult startQueryExecutionResult = athenaClient.startQueryExecution(startQueryExecutionRequest);
    return startQueryExecutionResult.getQueryExecutionId();
}
Also used : StartQueryExecutionResult(com.amazonaws.services.athena.model.StartQueryExecutionResult) ResultConfiguration(com.amazonaws.services.athena.model.ResultConfiguration) QueryExecutionContext(com.amazonaws.services.athena.model.QueryExecutionContext) StartQueryExecutionRequest(com.amazonaws.services.athena.model.StartQueryExecutionRequest)

Example 3 with StartQueryExecutionRequest

use of com.amazonaws.services.athena.model.StartQueryExecutionRequest in project aws-doc-sdk-examples by awsdocs.

the class StopQueryExecutionExample method submitAthenaQuery.

/**
 * 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();
}
Also used : StartQueryExecutionResult(com.amazonaws.services.athena.model.StartQueryExecutionResult) ResultConfiguration(com.amazonaws.services.athena.model.ResultConfiguration) QueryExecutionContext(com.amazonaws.services.athena.model.QueryExecutionContext) StartQueryExecutionRequest(com.amazonaws.services.athena.model.StartQueryExecutionRequest)

Example 4 with StartQueryExecutionRequest

use of com.amazonaws.services.athena.model.StartQueryExecutionRequest in project cerberus by Nike-Inc.

the class AthenaService method addPartitionIfMissing.

public void addPartitionIfMissing(String region, String bucket, String year, String month, String day, String hour) {
    String partition = String.format("year=%s/month=%s/day=%s/hour=%s", year, month, day, hour);
    String table = String.format(TABLE_TEMPLATE, environmentName);
    if (!partitions.contains(partition)) {
        try {
            String query = String.format("ALTER TABLE %s ADD PARTITION (year='%s', month='%s', day='%s', hour='%s') " + "LOCATION 's3://%s/audit-logs/partitioned/year=%s/month=%s/day=%s/hour=%s'", table, year, month, day, hour, bucket, year, month, day, hour);
            AmazonAthena athena = athenaClientFactory.getClient(region);
            StartQueryExecutionResult result = athena.startQueryExecution(new StartQueryExecutionRequest().withQueryString(query).withResultConfiguration(new ResultConfiguration().withOutputLocation(String.format("s3://%s/results/", bucket))));
            log.debug("Started query: '{}' to add partition: '{}' to table: '{}'", result.getQueryExecutionId(), partition, table);
            partitions.add(partition);
        } catch (AmazonClientException e) {
            log.error("Failed to start add partition query for year={}/month={}/day={}/hour={}", year, month, day, hour, e);
        }
    }
}
Also used : StartQueryExecutionResult(com.amazonaws.services.athena.model.StartQueryExecutionResult) ResultConfiguration(com.amazonaws.services.athena.model.ResultConfiguration) AmazonClientException(com.amazonaws.AmazonClientException) StartQueryExecutionRequest(com.amazonaws.services.athena.model.StartQueryExecutionRequest) AmazonAthena(com.amazonaws.services.athena.AmazonAthena)

Aggregations

StartQueryExecutionRequest (com.amazonaws.services.athena.model.StartQueryExecutionRequest)4 ResultConfiguration (com.amazonaws.services.athena.model.ResultConfiguration)3 StartQueryExecutionResult (com.amazonaws.services.athena.model.StartQueryExecutionResult)3 QueryExecutionContext (com.amazonaws.services.athena.model.QueryExecutionContext)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonAthena (com.amazonaws.services.athena.AmazonAthena)1 GetQueryResultsResult (com.amazonaws.services.athena.model.GetQueryResultsResult)1