1 changed files with 0 additions and 93 deletions
@ -1,93 +0,0 @@ |
|||
package com.dashboard.aws.lambda.handler; |
|||
|
|||
import com.amazonaws.services.lambda.runtime.Context; |
|||
import com.amazonaws.services.lambda.runtime.RequestHandler; |
|||
import com.dashboard.aws.lambda.Constants; |
|||
import com.dashboard.aws.lambda.service.MySQLService; |
|||
import com.dashboard.aws.lambda.service.S3Service; |
|||
import com.dashboard.aws.lambda.util.CsvUtil; |
|||
import com.dashboard.aws.lambda.util.DateUtil; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
|
|||
import java.time.LocalDate; |
|||
import java.time.ZoneId; |
|||
import java.util.*; |
|||
|
|||
public class MySQLToS3Handler implements RequestHandler<Map<String, Object>, String> { |
|||
|
|||
private static final Logger logger = LoggerFactory.getLogger(MySQLToS3Handler.class); |
|||
|
|||
private static final String DB_SCHEMA = System.getenv("DB_SCHEMA"); |
|||
|
|||
|
|||
private final MySQLService mysqlService = new MySQLService(); |
|||
private final S3Service s3Service = new S3Service(); |
|||
|
|||
@Override |
|||
public String handleRequest(Map<String, Object> event, Context context) { |
|||
try { |
|||
ZoneId zone = ZoneId.of("Asia/Tokyo"); |
|||
LocalDate now = DateUtil.resolveEventDate( event, zone); |
|||
LocalDate startDate = now.minusDays(5); // 5天前
|
|||
LocalDate endDate = now.minusDays(3); // 3天前
|
|||
|
|||
long startTs = startDate.atStartOfDay(ZoneId.of("Asia/Tokyo")).toInstant().toEpochMilli(); |
|||
long endTs = endDate.atStartOfDay(ZoneId.of("Asia/Tokyo")).toInstant().toEpochMilli(); |
|||
|
|||
String schema = DB_SCHEMA; |
|||
|
|||
for (String table : Constants.tables) { |
|||
// 查询旧数据
|
|||
List<Map<String, Object>> rows = mysqlService.queryOldData(schema, table, startTs, endTs); |
|||
if (rows.isEmpty()) { |
|||
logger.info("[{}][{}] no data....", schema, table); |
|||
continue; |
|||
} |
|||
|
|||
// 按年月日分组
|
|||
Map<String, List<Map<String, Object>>> grouped = new HashMap<>(); |
|||
for (Map<String, Object> row : rows) { |
|||
int y = (int) row.get("date_year"); |
|||
int m = (int) row.get("date_month"); |
|||
int d = (int) row.get("date_day"); |
|||
// 格式化年月日,不足两位补0
|
|||
String monthStr = String.format("%02d", m); |
|||
String dayStr = String.format("%02d", d); |
|||
String key = String.format("%d-%s-%s", y, monthStr, dayStr); |
|||
|
|||
grouped.computeIfAbsent(key, k -> new ArrayList<>()).add(row); |
|||
} |
|||
|
|||
// 上传每个分组
|
|||
boolean allSuccess = true; |
|||
for (Map.Entry<String, List<Map<String, Object>>> entry : grouped.entrySet()) { |
|||
String dateKey = entry.getKey(); |
|||
byte[] csvBytes = CsvUtil.toCsvBytes(entry.getValue()); |
|||
String s3Key = String.format("%s/%s.csv", table, dateKey); |
|||
try { |
|||
s3Service.upload(s3Key, csvBytes); |
|||
logger.info("[{}][{}] backup success -> {}", schema, table, s3Key); |
|||
} catch (Exception e) { |
|||
allSuccess = false; |
|||
logger.error("[{}][{}] backup failed -> {}", schema, table, s3Key, e); |
|||
} |
|||
} |
|||
|
|||
// 全部上传成功才删除数据
|
|||
if (allSuccess) { |
|||
mysqlService.deleteOldData(schema, table, startTs, endTs); |
|||
logger.info("[{}][{}] delete old data", schema, table); |
|||
} else { |
|||
logger.error("[{}][{}] not all files uploaded successfully, skip delete", schema, table); |
|||
} |
|||
} |
|||
|
|||
return "backup process finished"; |
|||
|
|||
} catch (Exception e) { |
|||
logger.error("backup error", e); |
|||
return "backup error:" + e.getMessage(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue