

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

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

# 開放原始碼 Telegraf
<a name="Telegraf"></a>

 您可以使用適用於 Telegraf 的 Timestream for LiveAnalytics 輸出外掛程式，直接從開放原始碼 Telegraf 將指標寫入 Timestream for LiveAnalytics。

 本節說明如何使用 Timestream for LiveAnalytics 輸出外掛程式安裝 Telegraf、如何使用 Timestream for LiveAnalytics 輸出外掛程式執行 Telegraf，以及開放原始碼 Telegraf 如何與 Timestream for LiveAnalytics 搭配使用。

**Topics**
+ [使用 Timestream for LiveAnalytics 輸出外掛程式安裝 Telegraf](Telegraf.installing-output-plugin.md)
+ [使用 Timestream for LiveAnalytics 輸出外掛程式執行 Telegraf](Telegraf.running-output-plugin.title.md)
+ [將 Telegraf/InfluxDB 指標映射至 Timestream for LiveAnalytics 模型](Telegraf.how-it-works.md)

# 使用 Timestream for LiveAnalytics 輸出外掛程式安裝 Telegraf
<a name="Telegraf.installing-output-plugin"></a>

從 1.16 版開始，Timestream for LiveAnalytics 輸出外掛程式可在官方 Telegraf 版本中使用。若要在大多數主要作業系統上安裝輸出外掛程式，請遵循 [InfluxData Telegraf 文件](https://docs.influxdata.com/telegraf/v1.16/introduction/installation/)中所述的步驟。若要在 Amazon Linux 2 作業系統上安裝 ，請遵循下列指示。

## 在 Amazon Linux 2 上使用 Timestream for LiveAnalytics 輸出外掛程式安裝 Telegraf
<a name="w2aab7c44c35b9b5"></a>

 若要在 Amazon Linux 2 上使用 Timestream 輸出外掛程式安裝 Telegraf，請執行下列步驟。

1. 使用`yum`套件管理員安裝 Telegraf。

   ```
   cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo
   [influxdb]
   name = InfluxDB Repository - RHEL \$releasever
   baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable
   enabled = 1
   gpgcheck = 1
   gpgkey = https://repos.influxdata.com/influxdb.key
   EOF
   ```

1. 執行下列命令。

   ```
   sudo sed -i "s/\$releasever/$(rpm -E %{rhel})/g" /etc/yum.repos.d/influxdb.repo
   ```

1. 安裝並啟動 Telegraf。

   ```
   sudo yum install telegraf
   sudo service telegraf start
   ```

# 使用 Timestream for LiveAnalytics 輸出外掛程式執行 Telegraf
<a name="Telegraf.running-output-plugin.title"></a>

您可以依照下列指示，使用 Timestream for LiveAnalytics 外掛程式執行 Telegraf。

1. 使用 Telegraf 產生範例組態。

   ```
   telegraf --section-filter agent:inputs:outputs --input-filter cpu:mem --output-filter timestream config > example.config
   ```

1. [使用管理主控台](console_timestream.md#console_timestream.db.using-console)、[CLI](https://docs.aws.amazon.com/cli/latest/reference/timestream-write/create-database.html) 或 [SDKs](getting-started-sdks.md) 在 Timestream 中建立資料庫。

1. 在 `example.config` 檔案中，透過編輯 `[[outputs.timestream]] `區段下的下列索引鍵來新增資料庫名稱。

   ```
   database_name = "yourDatabaseNameHere"
   ```

1. 根據預設，Telegraf 會建立資料表。如果您想要手動建立資料表，`create_table_if_not_exists`請將 設定為 `false`，並依照指示[使用管理主控台](console_timestream.md#console_timestream.table.using-console)、[CLI](https://docs.aws.amazon.com/cli/latest/reference/timestream-write/create-table.html) 或 [SDKs](getting-started-sdks.md) 建立資料表。

1. 在 *example.config* 檔案中，於 `[[outputs.timestream]] `區段下設定登入資料。登入資料應允許下列操作。

   ```
   timestream:DescribeEndpoints
   timestream:WriteRecords
   ```
**注意**  
如果您將 `create_table_if_not_exists` 設定為 `true`，請包含：  

   ```
   timestream:CreateTable
   ```
**注意**  
如果您將 `describe_database_on_start`設定為 `true`，請包含下列項目。  

   ```
   timestream:DescribeDatabase
   ```

1. 您可以根據您的偏好設定編輯其餘組態。

1. 完成組態檔案的編輯後，請使用下列命令執行 Telegraf。

   ```
   ./telegraf --config example.config
   ```

1. 指標應該會在幾秒鐘內顯示，視您的代理程式組態而定。您也應該會在 Timestream 主控台中看到新的資料表、*cpu* 和 *mem*。

# 將 Telegraf/InfluxDB 指標映射至 Timestream for LiveAnalytics 模型
<a name="Telegraf.how-it-works"></a>

 從 Telegraf 將資料寫入 Timestream for LiveAnalytics 時，資料會對應如下。
+ 時間戳記會寫入為時間欄位。
+ 標籤會寫入為維度。
+ 欄位會寫入為量值。
+ 測量大部分會寫入資料表名稱 （以下將詳細說明）。

適用於 Telegraf 的 Timestream for LiveAnalytics 輸出外掛程式提供多種選項，可在 Timestream for LiveAnalytics 中組織和儲存資料。這可以用範例描述，該範例以行通訊協定格式的資料開頭。

`weather,location=us-midwest,season=summer temperature=82,humidity=71 1465839830100400200 airquality,location=us-west no2=5,pm25=16 1465839830100400200`

以下說明資料。
+ 測量名稱為 `weather`和 `airquality`。
+ 標籤為 `location`和 `season`。
+ 欄位為 `temperature`、`no2`、 `humidity`和 `pm25`。

**Topics**
+ [將資料存放在多個資料表中](#Telegraf.how-it-works.multi-table-single-measure.title)
+ [將資料存放在單一資料表中](#Telegraf.how-it-works.single-table-single-measure.title)

## 將資料存放在多個資料表中
<a name="Telegraf.how-it-works.multi-table-single-measure.title"></a>

您可以選擇為每個測量建立單獨的資料表，並將每個欄位存放在每個資料表的個別資料列中。

組態為 `mapping_mode = "multi-table"`。
+ Timestream for LiveAnalytics 轉接器會建立兩個資料表，即 `weather`和 `airquality`。
+ 每個資料表列只會包含單一欄位。

產生的 LiveAnalytics 的 Timestream 資料表 `weather`和 `airquality`會如下所示。


**`weather`**  

| time | location | 季節 | measure\$1name | measure\$1value::bigint | 
| --- | --- | --- | --- | --- | 
|  2016-06-13 17：43：50  |  us-midwest  |  暑假  |  溫度  |  82  | 
|  2016-06-13 17：43：50  |  us-midwest  |  暑假  |  濕度  |  71  | 


**`airquality`**  

| time | location | measure\$1name | measure\$1value::bigint | 
| --- | --- | --- | --- | 
|  2016-06-13 17：43：50  |  us-midwest  |  no2   |  5  | 
|  2016-06-13 17：43：50  |  us-midwest  |  pm25   |  16  | 

## 將資料存放在單一資料表中
<a name="Telegraf.how-it-works.single-table-single-measure.title"></a>

您可以選擇將所有測量結果存放在單一資料表中，並將每個欄位存放在個別的資料表列中。

組態為 `mapping_mode = "single-table"`。使用 `single-table`、 `single_table_name`和 時有兩個額外的組態`single_table_dimension_name_for_telegraf_measurement_name`。
+ Timestream for LiveAnalytics 輸出外掛程式會建立名為 *<single\$1table\$1name>* 的單一資料表，其中包含 *<single\$1table\$1dimension\$1name\$1for\$1telegraf\$1measurement\$1name>* 欄。
+ 資料表可以在單一資料表列中包含多個欄位。

產生的 LiveAnalytics Timestream 資料表如下所示。


**`weather`**  

| time | location | 季節 | *<single\$1table\$1dimension\$1name\$1 for\$1telegraf\$1measurement\$1name>* | measure\$1name | measure\$1value::bigint | 
| --- | --- | --- | --- | --- | --- | 
|  2016-06-13 17：43：50  |  us-midwest  |  暑假  |  天氣  |  溫度  |  82  | 
|  2016-06-13 17：43：50  |  us-midwest  |  暑假  |  天氣  |  濕度  |  71  | 
|  2016-06-13 17：43：50  |  us-midwest  |  暑假  |  空氣品質  |  no2  |  5  | 
|  2016-06-13 17：43：50  |  us-midwest  |  暑假  |  天氣  |  pm25  |  16  | 