是否可以在调用lamda函数中使用组聚合? [英] Is it possible to use group aggregation in invoke lamda function?

查看:72
本文介绍了是否可以在调用lamda函数中使用组聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了三个lambda函数1).postData 2).like 3).comment.我正在使用invoke lambda函数来组合三个输出.找到以下代码供您参考.

I have created three lambda functions 1).postData 2).like 3).comment. I am using invoke lambda function to combine three output. Find the below code for your reference.

lambdas = boto3.client("lambda")

def lambda_handler(event, context):

print("Received event: " + json.dumps(event, indent=1))

response1 = lambdas.invoke(FunctionName="postdata", 
InvocationType="RequestResponse", Payload=json.dumps(event, 
default=json_util.default));

response2 = lambdas.invoke(FunctionName="like", 
InvocationType="RequestResponse", Payload=json.dumps(event, 
default=json_util.default));

response3 = lambdas.invoke(FunctionName="comment", 
InvocationType="RequestResponse", Payload=json.dumps(event, 
default=json_util.default));

result1 = json.loads(response1.get('Payload').read())
result2 = json.loads(response2.get('Payload').read())
result3 = json.loads(response3.get('Payload').read())

return result1+result2+result3

在这里,我在一个数组中获得了三个lambda函数.在三个lambda函数中,我们有一个唯一的ID,但有三个不同的文件名.

here i getting three lambda functions out in one array. In three lambda functions, we have one unique id but three different filed names.like the below

[

{

  "_id": {
    "$oid": "5d6fbc4256cfe9fcfd8c4136"
  },
  "location": null,
  "media_type": "jpg",
},
{
  "_id": {
    "$oid": "5d5dd01d93ab7d1ed7e2ff31"
  },
  "location": null,
  "media_type": "jpg",

},

{

 "from": {
    "$oid": "5d5dcccf9cf5ce000183d15e"
  },
  "to": {
    "$oid": "5d5dd01d93ab7d1ed7e2ff31"
  },
  "interaction_type": "feelings",
},
{
 "from": {
    "$oid": "5d5dcccf9cf5ce000183d15e"
  },
  "to": {
    "$oid": "5d6fbc4256cfe9fcfd8c4136"
  },
  "interaction_type": "feelings",

},

{

  "from": {
    "$oid": "5d5dcccf9cf5ce000183d15e"
  },
  "to": {
    "$oid": "5d5dd01d93ab7d1ed7e2ff31"
  },
  "interaction_data": "nice car.....",
},
{
  "from": {
    "$oid": "5d5dcccf9cf5ce000183d15e"
  },
  "to": {
    "$oid": "5d5dd01d93ab7d1ed7e2ff31"
  },
  "interaction_data": "awesome car.....",
},
{
  "from": {
    "$oid": "5d5dcccf9cf5ce000183d15e"
  },
  "to": {
    "$oid": "5d6fbc4256cfe9fcfd8c4136"
  },
  "interaction_data": "Hello world....",

}

]

我用粗体大括号getpost对输出进行了划分,例如注释输出. 我关心的是getpost-"_ id",例如-"to",注释-"to"字段值是唯一的.基于此唯一值,我可以对输出进行分组.意味着我需要_id来提交相同的值,这些值将显示在一个数组中.有可能吗?

I have divided the output with bold braces getpost, like, comment outputs. My concern is getpost -"_id", like - "to", comment - "to" fields values are unique. based on this unique value, can i group the output. means I need _id, to fileds values same,that values will display in one array. is it possible?

谢谢.

推荐答案

您在这里有两个选择.

You have two options here.

  1. 创建一个可以运行所有三个查询的lambda,然后在其中进行汇总(下面的mongo分组示例).

  1. Create one lambda that will run all three queries, and aggregate there (example of mongo grouping below).

如果尚未解析JSON响应对象,则对其进行解析,然后手动进行汇总.

Parse your JSON response objects, if they have not been parsed already, and manually aggregate.

显然,#1的性能更高,并且更优雅.

Obviously #1 will be more performant, and is way more elegant.

Mongo聚合/组函数示例:

Mongo aggregate/group function example:

db.sales.aggregate(
   [
      {
        $group : {
           _id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },
           totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
           averageQuantity: { $avg: "$quantity" },
           count: { $sum: 1 }
        }
      }
   ]
)

如果您决定在lambda中使用mongo分组路线,请记住它在计算时会在内存存储中使用.如果您的数据集很大,则会给您一个错误.如果遇到此问题,请按照文档建议将allowDiskUse设置为true.

If you decide to go the mongo grouping route in your lambda, please keep in mind that it uses in memory storage while it is calculating. If your data set is huge this will give you an error. If you run into that issue set allowDiskUse to true as the docs suggest.

请参阅:

这篇关于是否可以在调用lamda函数中使用组聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆