我想決議以下來自 AWS GetMetricData API 的 json 陣列。它包含不同陣列中的時間戳和值:
{
"Messages": [],
"MetricDataResults": [
{
"Timestamps": [
"2021-12-28T02:11:00Z",
"2021-12-28T02:10:00Z",
"2021-12-28T02:09:00Z",
"2021-12-28T02:08:00Z",
"2021-12-28T02:07:00Z",
"2021-12-28T02:06:00Z",
"2021-12-28T02:05:00Z",
"2021-12-28T02:04:00Z",
"2021-12-28T02:03:00Z",
"2021-12-28T02:02:00Z",
"2021-12-28T02:01:00Z",
"2021-12-28T02:00:00Z",
"2021-12-28T01:59:00Z",
"2021-12-28T01:58:00Z",
"2021-12-28T01:57:00Z",
"2021-12-28T01:56:00Z",
"2021-12-28T01:55:00Z",
"2021-12-28T01:54:00Z",
"2021-12-28T01:53:00Z",
"2021-12-28T01:52:00Z",
"2021-12-28T01:51:00Z"
],
"StatusCode": "Complete",
"Values": [
5.0,
2.0,
2.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
0.0
],
"Id": "groupDesiredCapacity",
"Label": "myRequestLabel"
},
{
"Timestamps": [
"2021-12-28T02:11:00Z",
"2021-12-28T02:10:00Z",
"2021-12-28T02:09:00Z",
"2021-12-28T02:08:00Z",
"2021-12-28T02:07:00Z",
"2021-12-28T02:06:00Z",
"2021-12-28T02:05:00Z",
"2021-12-28T02:04:00Z",
"2021-12-28T02:03:00Z",
"2021-12-28T02:02:00Z",
"2021-12-28T02:01:00Z",
"2021-12-28T02:00:00Z",
"2021-12-28T01:59:00Z",
"2021-12-28T01:58:00Z",
"2021-12-28T01:57:00Z",
"2021-12-28T01:56:00Z",
"2021-12-28T01:55:00Z",
"2021-12-28T01:54:00Z",
"2021-12-28T01:53:00Z",
"2021-12-28T01:52:00Z",
"2021-12-28T01:51:00Z"
],
"StatusCode": "Complete",
"Values": [
5.0,
2.0,
2.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
0.0
],
"Id": "groupInServiceCapacity",
"Label": "myRequestLabel"
},
{
"Timestamps": [
"2021-12-28T02:11:00Z",
"2021-12-28T02:10:00Z",
"2021-12-28T02:09:00Z",
"2021-12-28T02:08:00Z",
"2021-12-28T02:07:00Z",
"2021-12-28T02:06:00Z",
"2021-12-28T02:05:00Z",
"2021-12-28T02:04:00Z",
"2021-12-28T02:03:00Z",
"2021-12-28T02:02:00Z",
"2021-12-28T02:01:00Z",
"2021-12-28T02:00:00Z",
"2021-12-28T01:59:00Z",
"2021-12-28T01:58:00Z",
"2021-12-28T01:57:00Z",
"2021-12-28T01:56:00Z",
"2021-12-28T01:55:00Z",
"2021-12-28T01:54:00Z",
"2021-12-28T01:53:00Z",
"2021-12-28T01:52:00Z"
],
"StatusCode": "Complete",
"Values": [
94.20519316022799,
97.325,
99.95833333333333,
99.94166666666666,
97.35833333333333,
62.375,
96.61666666666666,
0.3916666666666666,
0.3833525009583812,
0.391647084312451,
24.05873431223854,
60.84898585023583,
64.54059099015015,
25.07541792363206,
0.25,
0.2499958334027766,
0.28333333333333327,
0.4000066667777796,
58.31569473842103,
5.98135840794e-05
],
"Id": "cpuUtilization",
"Label": "myRequestLabel"
}
]
}
我正在嘗試將其轉換為如下所示的表格:
Timestamp | Value | Id
2021-12-28T02:11:00Z | 5.0 | groupDesiredCapacity
我用谷歌搜索并提出了這個 jq 查詢,
.MetricDataResults[] | {t: .Timestamps[], v: .Values[],i: .Id}|[.t,.v,.i]|@csv
但它似乎“乘以”這些值而不是映射它們。
有點堅持這個,任何想法/幫助表示贊賞。
uj5u.com熱心網友回復:
使用transpose內置函式將陣列成員相互匹配
jq -r '.MetricDataResults[] | ([.Timestamps, .Values] | transpose[]) [.Id] | @csv'
"2021-12-28T02:11:00Z",5,"groupDesiredCapacity"
"2021-12-28T02:10:00Z",2,"groupDesiredCapacity"
"2021-12-28T02:09:00Z",2,"groupDesiredCapacity"
...
"2021-12-28T01:54:00Z",0.4000066667777796,"cpuUtilization"
"2021-12-28T01:53:00Z",58.31569473842103,"cpuUtilization"
"2021-12-28T01:52:00Z",5.98135840794e-05,"cpuUtilization"
演示
如果您想使用所需的輸出格式而不是 csv,請替換@csv為join(" | ")
jq -r '.MetricDataResults[] | ([.Timestamps, .Values] | transpose[]) [.Id] | join(" | ")'
2021-12-28T02:11:00Z | 5 | groupDesiredCapacity
2021-12-28T02:10:00Z | 2 | groupDesiredCapacity
2021-12-28T02:09:00Z | 2 | groupDesiredCapacity
...
2021-12-28T01:54:00Z | 0.4000066667777796 | cpuUtilization
2021-12-28T01:53:00Z | 58.31569473842103 | cpuUtilization
2021-12-28T01:52:00Z | 5.98135840794e-05 | cpuUtilization
演示
uj5u.com熱心網友回復:
一個簡單的替代方案:
.MetricDataResults[]
| .Id as $Id
| range(0; .Timestamps|length) as $i
| [.Timestamps[$i], .Values[$i], $Id]
| @csv
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/395916.html
