从Kinesis流中读取AWS短信(SMS)的发送日志



我正在使用AWS SNS短信功能并向手机发送短信。我想读取交货状态成功|失败。为此,我遵循以下步骤:

  • 创建Cloudwatch日志组
  • 创建Kinesis订阅过滤器
  • 现在日志将在Kinesis蒸汽上可用

我可以从Kinesis steam中读取日志,但不是必需的格式,我希望它是json格式。在此处输入图像描述

如果我直接将数据发送到Kinesis流并读回,它可以很好地工作,并且具有正确的可读格式(json(。

交付日志:

{
"notification": {
"messageId": "0aaabb6c-35ab-5a0e-b446-e1048f5623b9",
"timestamp": "2022-01-24 14:33:33.441"
},
"delivery": {
"destination": "<phone-number>",
"smsType": "Transactional",
"providerResponse": "Sandboxed account unable to send to number.",
"dwellTimeMs": 44
},
"status": "FAILURE"
}

代码:DeliveryStatusProcessor#processRecords(ProcessRecordsInput processRecordsInpout(

public void processRecords(ProcessRecordsInput processRecordsInput) {
try {
log.info("Processing {} record(s)", processRecordsInput.records().size());
processRecordsInput.records().forEach(incomingDeliveryStatus ->
{
try {
processRecord(incomingDeliveryStatus);
} catch (IOException e) {
log.info("Failed to process records.");
}
});
} catch (Throwable t) {
log.error("Caught throwable while processing records. Aborting.");
Runtime.getRuntime().halt(1);
} finally {
//
}
}
private void processRecord(KinesisClientRecord record) throws IOException {
byte[] messageStatus = new byte[record.data().remaining()];
record.data().get(messageStatus);
String string = new String(messageStatus);
System.out.println("================>>>"+string);      
}

cloudwatch以gzip压缩格式发送日志。你需要在消费者应用程序中解压缩它们,以获得正确的格式

最新更新