|
|
|
@ -18,6 +18,9 @@ public class MySQLToS3Handler implements RequestHandler<Map<String, Object>, Str |
|
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
|
@ -32,57 +35,51 @@ public class MySQLToS3Handler implements RequestHandler<Map<String, Object>, Str |
|
|
|
long startTs = startDate.atStartOfDay(ZoneId.of("Asia/Tokyo")).toInstant().toEpochMilli(); |
|
|
|
long endTs = endDate.atStartOfDay(ZoneId.of("Asia/Tokyo")).toInstant().toEpochMilli(); |
|
|
|
|
|
|
|
// 查询符合条件的企业ID
|
|
|
|
List<Long> companyIds = mysqlService.getActiveCompanyIds(); |
|
|
|
logger.info("company id list: {}", companyIds); |
|
|
|
|
|
|
|
for (Long companyId : companyIds) { |
|
|
|
String schema = "data_center_dongjian_" + companyId; |
|
|
|
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; |
|
|
|
} |
|
|
|
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); |
|
|
|
} |
|
|
|
// 按年月日分组
|
|
|
|
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/%s.csv", table, companyId, 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); |
|
|
|
} |
|
|
|
// 上传每个分组
|
|
|
|
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); |
|
|
|
} |
|
|
|
// 全部上传成功才删除数据
|
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|