SpringBoot -> Athena -> S3

Chans·2023년 11월 1일
0

개요

SpringBoot에서 S3에 쌓인 로그 데이터를 athena에 연결해 Query질의를 하고 결과를 S3에 저장이 가능한가?를 검증 해보았다.

1. dependency

dependencies 

   implementation 'com.amazonaws:aws-java-sdk-athena:1.12.576'  
}

2. Athena client 정의

accessKey와 secretKey는 aws계정 key값을 넣어주었다.

@Configuration
public class AthenaConfig {

    @Value("${aws.access.key}")
    private String accessKey;

    @Value("${aws.secret.key}")
    private String secretKey;

    @Bean
    public AmazonAthena createClient() {
       BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
       return AmazonAthenaClientBuilder.standard()
             .withRegion(Regions.AP_NORTHEAST_2)
             .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
             .withClientConfiguration(new ClientConfiguration().withClientExecutionTimeout(AthenaExampleConstants.CLIENT_EXECUTION_TIMEOUT))
             .build();
    }
}

3. Athena 테이블 데이터 확인

Glue Crawer를 통해 S3에 쌓인 로그들을 clickstream_db에 테이블을 확인한다.

Glue Crawer는 스케쥴링으로 특정 시간 주기로 S3 → athena table화가 가능하다.

4. 상수값 정의

쿼리 결과를 저장할 S3 경로, DB 명, 수행 쿼리 등을 정의한다.

public class AthenaExampleConstants {

    public static final int CLIENT_EXECUTION_TIMEOUT = 100000;
    public static final String ATHENA_OUTPUT_BUCKET = "s3://url";
    // This is querying a table created by the getting started tutorial in Athena
    public static final String ATHENA_SAMPLE_QUERY = "SELECT * from table limit 10;";
    public static final String ATHENA_DEFAULT_DATABASE = "dbName";
}

5. Query 결과 확인

단순 테스트 용도를 위해 리팩토링 없이 조회하였다.

private String submitAthenaQuery() {
    // The QueryExecutionContext allows us to set the Database.
    QueryExecutionContext queryExecutionContext = new QueryExecutionContext().withDatabase(AthenaExampleConstants.ATHENA_DEFAULT_DATABASE);
 
    // The result configuration specifies where the results of the query should go in S3 and encryption options
    ResultConfiguration resultConfiguration = new ResultConfiguration()
            // You can provide encryption options for the output that is written.
            // .withEncryptionConfiguration(encryptionConfiguration)
            .withOutputLocation(AthenaExampleConstants.ATHENA_OUTPUT_BUCKET);
 
    // Create the StartQueryExecutionRequest to send to Athena which will start the query.
    StartQueryExecutionRequest startQueryExecutionRequest = new StartQueryExecutionRequest()
            .withQueryString(AthenaExampleConstants.ATHENA_SAMPLE_QUERY)
            .withQueryExecutionContext(queryExecutionContext)
            .withResultConfiguration(resultConfiguration);
 
    StartQueryExecutionResult startQueryExecutionResult = athenaClient.startQueryExecution(startQueryExecutionRequest);
 
    GetQueryResultsRequest request = new GetQueryResultsRequest();
    request.setQueryExecutionId(startQueryExecutionResult.getQueryExecutionId());
    GetQueryResultsResult result = athenaClient.getQueryResults(request);
 
    return startQueryExecutionResult.getQueryExecutionId();
}

debug 모드 데이터 결과 확인

6. S3 질의 결과 확인

결론

SpringBoot에서 S3에 쌓인 로그데이터를 Athena의 테이블로 조회해서 결과 확인이 가능함을 확인하였다.

Abusing정책에 따라 쿼리 질의를 정의함에 따라 SpringBoot에서 상품의 포인트 차감이라던지 상품 viewing에 대한 제한로직 추가가 가능해 보인다.

참고
https://docs.aws.amazon.com/ko_kr/athena/latest/ug/code-samples.html
https://github.com/awsdocs/aws-doc-sdk-examples

0개의 댓글