JMESPath如何汇总数据
How JMESPath summary data
az consumption usage list -m --query "sort_by([],&usageQuantity)[-5:].{name:instanceName,ser:consumedService,cat:meterDetails.meterCategory,qty:usageQuantity}"
得到json:
[
{
"cat": "Azure App Service",
"name": "weiwebapp02serviceplan",
"qty": "9",
"ser": "microsoft.web"
},
{
"cat": "Azure App Service",
"name": "weiwebapp01serviceplan",
"qty": "9",
"ser": "microsoft.web"
},
{
"cat": "Virtual Machines",
"name": "demoVM",
"qty": "9.7",
"ser": "Microsoft.Compute"
},
{
"cat": "Bandwidth",
"name": "weiwebapp01",
"qty": "10",
"ser": "microsoft.web"
},
{
"cat": "Container Registry",
"name": "42556f4129fb4c5db21e365ea2770211",
"qty": "98",
"ser": "Microsoft.ContainerRegistry"
}
]
希望得到结果=98 + 10 + 9.7 + 9 + 9
=135.7
我尝试过的:
我调用了 sum
函数,但它不起作用
az consumption usage list -m --query "sort_by([],&usageQuantity)[-5:].qty:usageQuantity | sum()"
并获取消息
UnexpectedError: The command failed with an unexpected error. Here is the traceback:
Expected 1 argument for function sum(), received 0
我明白了,因为 usageQuantity 类型是 string ,所以它必须转换为数字。
az consumption usage list -m --query "sort_by([],&usageQuantity)[-5:].usageQuantity | [].to_number(@) | sum([]) ""
az consumption usage list -m --query "sort_by([],&usageQuantity)[-5:].{name:instanceName,ser:consumedService,cat:meterDetails.meterCategory,qty:usageQuantity}"
得到json:
[
{
"cat": "Azure App Service",
"name": "weiwebapp02serviceplan",
"qty": "9",
"ser": "microsoft.web"
},
{
"cat": "Azure App Service",
"name": "weiwebapp01serviceplan",
"qty": "9",
"ser": "microsoft.web"
},
{
"cat": "Virtual Machines",
"name": "demoVM",
"qty": "9.7",
"ser": "Microsoft.Compute"
},
{
"cat": "Bandwidth",
"name": "weiwebapp01",
"qty": "10",
"ser": "microsoft.web"
},
{
"cat": "Container Registry",
"name": "42556f4129fb4c5db21e365ea2770211",
"qty": "98",
"ser": "Microsoft.ContainerRegistry"
}
]
希望得到结果=98 + 10 + 9.7 + 9 + 9
=135.7
我尝试过的:
我调用了 sum
函数,但它不起作用
az consumption usage list -m --query "sort_by([],&usageQuantity)[-5:].qty:usageQuantity | sum()"
并获取消息
UnexpectedError: The command failed with an unexpected error. Here is the traceback:
Expected 1 argument for function sum(), received 0
我明白了,因为 usageQuantity 类型是 string ,所以它必须转换为数字。
az consumption usage list -m --query "sort_by([],&usageQuantity)[-5:].usageQuantity | [].to_number(@) | sum([]) ""