AWSKinesisFirehose,TransformationLambdaandOpenSearchlogprocessing
创始人
2024-09-26 01:30:13
0

使用AWS Kinesis Firehose将日志数据发送到S3存储桶中,然后使用AWS Lambda进行转换处理,并将处理好的数据存储到OpenSearch中。具体示例代码如下:

1、创建Firehose Delivery Stream

const AWS = require('aws-sdk'); const firehose = new AWS.Firehose({region: 'us-east-1'});

exports.handler = async (event) => { const params = { DeliveryStreamName: 'firehose_delivery_stream', S3DestinationConfiguration: { BucketARN: 'arn:aws:s3:::my-s3-bucket', RoleARN: 'arn:aws:iam::aws:policy/AmazonS3FullAccess', Prefix: 'logs/year=!{timestamp:yyyy}/month=!{timestamp:MM}/day=!{timestamp:dd}/hour=!{timestamp:HH}/', BufferingHints: { SizeInMBs: 1, IntervalInSeconds: 60 }, CompressionFormat: 'GZIP', CloudWatchLoggingOptions: { Enabled: true, LogGroupName: 'my-firehose-logs', LogStreamName: 'my-firehose-stream' } } };

try {
  const stream = await firehose.createDeliveryStream(params).promise();
  console.log(stream);
  return {
    statusCode: 200,
    body: JSON.stringify('Stream created successfully')
  };
} catch (err) {
  console.log(err);
  return {
    statusCode: 500,
    body: JSON.stringify('Failed to create stream')
  };
}

};

2、创建转换Lambda函数

exports.handler = async (event) => { let records = event.records.map((record) => { const payload = JSON.parse(Buffer.from(record.data, 'base64').toString('utf8'));

let newPayload = {
  // transform the payload here
};

return {
  recordId: record.recordId,
  result: 'Ok',
  data: Buffer.from(JSON.stringify(newPayload)).toString('base64')
};

});

return { records }; };

3、创建OpenSearch索引

const { OpenSearchService } = require("aws-sdk");

const opensearch = new OpenSearchService({ region: "us-east-1" });

const domainName = "my-opensearch-domain";

exports.handler = async function (event) { await opensearch.createIndex({ DomainName: domainName, Body: {}}).promise();

let response = { statusCode: 200, body: JSON.stringify("Index created successfully") }; return response; };

4、使用Lambda将数据发送到OpenSearch

const { OpenSearchService } = require("aws-sdk");

const opensearch = new OpenSearchService({ region: "us-east-1" });

const domainName = "my-opensearch-domain";

exports.handler = async (event) => { let records = event.records.map((record) => { const payload = JSON.parse(Buffer.from(record.data, 'base64').toString('utf8'));

// transform the payload here

return {
  index: {
    _index: 'my-index',
    _type: '_doc',
    _id: 'my-id', // optional
    _source: payload
  }
};

});

await opensearch.bulk({ DomainName: domainName, Body: records }).promise();

return { records }; };

相关内容

热门资讯

关于!微信微乐自建房辅助工具,... 关于!微信微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)方法1)微乐家乡麻将自建房辅...
大家学习交流!微信卡五星辅助器... 大家学习交流!微信卡五星辅助器,微乐小程序免费黑科技(开挂自建房)方法1、很好的工具软件,可以解锁游...
三分钟了解!微乐微信自建房辅助... 三分钟了解!微乐微信自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具1、三分钟了解...
一分钟教会你!微乐家乡麻将自建... 一分钟教会你!微乐家乡麻将自建房辅助app,微乐自建房脚本免费入口(开挂自建房)方法1、用户打开应用...
分享实测!微信微乐自建房辅助工... 分享实测!微信微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)器1、起透看视 微信微乐自建房...
重大通报!微乐自建房辅助工具脚... 重大通报!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)app微乐自建房辅助工具脚本破解...
如何分辨真伪!微乐微信自建房辅... 如何分辨真伪!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)技巧1、微乐微信自建房辅...
我来教教你!微乐自建房辅助工具... 我来教教你!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)工具1、玩家可以在微乐家...
重大来袭!微乐自建房辅助工具神... 重大来袭!微乐自建房辅助工具神器,微乐小程序免费黑科技(开挂自建房)器1、游戏颠覆性的策略玩法,独创...
带你了解!微信卡五星辅助器,微... 带你了解!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)方法1、带你了解!微信卡五星辅助器,...