코드 샘플 - Amazon Athena

코드 샘플

이 주제의 예제에서는 Athena 애플리케이션 작성의 출발점으로 SDK for Java 2.x를 사용합니다.

참고

다른 언어별 AWS SDK를 사용하여 Athena를 프로그래밍하는 방법에 대한 자세한 내용은 다음 리소스를 참조하십시오.

이 섹션의 Java 코드 예제 실행에 대한 자세한 내용은 GitHub의 AWS 코드 예제 리포지토리에 있는 Amazon Athena Java Readme를 참조하십시오. Athena용 Java 프로그래밍 참조는 AWS SDK for Java 2.x의 AthenaClient를 참조하십시오.

참고

이 샘플은 문자열에 상수(예: ATHENA_SAMPLE_QUERY)를 사용합니다. 이는 ExampleConstants.java 클래스 선언에 정의되어 있습니다. 이 상수를 사용자의 고유한 문자열 또는 정의된 상수로 바꿉니다.

상수

ExampleConstants.java 클래스는 Athena의 시작하기 자습서에서 생성한 테이블을 쿼리하는 방법을 보여줍니다.

package aws.example.athena; public class ExampleConstants { public static final int CLIENT_EXECUTION_TIMEOUT = 100000; public static final String ATHENA_OUTPUT_BUCKET = "s3://bucketscott2"; // change the Amazon S3 bucket name to match // your environment // Demonstrates how to query a table with a comma-separated value (CSV) table. // For information, see // https://docs.aws.amazon.com/athena/latest/ug/work-with-data.html public static final String ATHENA_SAMPLE_QUERY = "SELECT * FROM scott2;"; // change the Query statement to match // your environment public static final long SLEEP_AMOUNT_IN_MS = 1000; public static final String ATHENA_DEFAULT_DATABASE = "mydatabase"; // change the database to match your database }

Athena에 액세스할 클라이언트 생성

AthenaClientFactory.java 클래스는 Amazon Athena 클라이언트를 생성 및 구성하는 방법을 보여 줍니다.

package aws.example.athena; import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.athena.AthenaClient; import software.amazon.awssdk.services.athena.AthenaClientBuilder; public class AthenaClientFactory { private final AthenaClientBuilder builder = AthenaClient.builder() .region(Region.US_WEST_2) .credentialsProvider(ProfileCredentialsProvider.create()); public AthenaClient createClient() { return builder.build(); } }

쿼리 실행 시작

StartQueryExample은 Athena에 쿼리를 제출하고 결과를 사용할 수 있을 때까지 기다린 후 결과를 처리하는 방법을 보여 줍니다.

package aws.example.athena; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.athena.AthenaClient; import software.amazon.awssdk.services.athena.model.QueryExecutionContext; import software.amazon.awssdk.services.athena.model.ResultConfiguration; import software.amazon.awssdk.services.athena.model.StartQueryExecutionRequest; import software.amazon.awssdk.services.athena.model.StartQueryExecutionResponse; import software.amazon.awssdk.services.athena.model.AthenaException; import software.amazon.awssdk.services.athena.model.GetQueryExecutionRequest; import software.amazon.awssdk.services.athena.model.GetQueryExecutionResponse; import software.amazon.awssdk.services.athena.model.QueryExecutionState; import software.amazon.awssdk.services.athena.model.GetQueryResultsRequest; import software.amazon.awssdk.services.athena.model.GetQueryResultsResponse; import software.amazon.awssdk.services.athena.model.ColumnInfo; import software.amazon.awssdk.services.athena.model.Row; import software.amazon.awssdk.services.athena.model.Datum; import software.amazon.awssdk.services.athena.paginators.GetQueryResultsIterable; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class StartQueryExample { public static void main(String[] args) throws InterruptedException { AthenaClient athenaClient = AthenaClient.builder() .region(Region.US_WEST_2) .build(); String queryExecutionId = submitAthenaQuery(athenaClient); waitForQueryToComplete(athenaClient, queryExecutionId); processResultRows(athenaClient, queryExecutionId); athenaClient.close(); } // Submits a sample query to Amazon Athena and returns the execution ID of the // query. public static String submitAthenaQuery(AthenaClient athenaClient) { try { // The QueryExecutionContext allows us to set the database. QueryExecutionContext queryExecutionContext = QueryExecutionContext.builder() .database(ExampleConstants.ATHENA_DEFAULT_DATABASE) .build(); // The result configuration specifies where the results of the query should go. ResultConfiguration resultConfiguration = ResultConfiguration.builder() .outputLocation(ExampleConstants.ATHENA_OUTPUT_BUCKET) .build(); StartQueryExecutionRequest startQueryExecutionRequest = StartQueryExecutionRequest.builder() .queryString(ExampleConstants.ATHENA_SAMPLE_QUERY) .queryExecutionContext(queryExecutionContext) .resultConfiguration(resultConfiguration) .build(); StartQueryExecutionResponse startQueryExecutionResponse = athenaClient .startQueryExecution(startQueryExecutionRequest); return startQueryExecutionResponse.queryExecutionId(); } catch (AthenaException e) { e.printStackTrace(); System.exit(1); } return ""; } // Wait for an Amazon Athena query to complete, fail or to be cancelled. public static void waitForQueryToComplete(AthenaClient athenaClient, String queryExecutionId) throws InterruptedException { GetQueryExecutionRequest getQueryExecutionRequest = GetQueryExecutionRequest.builder() .queryExecutionId(queryExecutionId) .build(); GetQueryExecutionResponse getQueryExecutionResponse; boolean isQueryStillRunning = true; while (isQueryStillRunning) { getQueryExecutionResponse = athenaClient.getQueryExecution(getQueryExecutionRequest); String queryState = getQueryExecutionResponse.queryExecution().status().state().toString(); if (queryState.equals(QueryExecutionState.FAILED.toString())) { throw new RuntimeException( "The Amazon Athena query failed to run with error message: " + getQueryExecutionResponse .queryExecution().status().stateChangeReason()); } else if (queryState.equals(QueryExecutionState.CANCELLED.toString())) { throw new RuntimeException("The Amazon Athena query was cancelled."); } else if (queryState.equals(QueryExecutionState.SUCCEEDED.toString())) { isQueryStillRunning = false; } else { // Sleep an amount of time before retrying again. Thread.sleep(ExampleConstants.SLEEP_AMOUNT_IN_MS); } System.out.println("The current status is: " + queryState); } } // This code retrieves the results of a query public static void processResultRows(AthenaClient athenaClient, String queryExecutionId) { try { // Max Results can be set but if its not set, // it will choose the maximum page size. GetQueryResultsRequest getQueryResultsRequest = GetQueryResultsRequest.builder() .queryExecutionId(queryExecutionId) .build(); GetQueryResultsIterable getQueryResultsResults = athenaClient .getQueryResultsPaginator(getQueryResultsRequest); for (GetQueryResultsResponse result : getQueryResultsResults) { List<ColumnInfo> columnInfoList = result.resultSet().resultSetMetadata().columnInfo(); List<Row> results = result.resultSet().rows(); processRow(results, columnInfoList); } } catch (AthenaException e) { e.printStackTrace(); System.exit(1); } } private static void processRow(List<Row> row, List<ColumnInfo> columnInfoList) { for (Row myRow : row) { List<Datum> allData = myRow.data(); for (Datum data : allData) { System.out.println("The value of the column is " + data.varCharValue()); } } } }

쿼리 실행 중지

StopQueryExecutionExample은 쿼리 예제를 실행하고 쿼리를 즉시 중지하고 쿼리가 취소되었는지 쿼리 상태를 확인합니다.

package aws.example.athena; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.athena.AthenaClient; import software.amazon.awssdk.services.athena.model.StopQueryExecutionRequest; import software.amazon.awssdk.services.athena.model.GetQueryExecutionRequest; import software.amazon.awssdk.services.athena.model.GetQueryExecutionResponse; import software.amazon.awssdk.services.athena.model.QueryExecutionState; import software.amazon.awssdk.services.athena.model.AthenaException; import software.amazon.awssdk.services.athena.model.QueryExecutionContext; import software.amazon.awssdk.services.athena.model.ResultConfiguration; import software.amazon.awssdk.services.athena.model.StartQueryExecutionRequest; import software.amazon.awssdk.services.athena.model.StartQueryExecutionResponse; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class StopQueryExecutionExample { public static void main(String[] args) { AthenaClient athenaClient = AthenaClient.builder() .region(Region.US_WEST_2) .build(); String sampleQueryExecutionId = submitAthenaQuery(athenaClient); stopAthenaQuery(athenaClient, sampleQueryExecutionId); athenaClient.close(); } public static void stopAthenaQuery(AthenaClient athenaClient, String sampleQueryExecutionId) { try { StopQueryExecutionRequest stopQueryExecutionRequest = StopQueryExecutionRequest.builder() .queryExecutionId(sampleQueryExecutionId) .build(); athenaClient.stopQueryExecution(stopQueryExecutionRequest); GetQueryExecutionRequest getQueryExecutionRequest = GetQueryExecutionRequest.builder() .queryExecutionId(sampleQueryExecutionId) .build(); GetQueryExecutionResponse getQueryExecutionResponse = athenaClient .getQueryExecution(getQueryExecutionRequest); if (getQueryExecutionResponse.queryExecution() .status() .state() .equals(QueryExecutionState.CANCELLED)) { System.out.println("The Amazon Athena query has been cancelled!"); } } catch (AthenaException e) { e.printStackTrace(); System.exit(1); } } // Submits an example query and returns a query execution Id value public static String submitAthenaQuery(AthenaClient athenaClient) { try { QueryExecutionContext queryExecutionContext = QueryExecutionContext.builder() .database(ExampleConstants.ATHENA_DEFAULT_DATABASE) .build(); ResultConfiguration resultConfiguration = ResultConfiguration.builder() .outputLocation(ExampleConstants.ATHENA_OUTPUT_BUCKET) .build(); StartQueryExecutionRequest startQueryExecutionRequest = StartQueryExecutionRequest.builder() .queryExecutionContext(queryExecutionContext) .queryString(ExampleConstants.ATHENA_SAMPLE_QUERY) .resultConfiguration(resultConfiguration).build(); StartQueryExecutionResponse startQueryExecutionResponse = athenaClient .startQueryExecution(startQueryExecutionRequest); return startQueryExecutionResponse.queryExecutionId(); } catch (AthenaException e) { e.printStackTrace(); System.exit(1); } return null; } }

쿼리 실행 나열

ListQueryExecutionsExample은 쿼리 실행 ID의 목록을 가져오는 방법을 보여 줍니다.

package aws.example.athena; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.athena.AthenaClient; import software.amazon.awssdk.services.athena.model.AthenaException; import software.amazon.awssdk.services.athena.model.ListQueryExecutionsRequest; import software.amazon.awssdk.services.athena.model.ListQueryExecutionsResponse; import software.amazon.awssdk.services.athena.paginators.ListQueryExecutionsIterable; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListQueryExecutionsExample { public static void main(String[] args) { AthenaClient athenaClient = AthenaClient.builder() .region(Region.US_WEST_2) .build(); listQueryIds(athenaClient); athenaClient.close(); } public static void listQueryIds(AthenaClient athenaClient) { try { ListQueryExecutionsRequest listQueryExecutionsRequest = ListQueryExecutionsRequest.builder().build(); ListQueryExecutionsIterable listQueryExecutionResponses = athenaClient .listQueryExecutionsPaginator(listQueryExecutionsRequest); for (ListQueryExecutionsResponse listQueryExecutionResponse : listQueryExecutionResponses) { List<String> queryExecutionIds = listQueryExecutionResponse.queryExecutionIds(); System.out.println("\n" + queryExecutionIds); } } catch (AthenaException e) { e.printStackTrace(); System.exit(1); } } }

명명된 쿼리 생성

CreateNamedQueryExample은 명명된 쿼리를 생성하는 방법을 보여 줍니다.

package aws.example.athena; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.athena.AthenaClient; import software.amazon.awssdk.services.athena.model.AthenaException; import software.amazon.awssdk.services.athena.model.CreateNamedQueryRequest; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateNamedQueryExample { public static void main(String[] args) { final String USAGE = """ Usage: <name> Where: name - the name of the Amazon Athena query.\s """; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } String name = args[0]; AthenaClient athenaClient = AthenaClient.builder() .region(Region.US_WEST_2) .build(); createNamedQuery(athenaClient, name); athenaClient.close(); } public static void createNamedQuery(AthenaClient athenaClient, String name) { try { // Create the named query request. CreateNamedQueryRequest createNamedQueryRequest = CreateNamedQueryRequest.builder() .database(ExampleConstants.ATHENA_DEFAULT_DATABASE) .queryString(ExampleConstants.ATHENA_SAMPLE_QUERY) .description("Sample Description") .name(name) .build(); athenaClient.createNamedQuery(createNamedQueryRequest); System.out.println("Done"); } catch (AthenaException e) { e.printStackTrace(); System.exit(1); } } }

명명된 쿼리 삭제

DeleteNamedQueryExample은 명명된 쿼리 ID를 사용하여 명명된 쿼리를 삭제하는 방법을 보여 줍니다.

package aws.example.athena; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.athena.AthenaClient; import software.amazon.awssdk.services.athena.model.DeleteNamedQueryRequest; import software.amazon.awssdk.services.athena.model.AthenaException; import software.amazon.awssdk.services.athena.model.CreateNamedQueryRequest; import software.amazon.awssdk.services.athena.model.CreateNamedQueryResponse; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteNamedQueryExample { public static void main(String[] args) { final String USAGE = """ Usage: <name> Where: name - the name of the Amazon Athena query.\s """; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } String name = args[0]; AthenaClient athenaClient = AthenaClient.builder() .region(Region.US_WEST_2) .build(); String sampleNamedQueryId = getNamedQueryId(athenaClient, name); deleteQueryName(athenaClient, sampleNamedQueryId); athenaClient.close(); } public static void deleteQueryName(AthenaClient athenaClient, String sampleNamedQueryId) { try { DeleteNamedQueryRequest deleteNamedQueryRequest = DeleteNamedQueryRequest.builder() .namedQueryId(sampleNamedQueryId) .build(); athenaClient.deleteNamedQuery(deleteNamedQueryRequest); } catch (AthenaException e) { e.printStackTrace(); System.exit(1); } } public static String getNamedQueryId(AthenaClient athenaClient, String name) { try { CreateNamedQueryRequest createNamedQueryRequest = CreateNamedQueryRequest.builder() .database(ExampleConstants.ATHENA_DEFAULT_DATABASE) .queryString(ExampleConstants.ATHENA_SAMPLE_QUERY) .name(name) .description("Sample description") .build(); CreateNamedQueryResponse createNamedQueryResponse = athenaClient.createNamedQuery(createNamedQueryRequest); return createNamedQueryResponse.namedQueryId(); } catch (AthenaException e) { e.printStackTrace(); System.exit(1); } return null; } }

명명된 쿼리 나열

ListNamedQueryExample은 명명된 쿼리 ID의 목록을 가져오는 방법을 보여 줍니다.

package aws.example.athena; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.athena.AthenaClient; import software.amazon.awssdk.services.athena.model.AthenaException; import software.amazon.awssdk.services.athena.model.ListNamedQueriesRequest; import software.amazon.awssdk.services.athena.model.ListNamedQueriesResponse; import software.amazon.awssdk.services.athena.paginators.ListNamedQueriesIterable; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListNamedQueryExample { public static void main(String[] args) { AthenaClient athenaClient = AthenaClient.builder() .region(Region.US_WEST_2) .build(); listNamedQueries(athenaClient); athenaClient.close(); } public static void listNamedQueries(AthenaClient athenaClient) { try { ListNamedQueriesRequest listNamedQueriesRequest = ListNamedQueriesRequest.builder() .build(); ListNamedQueriesIterable listNamedQueriesResponses = athenaClient .listNamedQueriesPaginator(listNamedQueriesRequest); for (ListNamedQueriesResponse listNamedQueriesResponse : listNamedQueriesResponses) { List<String> namedQueryIds = listNamedQueriesResponse.namedQueryIds(); System.out.println(namedQueryIds); } } catch (AthenaException e) { e.printStackTrace(); System.exit(1); } } }