如何运行AWS S3中的倍数文件的lambda函数
当检查S3存储桶中的新事件时,我具有以下Lambda功能,可以在AWS胶水中运行我的脚本。
import json
import boto3
from urllib.parse import unquote_plus
def lambda_handler(event, context):
bucketName = event["Records"][0]["s3"]["bucket"]["name"]
fileNameFull = event["Records"][0]["s3"]["object"]["key"]
fileName = unquote_plus(fileNameFull)
print(bucket, fileName)
glue = boto3.client('glue')
response = glue.start_job_run(
JobName = 'My_Job_Glue',
Arguments = {
'--s3_target_path_key': fileName,
'--s3_target_path_bucket': bucketName
}
)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
起初它运行良好,我部分地得到了我需要做的事情。实际发生的事情是,我将始终对同一存储桶发生多个文件,并且此当前逻辑在每个发生时都会运行我的胶水脚本(如果我有3个文件,我有3个胶合作业运行)。只有在确定所有新数据时,才能改善功能以运行脚本?今天,我有Kafka Connect配置了批次5000个记录,如果在几分钟结束时,该批次未到达,则该记录与那里一样多。
I have the following Lambda function to run my script in AWS Glue when a new occurrence in an S3 bucket is checked.
import json
import boto3
from urllib.parse import unquote_plus
def lambda_handler(event, context):
bucketName = event["Records"][0]["s3"]["bucket"]["name"]
fileNameFull = event["Records"][0]["s3"]["object"]["key"]
fileName = unquote_plus(fileNameFull)
print(bucket, fileName)
glue = boto3.client('glue')
response = glue.start_job_run(
JobName = 'My_Job_Glue',
Arguments = {
'--s3_target_path_key': fileName,
'--s3_target_path_bucket': bucketName
}
)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
At first it works great and I'm partially getting what I need it to do. What actually happens, is that I will always have more than one file occurrence for the same Bucket and this current logic is running my Glue script at each occurrence (if I have 3 files, I have 3 Glue job runs). How could I improve my function in order to run my script only when all new data is identified? Today I have Kafka Connect configured that batches 5000 records and if at the end of a few minutes this batch is not reached it forces as many records as it has there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
S3允许您使用简单队列服务(SQS)通知Lambda功能。使用SQS可以使您可以将消息批量发送到lambda。
S3 allows you to notify Lambda functions using Simple Queue Service (SQS). Using SQS allows you to batch messages to Lambda.