

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

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

# 存取 LiveAnalytics 的 Timestream
<a name="accessing"></a>

您可以使用主控台、CLI 或 API 存取 Timestream for LiveAnalytics。如需存取 Timestream for LiveAnalytics 的相關資訊，請檢閱以下內容：

**Topics**
+ [註冊 AWS 帳戶](#sign-up-for-aws)
+ [建立具有管理存取權的使用者](#create-an-admin)
+ [為 LiveAnalytics 存取提供 Timestream](#getting-started.prereqs.iam-user)
+ [授與程式設計存取權](#programmatic-access)
+ [使用主控台](console_timestream.md)
+ [使用 存取 Amazon Timestream for LiveAnalytics AWS CLI](Tools.CLI.md)
+ [使用 API](Using.API.md)
+ [使用 AWS SDKs](getting-started-sdks.md)

## 註冊 AWS 帳戶
<a name="sign-up-for-aws"></a>

如果您沒有 AWS 帳戶，請完成下列步驟來建立一個。

**註冊 AWS 帳戶**

1. 開啟 [https://portal.aws.amazon.com/billing/signup](https://portal.aws.amazon.com/billing/signup)。

1. 請遵循線上指示進行。

   部分註冊程序需接收來電或簡訊，並在電話鍵盤輸入驗證碼。

   當您註冊 時 AWS 帳戶，*AWS 帳戶根使用者*會建立 。根使用者有權存取該帳戶中的所有 AWS 服務 和資源。作為安全最佳實務，請將管理存取權指派給使用者，並且僅使用根使用者來執行[需要根使用者存取權的任務](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_root-user.html#root-user-tasks)。

AWS 會在註冊程序完成後傳送確認電子郵件給您。您可以隨時登錄 [https://aws.amazon.com/](https://aws.amazon.com/) 並選擇**我的帳戶**，以檢視您目前的帳戶活動並管理帳戶。

## 建立具有管理存取權的使用者
<a name="create-an-admin"></a>

註冊 後 AWS 帳戶，請保護 AWS 帳戶根使用者、啟用 AWS IAM Identity Center和建立管理使用者，以免將根使用者用於日常任務。

**保護您的 AWS 帳戶根使用者**

1.  選擇**根使用者**並輸入 AWS 帳戶 您的電子郵件地址，以帳戶擁有者[AWS 管理主控台](https://console.aws.amazon.com/)身分登入 。在下一頁中，輸入您的密碼。

   如需使用根使用者登入的說明，請參閱 *AWS 登入 使用者指南*中的[以根使用者身分登入](https://docs.aws.amazon.com/signin/latest/userguide/console-sign-in-tutorials.html#introduction-to-root-user-sign-in-tutorial)。

1. 若要在您的根使用者帳戶上啟用多重要素驗證 (MFA)。

   如需說明，請參閱《*IAM 使用者指南*》中的[為您的 AWS 帳戶 根使用者 （主控台） 啟用虛擬 MFA 裝置](https://docs.aws.amazon.com/IAM/latest/UserGuide/enable-virt-mfa-for-root.html)。

**建立具有管理存取權的使用者**

1. 啟用 IAM Identity Center。

   如需指示，請參閱《AWS IAM Identity Center 使用者指南》**中的[啟用 AWS IAM Identity Center](https://docs.aws.amazon.com//singlesignon/latest/userguide/get-set-up-for-idc.html)。

1. 在 IAM Identity Center 中，將管理存取權授予使用者。

   如需使用 IAM Identity Center 目錄 做為身分來源的教學課程，請參閱*AWS IAM Identity Center 《 使用者指南*》中的[使用預設值設定使用者存取 IAM Identity Center 目錄](https://docs.aws.amazon.com//singlesignon/latest/userguide/quick-start-default-idc.html)。

**以具有管理存取權的使用者身分登入**
+ 若要使用您的 IAM Identity Center 使用者簽署，請使用建立 IAM Identity Center 使用者時傳送至您電子郵件地址的簽署 URL。

  如需使用 IAM Identity Center 使用者登入的說明，請參閱*AWS 登入 《 使用者指南*》中的[登入 AWS 存取入口網站](https://docs.aws.amazon.com/signin/latest/userguide/iam-id-center-sign-in-tutorial.html)。

**指派存取權給其他使用者**

1. 在 IAM Identity Center 中，建立一個許可集來遵循套用最低權限的最佳實務。

   如需指示，請參閱《AWS IAM Identity Center 使用者指南》**中的[建立許可集](https://docs.aws.amazon.com//singlesignon/latest/userguide/get-started-create-a-permission-set.html)。

1. 將使用者指派至群組，然後對該群組指派單一登入存取權。

   如需指示，請參閱《AWS IAM Identity Center 使用者指南》**中的[新增群組](https://docs.aws.amazon.com//singlesignon/latest/userguide/addgroups.html)。

## 為 LiveAnalytics 存取提供 Timestream
<a name="getting-started.prereqs.iam-user"></a>

 存取 Timestream for LiveAnalytics 所需的許可已授予管理員。對於其他使用者，您應該使用下列政策授予他們 Timestream for LiveAnalytics 存取權：

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "timestream:*",
        "kms:DescribeKey",
        "kms:CreateGrant",
        "kms:Decrypt",
        "dbqms:CreateFavoriteQuery",
        "dbqms:DescribeFavoriteQueries",
        "dbqms:UpdateFavoriteQuery",
        "dbqms:DeleteFavoriteQueries",
        "dbqms:GetQueryString",
        "dbqms:CreateQueryHistory",
        "dbqms:UpdateQueryHistory",
        "dbqms:DeleteQueryHistory",
        "dbqms:DescribeQueryHistory",
        "s3:ListAllMyBuckets"
      ],
      "Resource": "*"
    }
  ]
}
```

------

**注意**  
如需 的相關資訊`dbqms`，請參閱[資料庫查詢中繼資料服務的動作、資源和條件索引鍵](https://docs.aws.amazon.com/service-authorization/latest/reference/list_databasequerymetadataservice.html)。如需詳細資訊，`kms`請參閱 Key Management Service 的動作、資源和條件金鑰。 [AWS](https://docs.aws.amazon.com/service-authorization/latest/reference/list_awskeymanagementservice.html)

## 授與程式設計存取權
<a name="programmatic-access"></a>

如果使用者想要與 AWS 外部互動，則需要程式設計存取 AWS 管理主控台。授予程式設計存取權的方式取決於正在存取的使用者類型 AWS。

若要授予使用者程式設計存取權，請選擇下列其中一個選項。


****  

| 哪個使用者需要程式設計存取權？ | 到 | 根據 | 
| --- | --- | --- | 
| IAM | （建議） 使用主控台登入資料做為臨時登入資料，以簽署對 AWS CLI、 AWS SDKs程式設計請求。 AWS APIs |  請依照您要使用的介面所提供的指示操作。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/timestream/latest/developerguide/accessing.html)  | 
|  人力資源身分 (IAM Identity Center 中管理的使用者)  | 使用暫時登入資料簽署對 AWS CLI、 AWS SDKs程式設計請求。 AWS APIs |  請依照您要使用的介面所提供的指示操作。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/timestream/latest/developerguide/accessing.html)  | 
| IAM | 使用暫時登入資料簽署對 AWS CLI、 AWS SDKs程式設計請求。 AWS APIs | 遵循《IAM 使用者指南》中[將臨時登入資料與 AWS 資源](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html)搭配使用的指示。 | 
| IAM | (不建議使用)使用長期登入資料來簽署對 AWS CLI、 AWS SDKs程式設計請求。 AWS APIs |  請依照您要使用的介面所提供的指示操作。 [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/timestream/latest/developerguide/accessing.html)  | 

# 使用主控台
<a name="console_timestream"></a>

 您可以使用 Timestream Live Analytics 的 AWS 管理主控台來建立、編輯、刪除、描述和列出資料庫和資料表。您也可以使用 主控台來執行查詢。

**Topics**
+ [教學課程](#console_timestream.db-w-sample-data)
+ [建立 資料庫](#console_timestream.db.using-console)
+ [建立資料表](#console_timestream.table.using-console)
+ [執行查詢](#console_timestream.queries.using-console)
+ [建立排程查詢](#console_timestream.scheduledquery.using-console)
+ [刪除排程查詢](#console_timestream.scheduledquerydeletedisable.using-console)
+ [刪除資料表](#console_timestream.delete-table.using-console)
+ [刪除資料庫](#console_timestream.delete-db.using-console)
+ [編輯資料表](#console_timestream.edit-table.using-console)
+ [編輯資料庫](#console_timestream.edit-db.using-console)

## 教學課程
<a name="console_timestream.db-w-sample-data"></a>

 本教學課程說明如何建立填入範例資料集的資料庫，並執行範例查詢。本教學中使用的範例資料集經常出現在 IoT 和 DevOps 案例中。IoT 資料集包含時間序列資料，例如卡車的速度、位置和負載，以簡化機群管理並識別最佳化機會。DevOps 資料集包含 EC2 執行個體指標，例如 CPU、網路和記憶體使用率，以改善應用程式效能和可用性。以下是本節中說明的[影片教學](https://www.youtube.com/watch?v=YBWCGDd4ChQ)課程 

請依照下列步驟建立填入範例資料集的資料庫，並使用 AWS 主控台執行範例查詢。

1. 開啟 [AWS 主控台](https://console.aws.amazon.com/timestream)。

1. 在導覽窗格中，選擇**資料庫**

1. 按一下**建立資料庫**。

1. 在建立資料庫頁面上，輸入下列內容：
   + **選擇組態** - 選取**範例資料庫**。
   + **名稱** - 輸入您選擇的資料庫名稱。
   + **選擇範例資料集** - 選取 **IoT** 和 **DevOps**。
   +  按一下**建立資料庫**以建立包含兩個資料表的資料庫：填入範例資料的 IoT 和 DevOps。

1. 在導覽窗格中，選擇**查詢編輯器**

1. 從頂端功能表中選取**範例查詢**。

1. 按一下其中一個範例查詢。這將帶您回到查詢編輯器，並將編輯器填入範例查詢。

1. 按一下**執行**以執行查詢，並查看查詢結果。

## 建立 資料庫
<a name="console_timestream.db.using-console"></a>

請依照下列步驟，使用 AWS 主控台建立資料庫。

1. 開啟 [AWS 主控台](https://console.aws.amazon.com/timestream)。

1. 在導覽窗格中，選擇**資料庫**

1. 按一下**建立資料庫**。

1. 在建立資料庫頁面上，輸入以下內容。
   + **選擇組態** - 選取**標準資料庫**。
   + **名稱** - 輸入您選擇的資料庫名稱。
   + **加密**：選擇 KMS 金鑰或使用預設選項，如果 KMS 金鑰不存在，Timestream Live Analytics 會在您的帳戶中建立 KMS 金鑰。

1.  按一下**建立資料庫**以建立資料庫。

## 建立資料表
<a name="console_timestream.table.using-console"></a>

請依照下列步驟，使用 AWS 主控台建立資料表。

1. 開啟 [AWS 主控台](https://console.aws.amazon.com/timestream)。

1. 在導覽窗格中，選擇**資料表**

1. 按一下**建立資料表**。

1. 在建立資料表頁面上，輸入以下內容。
   + **資料庫名稱** - 選取在 中建立的資料庫名稱[建立 資料庫](#console_timestream.db.using-console)。
   + **資料表名稱** - 輸入您選擇的資料表名稱。
   + **記憶體存放區保留** - 指定您希望在記憶體存放區中保留資料的時間長度。記憶體存放區會處理傳入資料，包括延遲抵達資料 （時間戳記早於目前時間的資料），並針對快速point-in-time查詢進行最佳化。
   + **磁性存放區保留** - 指定您希望在磁性存放區中保留資料的時間長度。磁性存放區適用於長期儲存，並針對快速分析查詢進行最佳化。

1.  按一下**建立資料表**。

## 執行查詢
<a name="console_timestream.queries.using-console"></a>

請依照下列步驟，使用 AWS 主控台執行查詢。

1. 開啟 [AWS 主控台](https://console.aws.amazon.com/timestream)。

1. 在導覽窗格中，選擇**查詢編輯器**

1. 在左側窗格中，選取在 中建立的資料庫[建立 資料庫](#console_timestream.db.using-console)。

1. 在左側窗格中，選取在 中建立的資料庫[建立資料表](#console_timestream.table.using-console)。

1. 在查詢編輯器中，您可以執行查詢。若要查看資料表中最新的 10 列，請執行：

   ```
   SELECT * FROM <database_name>.<table_name> ORDER BY time DESC LIMIT 10
   ```

1. （選用） 開啟**啟用洞**見，以取得有關查詢效率的洞見。

## 建立排程查詢
<a name="console_timestream.scheduledquery.using-console"></a>

請依照下列步驟，使用 AWS 主控台建立排程查詢。

1. 開啟 [AWS 主控台](https://console.aws.amazon.com/timestream)。

1. 在導覽窗格中，選擇**排程查詢**。

1. 按一下**建立排程查詢**。

1. 在**查詢名稱**和**目的地資料表**區段中，輸入以下內容。
   + **名稱** - 輸入查詢名稱。
   + **資料庫名稱** - 選取在 中建立的資料庫名稱[建立 資料庫](#console_timestream.db.using-console)。
   + **資料表名稱** - 選取在 中建立的資料表名稱[建立資料表](#console_timestream.table.using-console)。

1. 在**查詢陳述**式區段中，輸入有效的查詢陳述式。然後按一下**驗證查詢**。

1. 從**目的地資料表模型**中，定義任何未定義屬性的模型。您可以使用**視覺化建置器**或 JSON。

1. 在**執行排程**區段中，選擇**固定速率**或 **Chron 表達式**。如需時間表達式，請參閱[排程查詢的排程表達式](https://docs.aws.amazon.com/timestream/latest/developerguide/scheduledqueries-schedule.html)，以取得排程表達式的詳細資訊。

1. 在 **SNS 主題**區段中，輸入將用於通知的 SNS 主題。

1. 在**錯誤日誌報告**區段中，輸入將用於報告錯誤的 S3 位置。

   選擇 **Encryption key type** (加密金鑰類型)。

1. 在 **AWS KMS 金鑰****的安全設定**區段中，選擇 AWS KMS 金鑰的類型。

   輸入 Timestream for LiveAnalytics 用來執行排程查詢的 **IAM 角色**。如需角色所需許可和信任關係的詳細資訊，請參閱[排程查詢的 IAM 政策範例](https://docs.aws.amazon.com/timestream/latest/developerguide/security_iam_id-based-policy-examples.html#security_iam_id-based-policy-examples-sheduledqueries)。

1.  按一下**建立排程查詢**。

## 刪除排程查詢
<a name="console_timestream.scheduledquerydeletedisable.using-console"></a>

請依照下列步驟，使用 AWS 主控台刪除或停用排程查詢。

1. 開啟 [AWS 主控台](https://console.aws.amazon.com/timestream)。

1. 在導覽窗格中，選擇**排程查詢**

1. 選取在 中建立的排程查詢[建立排程查詢](#console_timestream.scheduledquery.using-console)。

1. 選取**動作**。

1. 選擇**停用****或刪除**。

1. 如果您選取刪除，請確認動作，然後選取**刪除**。

## 刪除資料表
<a name="console_timestream.delete-table.using-console"></a>

請依照下列步驟，使用 AWS 主控台刪除資料庫。

1. 開啟 [AWS 主控台](https://console.aws.amazon.com/timestream)。

1. 在導覽窗格中，選擇**資料表**

1. 選取您在 中建立的資料表[建立資料表](#console_timestream.table.using-console)。

1. 按一下 **Delete (刪除)**。

1. 在確認方塊中輸入*刪除*。

## 刪除資料庫
<a name="console_timestream.delete-db.using-console"></a>

請依照下列步驟，使用 AWS 主控台刪除資料庫：

1. 開啟 [AWS 主控台](https://console.aws.amazon.com/timestream)。

1. 在導覽窗格中，選擇**資料庫**

1. 選取您在建立資料庫中**建立的資料庫**。

1. 按一下 **Delete (刪除)**。

1. 在確認方塊中輸入*刪除*。

## 編輯資料表
<a name="console_timestream.edit-table.using-console"></a>

請依照下列步驟，使用 AWS 主控台編輯資料表。

1. 開啟 [AWS 主控台](https://console.aws.amazon.com/timestream)。

1. 在導覽窗格中，選擇**資料表**

1. 選取您在 中建立的資料表[建立資料表](#console_timestream.table.using-console)。

1. 按一下**編輯**

1. 編輯資料表詳細資訊並儲存。
   + **記憶體存放區保留** - 指定您希望在記憶體存放區中保留資料的時間長度。記憶體存放區會處理傳入資料，包括延遲抵達資料 （時間戳記早於目前時間的資料），並針對快速point-in-time查詢進行最佳化。
   + **磁性存放區保留** - 指定您希望在磁性存放區中保留資料的時間長度。磁性存放區適用於長期儲存，並針對快速分析查詢進行最佳化。

## 編輯資料庫
<a name="console_timestream.edit-db.using-console"></a>

請依照下列步驟，使用 AWS 主控台編輯資料庫。

1. 開啟 [AWS 主控台](https://console.aws.amazon.com/timestream)。

1. 在導覽窗格中，選擇**資料庫**

1. 選取您在建立資料庫中**建立的資料庫**。

1. 按一下**編輯**

1. 編輯資料庫詳細資訊並儲存。

# 使用 存取 Amazon Timestream for LiveAnalytics AWS CLI
<a name="Tools.CLI"></a>

 您可以使用 AWS Command Line Interface (AWS CLI) 從命令列控制多項 AWS 服務，並透過指令碼將其自動化。您可以使用 AWS CLI 進行臨機操作。您也可以使用它在公用程式指令碼中嵌入 Amazon Timestream for LiveAnalytics 操作。

 您必須先設定程式設計存取，才能 AWS CLI 搭配 Timestream for LiveAnalytics 使用 。如需詳細資訊，請參閱[授與程式設計存取權](accessing.md#programmatic-access)。

如需 中 Timestream for LiveAnalytics 查詢 API 可用之所有命令的完整清單 AWS CLI，請參閱 [AWS CLI 命令參考](https://docs.aws.amazon.com/cli/latest/reference/timestream-query/index.html)。

如需 中 Timestream for LiveAnalytics Write API 可用的所有命令的完整清單 AWS CLI，請參閱 [AWS CLI 命令參考](https://docs.aws.amazon.com/cli/latest/reference/timestream-write/index.html)。

**Topics**
+ [下載和設定 AWS CLI](#Tools.CLI.DownloadingAndRunning)
+ [AWS CLI 搭配 Timestream for LiveAnalytics 使用](#Tools.CLI.UsingWithQLDB)

## 下載和設定 AWS CLI
<a name="Tools.CLI.DownloadingAndRunning"></a>

 AWS CLI 會在 Windows、macOS 或 Linux 上執行。若要下載、安裝和設定它，請依照下列步驟執行：

1. 在 AWS CLI https：//[http://aws.amazon.com/cli](https://aws.amazon.com/cli) 下載 。

1. 遵循*AWS Command Line Interface 《 使用者指南*》中[安裝 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/installing.html) 和[設定 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html) 的指示。

## AWS CLI 搭配 Timestream for LiveAnalytics 使用
<a name="Tools.CLI.UsingWithQLDB"></a>

命令列格式包含 Amazon Timestream for LiveAnalytics 操作名稱，後面接著該操作的參數。除了 JSON 之外， AWS CLI 還支援參數值的速記語法。

 使用 `help` 列出 Timestream for LiveAnalytics 中的所有可用命令。例如：

```
aws timestream-write help
```

```
aws timestream-query help
```

 您也可以使用 `help` 描述特定命令，並進一步了解其用量：

```
aws timestream-write create-database help
```

 例如，若要建立資料庫：

```
aws timestream-write create-database --database-name myFirstDatabase
```

 若要建立已啟用磁性存放區寫入的資料表：

```
aws timestream-write create-table \
--database-name metricsdb \
--table-name metrics \
--magnetic-store-write-properties "{\"EnableMagneticStoreWrites\": true}"
```

使用單一度量記錄寫入資料：

```
aws timestream-write write-records \
--database-name metricsdb \
--table-name metrics \
--common-attributes "{\"Dimensions\":[{\"Name\":\"asset_id\", \"Value\":\"100\"}], \"Time\":\"1631051324000\",\"TimeUnit\":\"MILLISECONDS\"}" \
--records "[{\"MeasureName\":\"temperature\", \"MeasureValueType\":\"DOUBLE\",\"MeasureValue\":\"30\"},{\"MeasureName\":\"windspeed\", \"MeasureValueType\":\"DOUBLE\",\"MeasureValue\":\"7\"},{\"MeasureName\":\"humidity\", \"MeasureValueType\":\"DOUBLE\",\"MeasureValue\":\"15\"},{\"MeasureName\":\"brightness\", \"MeasureValueType\":\"DOUBLE\",\"MeasureValue\":\"17\"}]"
```

使用多度量記錄寫入資料：

```
# wide model helper method to create Multi-measure records
function ingest_multi_measure_records {
  epoch=`date +%s`
  epoch+=$i

  # multi-measure records
  aws timestream-write write-records \
  --database-name $src_db_wide \
  --table-name $src_tbl_wide \
  --common-attributes "{\"Dimensions\":[{\"Name\":\"device_id\", \
              \"Value\":\"12345678\"},\
            {\"Name\":\"device_type\", \"Value\":\"iPhone\"}, \
            {\"Name\":\"os_version\", \"Value\":\"14.8\"}, \
            {\"Name\":\"region\", \"Value\":\"us-east-1\"} ], \
            \"Time\":\"$epoch\",\"TimeUnit\":\"MILLISECONDS\"}" \
--records "[{\"MeasureName\":\"video_metrics\", \"MeasureValueType\":\"MULTI\", \
  \"MeasureValues\": \
  [{\"Name\":\"video_startup_time\",\"Value\":\"0\",\"Type\":\"BIGINT\"}, \
  {\"Name\":\"rebuffering_ratio\",\"Value\":\"0.5\",\"Type\":\"DOUBLE\"}, \
  {\"Name\":\"video_playback_failures\",\"Value\":\"0\",\"Type\":\"BIGINT\"}, \
  {\"Name\":\"average_frame_rate\",\"Value\":\"0.5\",\"Type\":\"DOUBLE\"}]}]" \
--endpoint-url $ingest_endpoint \
  --region  $region
}

# create 5 records
for i in {100..105};
  do ingest_multi_measure_records $i;
done
```

查詢資料表：

```
aws timestream-query query \
--query-string "SELECT time, device_id, device_type, os_version, 
region, video_startup_time, rebuffering_ratio, video_playback_failures, \
average_frame_rate \
FROM metricsdb.metrics \
where time >= ago (15m)"
```

若要建立排程查詢：

```
aws timestream-query create-scheduled-query \
  --name scheduled_query_name \
  --query-string "select bin(time, 1m) as time, \
          avg(measure_value::double) as avg_cpu, min(measure_value::double) as min_cpu, region \
          from $src_db.$src_tbl where measure_name = 'cpu' \
          and time BETWEEN @scheduled_runtime - (interval '5' minute)  AND @scheduled_runtime \
          group by region, bin(time, 1m)" \
  --schedule-configuration "{\"ScheduleExpression\":\"$cron_exp\"}" \
  --notification-configuration "{\"SnsConfiguration\":{\"TopicArn\":\"$sns_topic_arn\"}}" \
  --scheduled-query-execution-role-arn "arn:aws:iam::452360119086:role/TimestreamSQExecutionRole" \
  --target-configuration "{\"TimestreamConfiguration\":{\
          \"DatabaseName\": \"$dest_db\",\
          \"TableName\": \"$dest_tbl\",\
          \"TimeColumn\":\"time\",\
          \"DimensionMappings\":[{\
            \"Name\": \"region\", \"DimensionValueType\": \"VARCHAR\"
          }],\
          \"MultiMeasureMappings\":{\
            \"TargetMultiMeasureName\": \"mma_name\",
            \"MultiMeasureAttributeMappings\":[{\
              \"SourceColumn\": \"avg_cpu\", \"MeasureValueType\": \"DOUBLE\", \"TargetMultiMeasureAttributeName\": \"target_avg_cpu\"
            },\
            { \
              \"SourceColumn\": \"min_cpu\", \"MeasureValueType\": \"DOUBLE\", \"TargetMultiMeasureAttributeName\": \"target_min_cpu\"
            }] \
          }\
          }}" \
  --error-report-configuration "{\"S3Configuration\": {\
        \"BucketName\": \"$s3_err_bucket\",\
        \"ObjectKeyPrefix\": \"scherrors\",\
        \"EncryptionOption\": \"SSE_S3\"\
        }\
      }"
```

# 使用 API
<a name="Using.API"></a>

 除了 [SDKs](getting-started-sdks.md) 之外，Amazon Timestream for LiveAnalytics 透過*端點探索模式*提供直接 REST API 存取。端點探索模式及其使用案例如下所述。

## 端點探索模式
<a name="Using-API.endpoint-discovery"></a>

由於 Timestream Live Analytics SDKs 旨在透明地使用服務的架構，包括服務端點的管理和映射，因此建議您將 SDKs用於大多數應用程式。不過，有些執行個體需要使用 Timestream for LiveAnalytics REST API 端點探索模式：
+ 您使用 [VPC 端點 (AWS PrivateLink) 搭配 Timestream for LiveAnalytics](VPCEndpoints.md)
+ 您的應用程式使用尚未支援 SDK 的程式設計語言
+ 您需要更好地控制用戶端實作

本節包含端點探索模式如何運作、如何實作端點探索模式，以及用量備註的相關資訊。選取以下主題以進一步了解。

**Topics**
+ [端點探索模式](#Using-API.endpoint-discovery)
+ [端點探索模式的運作方式](Using-API.endpoint-discovery.how-it-works.md)
+ [實作端點探索模式](Using-API.endpoint-discovery.describe-endpoints.implementation.md)

# 端點探索模式的運作方式
<a name="Using-API.endpoint-discovery.how-it-works"></a>

 Timestream 使用[行動架構](architecture.md#cells)建置，以確保更好的擴展和流量隔離屬性。由於每個客戶帳戶都會對應至區域中的特定儲存格，因此您的應用程式必須使用帳戶已對應至的正確儲存格特定端點。使用 SDKs時，系統會為您透明地處理此映射，您不需要管理儲存格特定的端點。不過，當直接存取 REST API 時，您需要自行管理和映射正確的端點。此程序為*端點探索模式*，如下所述：

1.  端點探索模式會從呼叫 `DescribeEndpoints`動作開始 （如 [https://docs.aws.amazon.com/timestream/latest/developerguide/API_Reference.html](https://docs.aws.amazon.com/timestream/latest/developerguide/API_Reference.html) 節所述）。

1.  端點應在傳回time-to-live(TTL) 值 () 指定的時間內快取和重複使用[https://docs.aws.amazon.com/timestream/latest/developerguide/API_Endpoint.html#timestream-Type-Endpoint-CachePeriodInMinutes.html](https://docs.aws.amazon.com/timestream/latest/developerguide/API_Endpoint.html#timestream-Type-Endpoint-CachePeriodInMinutes.html)。然後，可以在 TTL 期間呼叫 Timestream Live Analytics API。

1.  TTL 過期後，應對 DescribeEndpoints 進行新的呼叫以重新整理端點 （換言之，從步驟 1 開始）。

**注意**  
 `DescribeEndpoints` 動作的語法、參數和其他用量資訊如 [API 參考](https://docs.aws.amazon.com/timestream/latest/developerguide/API_DescribeEndpoints.html)所述。請注意， `DescribeEndpoints`動作可透過兩個 SDKs，而且每個 SDK 都相同。

如需端點探索模式的實作，請參閱 [實作端點探索模式](Using-API.endpoint-discovery.describe-endpoints.implementation.md)。

# 實作端點探索模式
<a name="Using-API.endpoint-discovery.describe-endpoints.implementation"></a>

 若要實作端點探索模式，請選擇 API （寫入或查詢）、建立 **DescribeEndpoints** 請求，並在傳回的 TTL 值 (s) 期間使用傳回的端點。實作程序如下所述。

**注意**  
確保您熟悉[用量備註](#Using-API.endpoint-discovery.describe-endpoints.usage-notes)。

## 實作程序
<a name="Using-API.endpoint-discovery.describe-endpoints.implementation.procedure"></a>

1.  使用 [https://docs.aws.amazon.com/timestream/latest/developerguide/API_DescribeEndpoints.html](https://docs.aws.amazon.com/timestream/latest/developerguide/API_DescribeEndpoints.html)請求，取得您要呼叫的 API 端點 [（寫入](https://docs.aws.amazon.com/timestream/latest/developerguide/API_Operations_Amazon_Timestream_Write.html)或[查詢](https://docs.aws.amazon.com/timestream/latest/developerguide/API_Operations_Amazon_Timestream_Query.html))。

   1.  使用下列兩個端點之一，為 建立[https://docs.aws.amazon.com/timestream/latest/developerguide/API_DescribeEndpoints.html](https://docs.aws.amazon.com/timestream/latest/developerguide/API_DescribeEndpoints.html)對應至所關注 API [（寫入](https://docs.aws.amazon.com/timestream/latest/developerguide/API_Operations_Amazon_Timestream_Write.html)或[查詢](https://docs.aws.amazon.com/timestream/latest/developerguide/API_Operations_Amazon_Timestream_Query.html)) 的請求。請求沒有輸入參數。請務必閱讀以下備註。  
*寫入 SDK：*  

      ```
      ingest.timestream.<region>.amazonaws.com
      ```  
*查詢 SDK：*  

      ```
      query.timestream.<region>.amazonaws.com
      ```

      區域 的 CLI 呼叫範例`us-east-1`如下。

      ```
      REGION_ENDPOINT="https://query.timestream.us-east-1.amazonaws.com"
      REGION=us-east-1
      aws timestream-write describe-endpoints \
      --endpoint-url $REGION_ENDPOINT \
      --region $REGION
      ```
**注意**  
 HTTP "Host" 標頭*也必須*包含 API 端點。如果未填入 標頭，請求將會失敗。這是所有 HTTP/1.1 請求的標準需求。如果您使用支援 1.1 或更新版本的 HTTP 程式庫，HTTP 程式庫應該會自動為您填入 標頭。
**注意**  
將 *<region>* 替換為請求所在區域的區域識別符，例如 `us-east-1`

   1. 剖析 （擷取） 端點和快取 TTL 值的回應。回應是一或多個[`Endpoint`物件 ](https://docs.aws.amazon.com/timestream/latest/developerguide/API_Endpoint.html)的陣列。每個`Endpoint`物件都包含端點地址 (`Address`) 和該端點的 TTL (`CachePeriodInMinutes`)。

1.  快取端點，直到達到指定的 TTL。

1.  當 TTL 過期時，從實作的步驟 1 重新開始擷取新的端點。

## 端點探索模式的使用備註
<a name="Using-API.endpoint-discovery.describe-endpoints.usage-notes"></a>
+ **DescribeEndpoints** 動作是 Timestream Live Analytics 區域端點唯一可辨識的動作。
+ 回應包含要對其進行 Timestream Live Analytics API 呼叫的端點清單。
+  成功回應時，清單中應該至少有一個端點。如果清單中有多個端點，則任何端點都同樣可用於 API 呼叫，而且發起人可以選擇隨機使用的端點。
+ 除了端點的 DNS 地址之外，清單中的每個端點都會指定允許使用以分鐘為單位指定端點的存留時間 (TTL)。
+ 應在傳回的 TTL 值指定的時間內快取和重複使用端點 （以分鐘為單位）。TTL 過期後，應對 **DescribeEndpoints** 進行新的呼叫，以重新整理要使用的端點，因為 TTL 過期後，端點將無法再運作。

# 使用 AWS SDKs
<a name="getting-started-sdks"></a>

 您可以使用 AWS SDKs 存取 Amazon Timestream。Timestream 支援每種語言兩個 SDKs；即寫入 SDK 和查詢 SDK。寫入 SDK 用於執行 CRUD 操作，並將您的時間序列資料插入 Timestream。查詢 SDK 用於查詢儲存在 Timestream 中的現有時間序列資料。

完成所選 SDK 的必要先決條件後，您就可以開始使用 [程式碼範例](code-samples.md)。

**Topics**
+ [Java](getting-started.java.md)
+ [Java v2](getting-started.java-v2.md)
+ [Go](getting-started.go.md)
+ [Python](getting-started.python.md)
+ [Node.js](getting-started.node-js.md)
+ [.NET](getting-started.dot-net.md)

# Java
<a name="getting-started.java"></a>

若要開始使用 [Java 1.0 開發套件](https://aws.amazon.com/sdk-for-java/)和 Amazon Timestream，請完成先決條件，如下所述。

完成 Java 開發套件的必要先決條件後，您就可以開始使用 [程式碼範例](code-samples.md)。

## 先決條件
<a name="getting-started.java.prereqs"></a>

開始使用 Java 之前，您必須執行下列動作：

1. 遵循中的 AWS 設定說明[存取 LiveAnalytics 的 Timestream](accessing.md)。

1. 透過下載並安裝下列項目來設定 Java 開發環境：
   + Java SE 開發套件 8 （例如 [Amazon Corretto 8](https://docs.aws.amazon.com/corretto/latest/corretto-8-ug/downloads-list.html))。
   + Java IDE （例如 [Eclipse](http://www.eclipse.org) 或 [IntelliJ](https://www.jetbrains.com/idea/))。

      如需詳細資訊，請參閱 [入門 適用於 Java 的 AWS SDK](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html) 

1. 設定您的 AWS 登入資料和區域以進行開發：
   + 設定您的 AWS 安全登入資料以搭配 使用 適用於 Java 的 AWS SDK。
   + 設定您的 AWS 區域，以判斷您預設的 LiveAnalytics Timestream 端點。

## 使用 Apache Maven
<a name="getting-started.java.with-maven"></a>

 您可以使用 [Apache Maven ](https://maven.apache.org/) 來設定和建置 適用於 Java 的 AWS SDK 專案。

**注意**  
若要使用 Apache Maven，請確定您的 Java 開發套件和執行期為 1.8 或更新版本。

您可以將 AWS SDK 設定為 Maven 相依性，如[搭配使用 SDK 與 Apache Maven](https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-project-maven.html) 中所述。

您可以使用下列命令執行編譯並執行原始程式碼：

```
mvn clean compile
mvn exec:java -Dexec.mainClass=<your source code Main class>
```

**注意**  
 `<your source code Main class>` 是 Java 原始碼主要類別的路徑。

## 設定您的 AWS 登入資料
<a name="getting-started.java.credentials"></a>

[適用於 Java 的 AWS SDK](https://aws.amazon.com/sdk-for-java) 需要您在執行時間提供 AWS 登入資料給應用程式。本指南中的程式碼範例假設您使用 AWS 登入資料檔案，如《 *適用於 Java 的 AWS SDK 開發人員指南*》中的[設定 AWS 登入資料和開發區域](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup-credentials.html)中所述。

以下是名為 的 AWS 登入資料檔案範例`~/.aws/credentials`，其中波狀符號字元 (`~`) 代表您的主目錄。

```
[default] 
aws_access_key_id = AWS access key ID goes here 
aws_secret_access_key = Secret key goes here
```

# Java v2
<a name="getting-started.java-v2"></a>

若要開始使用 [Java 2.0 開發套件](https://aws.amazon.com/sdk-for-java/)和 Amazon Timestream，請完成先決條件，如下所述。

完成 Java 2.0 開發套件的必要先決條件後，您就可以開始使用 [程式碼範例](code-samples.md)。

## 先決條件
<a name="getting-started.java-v2.prereqs"></a>

開始使用 Java 之前，您必須執行下列動作：

1. 遵循中的 AWS 設定說明[存取 LiveAnalytics 的 Timestream](accessing.md)。

1. 您可以將 AWS SDK 設定為 Maven 相依性，如[搭配使用 SDK 與 Apache Maven](https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/welcome.html) 中所述。

1. 透過下載並安裝下列項目來設定 Java 開發環境：
   + Java SE 開發套件 8 （例如 [Amazon Corretto 8](https://docs.aws.amazon.com/corretto/latest/corretto-8-ug/downloads-list.html))。
   + Java IDE （例如 [Eclipse](http://www.eclipse.org) 或 [IntelliJ](https://www.jetbrains.com/idea/))。

      如需詳細資訊，請參閱 [入門 適用於 Java 的 AWS SDK](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html) 

## 使用 Apache Maven
<a name="getting-started.java-v2.with-maven"></a>

 您可以使用 [Apache Maven ](https://maven.apache.org/) 來設定和建置 適用於 Java 的 AWS SDK 專案。

**注意**  
若要使用 Apache Maven，請確定您的 Java 開發套件和執行期為 1.8 或更新版本。

您可以將 AWS SDK 設定為 Maven 相依性，如[搭配使用 SDK 與 Apache Maven](https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/welcome.html) 中所述。[此處](https://docs.aws.amazon.com/sdk-for-java/v2/migration-guide/whats-different.html#adding-v2)說明 pom.xml 檔案所需的變更。

您可以使用下列命令執行編譯並執行原始程式碼：

```
mvn clean compile
mvn exec:java -Dexec.mainClass=<your source code Main class>
```

**注意**  
 `<your source code Main class>` 是 Java 原始碼主要類別的路徑。

# Go
<a name="getting-started.go"></a>

若要開始使用 [Go SDK](https://aws.amazon.com/sdk-for-go/) 和 Amazon Timestream，請完成先決條件，如下所述。

完成 Go 開發套件的必要先決條件後，您就可以開始使用 [程式碼範例](code-samples.md)。

## 先決條件
<a name="getting-started.prereqs.go"></a>

1.  [下載 GO SDK 1.14](https://golang.org/doc/install)。

1.  [設定 GO 開發套件](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html)。

1.  [建構您的用戶端](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html)。

# Python
<a name="getting-started.python"></a>

若要開始使用 [Python SDK](https://aws.amazon.com/sdk-for-python/) 和 Amazon Timestream，請完成先決條件，如下所述。

完成 Python SDK 的必要先決條件後，您就可以開始使用 [程式碼範例](code-samples.md)。

## 先決條件
<a name="getting-started.python.prereqs"></a>

若要使用 Python，請依照[此處](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html)的指示安裝和設定 Boto3。

# Node.js
<a name="getting-started.node-js"></a>

若要開始使用 [Node.js SDK](https://aws.amazon.com/sdk-for-node-js/) 和 Amazon Timestream，請完成先決條件，如下所述。

完成 Node.js 開發套件的必要先決條件後，您就可以開始使用 [程式碼範例](code-samples.md)。

## 先決條件
<a name="getting-started.node-js.prereqs"></a>

開始使用 Node.js 之前，您必須執行下列動作：

1. [安裝 Node.js](https://nodejs.org/en/)。

1.  [安裝適用於 JavaScript 的 AWS SDK](https://aws.amazon.com/sdk-for-node-js/)。

# .NET
<a name="getting-started.dot-net"></a>

若要開始使用 [.NET SDK](https://aws.amazon.com/sdk-for-net/) 和 Amazon Timestream，請完成先決條件，如下所述。

完成 .NET 開發套件的必要先決條件後，您就可以開始使用 [程式碼範例](code-samples.md)。

## 先決條件
<a name="getting-started.dot-net.prereqs"></a>

開始使用 .NET 之前，請安裝所需的 NuGet 套件，並執行下列命令來確保 AWSSDK.Core 版本為 3.3.107 或更新版本：

```
dotnet add package AWSSDK.Core
dotnet add package AWSSDK.TimestreamWrite
dotnet add package AWSSDK.TimestreamQuery
```