

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 擷取指標資料 (GetMetricData)
<a name="metrics-classic-getdata"></a>

`GetMetricData` 是以程式設計方式擷取 Classic 指標資料點的主要 API。它在單一呼叫中支援批次查詢、數學表達式和查詢。

## 基本使用
<a name="metrics-classic-getdata-basic"></a>

擷取單一指標：

```
aws cloudwatch get-metric-data \
  --metric-data-queries '[
    {
      "Id": "cpu",
      "MetricStat": {
        "Metric": {
          "Namespace": "AWS/EC2",
          "MetricName": "CPUUtilization",
          "Dimensions": [{"Name": "InstanceId", "Value": "{{i-1234567890abcdef0}}"}]
        },
        "Period": 300,
        "Stat": "Average"
      }
    }
  ]' \
  --start-time $(date -u -d '1 hour ago' +%Y-%m-%dT%H:%M:%S) \
  --end-time $(date -u +%Y-%m-%dT%H:%M:%S) \
  --output json
```

## 批次查詢
<a name="metrics-classic-getdata-batch"></a>

在單一呼叫中擷取多個指標 （最多 500 個查詢）：

```
aws cloudwatch get-metric-data \
  --metric-data-queries '[
    {"Id": "cpu", "MetricStat": {"Metric": {"Namespace": "AWS/EC2", "MetricName": "CPUUtilization", "Dimensions": [{"Name": "InstanceId", "Value": "{{i-abc}}"}]}, "Period": 300, "Stat": "Average"}},
    {"Id": "mem", "MetricStat": {"Metric": {"Namespace": "CWAgent", "MetricName": "mem_used_percent", "Dimensions": [{"Name": "InstanceId", "Value": "{{i-abc}}"}]}, "Period": 300, "Stat": "Average"}}
  ]' \
  --start-time 2024-01-01T00:00:00 \
  --end-time 2024-01-01T01:00:00
```

## 數學表達式
<a name="metrics-classic-getdata-math"></a>

使用算術組合指標：

```
[
  {"Id": "rx", "MetricStat": {"Metric": {"Namespace": "AWS/EC2", "MetricName": "NetworkIn", "Dimensions": [{"Name": "InstanceId", "Value": "{{i-abc}}"}]}, "Period": 300, "Stat": "Sum"}},
  {"Id": "tx", "MetricStat": {"Metric": {"Namespace": "AWS/EC2", "MetricName": "NetworkOut", "Dimensions": [{"Name": "InstanceId", "Value": "{{i-abc}}"}]}, "Period": 300, "Stat": "Sum"}},
  {"Id": "total", "Expression": "rx + tx", "Label": "TotalNetwork"}
]
```

## GetMetricData 與 GetMetricStatistics
<a name="metrics-classic-getdata-comparison"></a>


|  | **GetMetricData** | **GetMetricStatistics** | 
| --- | --- | --- | 
| 批次支援 | 每次呼叫最多 500 個查詢 | 每個呼叫的單一指標 | 
| 數學表達式 | 是 | 否 | 
|  queries | 是 | 否 | 
| 分頁 | 是 (NextToken) | 否 | 
| 建議 | 是 — 主要 API | 舊版 - 請改用 GetMetricData  | 

## 定價
<a name="metrics-classic-getdata-pricing"></a>

透過 請求的每 1，000 個指標 $0.01`GetMetricData`。

**注意**  
對於 OTel 指標，請使用 Prometheus 相容查詢 API 或 CloudWatch Query Studio 搭配 PromQL。 `GetMetricData` 不會查詢 OTel 指標。