6 changed files with 200 additions and 12 deletions
@ -0,0 +1,78 @@ |
|||||
|
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.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 S3ToMySQLHandler implements RequestHandler<Map<String, Object>, String> { |
||||
|
|
||||
|
private static final Logger logger = LoggerFactory.getLogger(S3ToMySQLHandler.class); |
||||
|
|
||||
|
private final MySQLService mysqlService = new MySQLService(); |
||||
|
private final S3Service s3Service = new S3Service(); |
||||
|
|
||||
|
@Override |
||||
|
public String handleRequest(Map<String, Object> event, Context context) { |
||||
|
try { |
||||
|
// 同步目标日期(这里同步 3 天前的数据)
|
||||
|
// LocalDate targetDate = LocalDate.now(ZoneId.of("Asia/Tokyo")).minusDays(3);
|
||||
|
// String dateStr = targetDate.format(java.time.format.DateTimeFormatter.ISO_DATE);
|
||||
|
// String dateStr = "2025-10-08";
|
||||
|
LocalDate now = LocalDate.now(ZoneId.of("Asia/Tokyo")); |
||||
|
String dateStr = DateUtil.getLastYearSameIsoWeekDayStr(now); |
||||
|
|
||||
|
// 查询符合条件的企业ID
|
||||
|
List<Long> companyIds = mysqlService.getActiveCompanyIds(); |
||||
|
logger.info("company id list: {}", companyIds); |
||||
|
|
||||
|
List<String> tables = List.of("dashboard_record_accumulate", "dashboard_record_measure"); |
||||
|
|
||||
|
for (Long companyId : companyIds) { |
||||
|
String schema = "data_center_dongjian_" + companyId; |
||||
|
|
||||
|
for (String table : tables) { |
||||
|
// 构造 S3 文件路径
|
||||
|
String s3Key = String.format("%s/%s/%s.csv", table, companyId, dateStr); |
||||
|
logger.info("processing s3 file: {}", s3Key); |
||||
|
|
||||
|
// 下载 CSV
|
||||
|
byte[] content = s3Service.download(s3Key); |
||||
|
if (content == null || content.length == 0) { |
||||
|
logger.warn("[{}][{}] file empty or not found: {}", schema, table, s3Key); |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
// 解析 CSV
|
||||
|
List<Map<String, String>> rows = CsvUtil.parseCsv(content); |
||||
|
if (rows.isEmpty()) { |
||||
|
logger.warn("[{}][{}] csv empty: {}", schema, table, s3Key); |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
// 插入到 MySQL
|
||||
|
try { |
||||
|
int inserted = mysqlService.insertRows(schema, table, rows); |
||||
|
logger.info("[{}][{}] inserted {} rows", schema, table, inserted); |
||||
|
} catch (Exception e) { |
||||
|
logger.error("[{}][{}] insert failed -> {}", schema, table, s3Key, e); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return "sync process finished"; |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
logger.error("sync error", e); |
||||
|
return "sync error:" + e.getMessage(); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,40 @@ |
|||||
|
package com.dashboard.aws.lambda.util; |
||||
|
|
||||
|
import java.time.DayOfWeek; |
||||
|
import java.time.LocalDate; |
||||
|
import java.time.format.DateTimeFormatter; |
||||
|
import java.time.temporal.IsoFields; |
||||
|
|
||||
|
public class DateUtil { |
||||
|
|
||||
|
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_DATE; |
||||
|
|
||||
|
/** |
||||
|
* 根据指定日期,获取去年同 ISO 周同星期的日期 |
||||
|
* @param date 目标日期 |
||||
|
* @return 去年同周同日 LocalDate |
||||
|
*/ |
||||
|
public static LocalDate getLastYearSameIsoWeekDay(LocalDate date) { |
||||
|
if (date == null) throw new IllegalArgumentException("date不能为空"); |
||||
|
|
||||
|
int isoWeekYear = date.get(IsoFields.WEEK_BASED_YEAR); |
||||
|
int isoWeekNumber = date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR); |
||||
|
DayOfWeek dayOfWeek = date.getDayOfWeek(); |
||||
|
|
||||
|
return LocalDate.now() |
||||
|
.with(IsoFields.WEEK_BASED_YEAR, isoWeekYear - 1) |
||||
|
.with(IsoFields.WEEK_OF_WEEK_BASED_YEAR, isoWeekNumber) |
||||
|
.with(dayOfWeek); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 根据指定日期,获取去年同 ISO 周同星期的日期字符串 |
||||
|
* @param date 目标日期 |
||||
|
* @return 去年同周同日 yyyy-MM-dd |
||||
|
*/ |
||||
|
public static String getLastYearSameIsoWeekDayStr(LocalDate date) { |
||||
|
LocalDate lastYearDate = getLastYearSameIsoWeekDay(date); |
||||
|
return lastYearDate.format(FORMATTER); |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue