

如需與 Amazon Timestream for LiveAnalytics 類似的功能，請考慮使用 Amazon Timestream for InfluxDB。它提供簡化的資料擷取和單一位數毫秒查詢回應時間，以進行即時分析。[在這裡](https://docs.aws.amazon.com//timestream/latest/developerguide/timestream-for-influxdb.html)進一步了解。

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

# 插補函數
<a name="timeseries-specific-constructs.functions.interpolation"></a>

如果您的時間序列資料在特定時間點遺失事件的值，您可以使用插補來估計這些遺失事件的值。Amazon Timestream 支援四種插補變體：線性插補、立方曲線插補、最後觀察值向前 (locf) 插補，以及常數插補。本節提供 Timestream for LiveAnalytics 插補函數的使用資訊，以及範例查詢。



## 用量資訊
<a name="w2aab7c59c13c13c11b7"></a>


| 函式 | 輸出資料類型 | Description | 
| --- | --- | --- | 
|  `interpolate_linear(timeseries, array[timestamp])`  |  時間序列  |  使用[線性插補](https://wikipedia.org/wiki/Linear_interpolation)填入遺失的資料。  | 
|  `interpolate_linear(timeseries, timestamp)`  |  double  |  使用[線性插補](https://wikipedia.org/wiki/Linear_interpolation)填入遺失的資料。  | 
|  `interpolate_spline_cubic(timeseries, array[timestamp])`  |  時間序列  |  使用[立方曲線插補](https://wikiversity.org/wiki/Cubic_Spline_Interpolation#:~:text=Cubic%20spline%20interpolation%20is%20a,Lagrange%20polynomial%20and%20Newton%20polynomial.)填入缺少的資料。  | 
|  `interpolate_spline_cubic(timeseries, timestamp)`  |  double  |  使用[立方曲線插補](https://wikiversity.org/wiki/Cubic_Spline_Interpolation#:~:text=Cubic%20spline%20interpolation%20is%20a,Lagrange%20polynomial%20and%20Newton%20polynomial.)填入缺少的資料。  | 
|  `interpolate_locf(timeseries, array[timestamp])`  |  時間序列  |  使用最後一個取樣值填入遺失的資料。  | 
|  `interpolate_locf(timeseries, timestamp)`  |  double  |  使用最後一個取樣值填入遺失的資料。  | 
|  `interpolate_fill(timeseries, array[timestamp], double)`  |  時間序列  |  使用常數值填入遺失的資料。  | 
|  `interpolate_fill(timeseries, timestamp, double)`  |  double  |  使用常數值填入遺失的資料。  | 

## 查詢範例
<a name="w2aab7c59c13c13c11b9"></a>

**Example**  
尋找過去 2 小時內特定 EC2 主機以 30 秒間隔固定的平均 CPU 使用率，並使用線性插補填入缺少的值：  

```
WITH binned_timeseries AS (
SELECT hostname, BIN(time, 30s) AS binned_timestamp, ROUND(AVG(measure_value::double), 2) AS avg_cpu_utilization
FROM "sampleDB".DevOps
WHERE measure_name = 'cpu_utilization'
    AND hostname = 'host-Hovjv'
    AND time > ago(2h)
GROUP BY hostname, BIN(time, 30s)
), interpolated_timeseries AS (
SELECT hostname,
    INTERPOLATE_LINEAR(
        CREATE_TIME_SERIES(binned_timestamp, avg_cpu_utilization),
            SEQUENCE(min(binned_timestamp), max(binned_timestamp), 15s)) AS interpolated_avg_cpu_utilization
FROM binned_timeseries
GROUP BY hostname
)
SELECT time, ROUND(value, 2) AS interpolated_cpu
FROM interpolated_timeseries
CROSS JOIN UNNEST(interpolated_avg_cpu_utilization)
```

**Example**  
尋找過去 2 小時內特定 EC2 主機以 30 秒間隔固定的平均 CPU 使用率，根據最後的觀察值使用插補填入缺少的值：  

```
WITH binned_timeseries AS (
SELECT hostname, BIN(time, 30s) AS binned_timestamp, ROUND(AVG(measure_value::double), 2) AS avg_cpu_utilization
FROM "sampleDB".DevOps
WHERE measure_name = 'cpu_utilization'
    AND hostname = 'host-Hovjv'
    AND time > ago(2h)
GROUP BY hostname, BIN(time, 30s)
), interpolated_timeseries AS (
SELECT hostname,
    INTERPOLATE_LOCF(
        CREATE_TIME_SERIES(binned_timestamp, avg_cpu_utilization),
            SEQUENCE(min(binned_timestamp), max(binned_timestamp), 15s)) AS interpolated_avg_cpu_utilization
FROM binned_timeseries
GROUP BY hostname
)
SELECT time, ROUND(value, 2) AS interpolated_cpu
FROM interpolated_timeseries
CROSS JOIN UNNEST(interpolated_avg_cpu_utilization)
```