134 lines
3.7 KiB
Java
134 lines
3.7 KiB
Java
|
|
package com.snp.batch.global.partition;
|
||
|
|
|
||
|
|
import lombok.Getter;
|
||
|
|
import lombok.Setter;
|
||
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||
|
|
import org.springframework.stereotype.Component;
|
||
|
|
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Optional;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 파티션 관리 설정 (application.yml 기반)
|
||
|
|
*
|
||
|
|
* 설정 예시:
|
||
|
|
* app.batch.partition:
|
||
|
|
* daily-tables:
|
||
|
|
* - schema: snp_data
|
||
|
|
* table-name: ais_target
|
||
|
|
* partition-column: message_timestamp
|
||
|
|
* periods-ahead: 3
|
||
|
|
* monthly-tables:
|
||
|
|
* - schema: snp_data
|
||
|
|
* table-name: some_table
|
||
|
|
* partition-column: created_at
|
||
|
|
* periods-ahead: 2
|
||
|
|
* retention:
|
||
|
|
* daily-default-days: 14
|
||
|
|
* monthly-default-months: 1
|
||
|
|
* custom:
|
||
|
|
* - table-name: ais_target
|
||
|
|
* retention-days: 30
|
||
|
|
*/
|
||
|
|
@Getter
|
||
|
|
@Setter
|
||
|
|
@Component
|
||
|
|
@ConfigurationProperties(prefix = "app.batch.partition")
|
||
|
|
public class PartitionConfig {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 일별 파티션 테이블 목록 (파티션 네이밍: {table}_YYMMDD)
|
||
|
|
*/
|
||
|
|
private List<PartitionTableConfig> dailyTables = new ArrayList<>();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 월별 파티션 테이블 목록 (파티션 네이밍: {table}_YYYY_MM)
|
||
|
|
*/
|
||
|
|
private List<PartitionTableConfig> monthlyTables = new ArrayList<>();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 보관기간 설정
|
||
|
|
*/
|
||
|
|
private RetentionConfig retention = new RetentionConfig();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 파티션 테이블 설정
|
||
|
|
*/
|
||
|
|
@Getter
|
||
|
|
@Setter
|
||
|
|
public static class PartitionTableConfig {
|
||
|
|
private String schema = "snp_data";
|
||
|
|
private String tableName;
|
||
|
|
private String partitionColumn;
|
||
|
|
private int periodsAhead = 3; // 미리 생성할 기간 수 (daily: 일, monthly: 월)
|
||
|
|
|
||
|
|
public String getFullTableName() {
|
||
|
|
return schema + "." + tableName;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 보관기간 설정
|
||
|
|
*/
|
||
|
|
@Getter
|
||
|
|
@Setter
|
||
|
|
public static class RetentionConfig {
|
||
|
|
/**
|
||
|
|
* 일별 파티션 기본 보관기간 (일)
|
||
|
|
*/
|
||
|
|
private int dailyDefaultDays = 14;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 월별 파티션 기본 보관기간 (개월)
|
||
|
|
*/
|
||
|
|
private int monthlyDefaultMonths = 1;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 개별 테이블 보관기간 설정
|
||
|
|
*/
|
||
|
|
private List<CustomRetention> custom = new ArrayList<>();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 개별 테이블 보관기간 설정
|
||
|
|
*/
|
||
|
|
@Getter
|
||
|
|
@Setter
|
||
|
|
public static class CustomRetention {
|
||
|
|
private String tableName;
|
||
|
|
private Integer retentionDays; // 일 단위 보관기간 (일별 파티션용)
|
||
|
|
private Integer retentionMonths; // 월 단위 보관기간 (월별 파티션용)
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 일별 파티션 테이블의 보관기간 조회 (일 단위)
|
||
|
|
*/
|
||
|
|
public int getDailyRetentionDays(String tableName) {
|
||
|
|
return getCustomRetention(tableName)
|
||
|
|
.map(c -> c.getRetentionDays() != null ? c.getRetentionDays() : retention.getDailyDefaultDays())
|
||
|
|
.orElse(retention.getDailyDefaultDays());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 월별 파티션 테이블의 보관기간 조회 (월 단위)
|
||
|
|
*/
|
||
|
|
public int getMonthlyRetentionMonths(String tableName) {
|
||
|
|
return getCustomRetention(tableName)
|
||
|
|
.map(c -> c.getRetentionMonths() != null ? c.getRetentionMonths() : retention.getMonthlyDefaultMonths())
|
||
|
|
.orElse(retention.getMonthlyDefaultMonths());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 개별 테이블 보관기간 설정 조회
|
||
|
|
*/
|
||
|
|
private Optional<CustomRetention> getCustomRetention(String tableName) {
|
||
|
|
if (retention.getCustom() == null) {
|
||
|
|
return Optional.empty();
|
||
|
|
}
|
||
|
|
return retention.getCustom().stream()
|
||
|
|
.filter(c -> tableName.equals(c.getTableName()))
|
||
|
|
.findFirst();
|
||
|
|
}
|
||
|
|
}
|