我正在编写一个在DynamoDB事件上触发的lambda。记录更改的记录,构建csv文件,然后将其上传到s3。
当我在日志中打印更改的记录时,可以看到记录。但是,当我生成一个csv文件并上载它时,有时它确实会生成该文件并上载它。有时,它仅上传没有标题的文件。
以下是代码段:
private void buildAndUploadCSV(List<Map<String, AttributeValue>> changedRecords, Context context, String tableName)
throws IOException {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String outputName = tableName + "_" + formatter.format(calendar.getTime()) + ".csv";
List<String> headers = changedRecords.stream().flatMap(map -> map.keySet().stream()).distinct()
.collect(Collectors.toList());
Path filePath = Paths.get("/tmp", outputName);
try (BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath.toFile()), StandardCharsets.UTF_8)))
{
StringBuilder headerContent = new StringBuilder();
for (String string : headers) {
headerContent.append(string);
headerContent.append(",");
}
StringBuilder strBuilder = new StringBuilder();
for (Map<String, AttributeValue> lmap : changedRecords) {
StringBuilder stringBuilder = new StringBuilder("");
String sep = "";
for (Entry<String, AttributeValue> string2 : lmap.entrySet()) {
String value = string2.getValue().getS();
stringBuilder.append(sep).append("\"").append(value).append("\"");
sep = ",";
}
if (!stringBuilder.toString().isEmpty()) {
strBuilder.append(stringBuilder).append(System.getProperty("line.separator"));
}
}
bw.append(headerContent);
bw.append("\n");
bw.append(strBuilder);
context.getLogger().log("[DEBUG] String Builder is: " + strBuilder);
PutObjectRequest request = new PutObjectRequest("bucket", "data/" + outputName,
filePath.toFile());
s3.putObject(request);
} catch (IOException e) {
context.getLogger().log("[ERROR] - IOException " + e);
}
}
关于为什么某些文件为空的任何建议/输入?