

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

# 使用 Kinesis Data Streams 建立 Studio 筆記本
<a name="example-notebook-streams"></a>

本教學課程說明如何建立使用 Kinesis 資料串流作為來源的 Studio 筆記本。

**Topics**
+ [完成先決條件](#example-notebook-streams-setup)
+ [建立 AWS Glue 資料表](#example-notebook-streams-glue)
+ [使用 Kinesis Data Streams 建立 Studio 筆記本](#example-notebook-streams-create)
+ [將資料傳送至 Kinesis 資料串流](#example-notebook-streams-send)
+ [測試 Studio 筆記本](#example-notebook-streams-test)

## 完成先決條件
<a name="example-notebook-streams-setup"></a>

建立 Studio 筆記本之前，請先建立 Kinesis 資料串流 (`ExampleInputStream`)。您的應用程式使用此串流作為應用程式來源。

您可以使用 Amazon Kinesis 主控台或以下 AWS CLI 命令來建立該串流。如需主控台指示，請參閱《Amazon Kinesis Data Streams 開發人員指南》中的[建立和更新資料串流](https://docs.aws.amazon.com/kinesis/latest/dev/amazon-kinesis-streams.html)**。將該串流命名為 **ExampleInputStream**，並將**開啟的碎片數量**設定為 **1**。

若要使用 建立串流 (`ExampleInputStream`) AWS CLI，請使用下列 Amazon Kinesis `create-stream` AWS CLI 命令。

```
$ aws kinesis create-stream \
--stream-name ExampleInputStream \
--shard-count 1 \
--region us-east-1 \
--profile adminuser
```

## 建立 AWS Glue 資料表
<a name="example-notebook-streams-glue"></a>

您的 Studio 筆記本使用 [AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/what-is-glue.html) 資料庫取得有關 Kinesis Data Streams 資料來源的中繼資料。

**注意**  
您可以先手動建立資料庫，也可以讓 Managed Service for Apache Flink 在您建立筆記本時為您建立資料庫。同樣，您可以依照本節所述手動建立資料表，也可以在 Apache Zeppelin 的筆記本中，使用針對 Managed Service for Apache Flink 的建立資料表連接器程式碼，透過 DDL 陳述式建立資料表。然後，您可以簽入 AWS Glue ，以確保正確建立資料表。

**建立資料表**

1. 登入 AWS 管理主控台 ，並在 https：//[https://console.aws.amazon.com/glue/](https://console.aws.amazon.com/glue/) 開啟 AWS Glue 主控台。

1. 如果您還沒有 AWS Glue 資料庫，請從左側導覽列中選擇**資料庫**。選擇**新增資料庫**。在**新增資料庫**視窗中，為**資料庫名稱**輸入 **default**。選擇 **Create** (建立)。

1. 在左側導覽列中，選擇**資料表**。在**資料表**頁面，選擇**新增資料表** > **手動新增資料表**。

1. 在**設定資料表**頁面，為**資料表名稱**輸入 **stock**。請務必選取先前建立的資料庫。選擇 **Next (下一步)**。

1. 在**新增資料存放區**頁面，選擇 **Kinesis**。對於**串流名稱**，輸入 **ExampleInputStream**。針對 **Kinesis 來源 URL**，請選擇輸入 **https://kinesis.us-east-1.amazonaws.com**。如果您複製並貼上 **Kinesis 來源 URL**，請務必刪除任何前置或尾端空格。選擇 **Next (下一步)**。

1. 在**分類**頁面，選擇 **JSON**。選擇 **Next (下一步)**。

1. 在**定義結構描述**頁面，選擇「新增資料欄」以新增資料欄。新增具有下列屬性的欄：    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/managed-flink/latest/java/example-notebook-streams.html)

   選擇 **Next (下一步)**。

1. 在下一頁上，確認您的設定，然後選擇**完成**。

1. 從資料表清單中選取您新建立的資料表。

1. 選擇**編輯資料表**，然後新增索引鍵為 `managed-flink.proctime` 值為 `proctime` 的屬性。

1. 選擇**套用**。

## 使用 Kinesis Data Streams 建立 Studio 筆記本
<a name="example-notebook-streams-create"></a>

現在，您已建立應用程式使用的資源，接下來可以建立您的 Studio 筆記本。

**Topics**
+ [使用 建立 Studio 筆記本 AWS 管理主控台](#example-notebook-create-streams-console)
+ [使用 建立 Studio 筆記本 AWS CLI](#example-notebook-msk-create-api)

### 使用 建立 Studio 筆記本 AWS 管理主控台
<a name="example-notebook-create-streams-console"></a>

1. 前往 [https://console.aws.amazon.com/managed-flink/home?region=us-east-1\$1/applications/dashboard](https://console.aws.amazon.com/managed-flink/home?region=us-east-1#/applications/dashboard) 開啟 Managed Service in the Apache Flink 主控台。

1. 在 **Managed Service for Apache Flink 應用程式**頁面，選擇 **Studio** 標籤。選擇**建立 Studio 筆記本**。
**注意**  
您也可以藉由選取輸入 Amazon MSK 叢集或 Kinesis 資料串流，然後選擇**即時處理資料**，從 Amazon MSK 或 Kinesis Data Streams 主控台建立 Studio 筆記本。

1. 在**建立 Studio 筆記本**頁面，提供下列資訊：
   + 為筆記本名稱輸入 **MyNotebook**。
   + 為 **AWS Glue 資料庫**選擇**預設值**。

   選擇**建立 Studio 筆記本**。

1. 在 **MyNotebook** 頁面，選擇**執行**。等待**狀態**顯示為**執行中**。筆記本執行時需支付費用。

### 使用 建立 Studio 筆記本 AWS CLI
<a name="example-notebook-msk-create-api"></a>

若要使用 建立 Studio 筆記本 AWS CLI，請執行下列動作：

1. 驗證您的帳戶 ID。您需要此值來建立應用程式。

1. 建立角色 `arn:aws:iam::AccountID:role/ZeppelinRole`，並將下列許可新增至主控台自動建立的角色。

   `"kinesis:GetShardIterator",`

   `"kinesis:GetRecords",`

   `"kinesis:ListShards"`

1. 建立稱為 `create.json` 的檔案，其中具有以下內容。使用您的資訊取代預留位置的值。

   ```
   {
       "ApplicationName": "MyNotebook",
       "RuntimeEnvironment": "ZEPPELIN-FLINK-3_0",
       "ApplicationMode": "INTERACTIVE",
       "ServiceExecutionRole": "arn:aws:iam::AccountID:role/ZeppelinRole",
       "ApplicationConfiguration": {
           "ApplicationSnapshotConfiguration": {
               "SnapshotsEnabled": false
           },
           "ZeppelinApplicationConfiguration": {
               "CatalogConfiguration": {
                   "GlueDataCatalogConfiguration": {
                       "DatabaseARN": "arn:aws:glue:us-east-1:AccountID:database/default"
                   }
               }
           }
       }
   }
   ```

1. 若要建立應用程式，請執行下列命令：

   ```
   aws kinesisanalyticsv2 create-application --cli-input-json file://create.json 
   ```

1. 命令完成後，您應該會看到類似如下的輸出，其中顯示新 Studio 筆記本的詳細資料：以下為輸出範例。

   ```
   {
       "ApplicationDetail": {
           "ApplicationARN": "arn:aws:kinesisanalyticsus-east-1:012345678901:application/MyNotebook",
           "ApplicationName": "MyNotebook",
           "RuntimeEnvironment": "ZEPPELIN-FLINK-3_0",
           "ApplicationMode": "INTERACTIVE",
           "ServiceExecutionRole": "arn:aws:iam::012345678901:role/ZeppelinRole",
   ...
   ```

1. 若要執行應用程式，請執行下列命令：使用您的帳戶 ID 取代範例值。

   ```
   aws kinesisanalyticsv2 start-application --application-arn arn:aws:kinesisanalyticsus-east-1:012345678901:application/MyNotebook\
   ```

## 將資料傳送至 Kinesis 資料串流
<a name="example-notebook-streams-send"></a>

若要將測試資料傳送至 Kinesis 資料串流，請執行下列動作：

1. 開啟 [Kinesis 資料產生器](https://awslabs.github.io/amazon-kinesis-data-generator/web/help.html)。

1. 選擇使用 **CloudFormation 建立 Cognito 使用者**。

1.  CloudFormation 主控台會開啟 Kinesis Data Generator 範本。選擇 **Next (下一步)**。

1. 在**指定堆疊詳細資訊**頁面，輸入 Cognito 使用者的使用者名稱和密碼。選擇 **Next (下一步)**。

1. 在**設定堆疊選項**頁面，選擇**下一步**。

1. 在**檢閱 Kinesis-Data-Generator-Cognito-User** 頁面中，選擇**我確認 AWS CloudFormation 可能會建立 IAM 資源。**核取方塊。選擇 **Create Stack** (建立堆疊)。

1. 等待 CloudFormation 堆疊完成建立。堆疊完成後，在 CloudFormation 主控台中開啟 **Kinesis-Data-Generator-Cognito-User** 堆疊，然後選擇**輸出**索引標籤。開啟針對 **KinesisDataGeneratorUrl** 輸出值所列出的 URL。

1. 在 **Amazon Kinesis 資料產生器**頁面，使用您在步驟 4 中建立的憑證登入。

1. 在下一頁面，提供下列值：    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/managed-flink/latest/java/example-notebook-streams.html)

   為**記錄範本**貼上下列程式碼：

   ```
   {
       "ticker": "{{random.arrayElement(
           ["AMZN","MSFT","GOOG"]
       )}}",
       "price": {{random.number(
           {
               "min":10,
               "max":150
           }
       )}}
   }
   ```

1. 選擇**傳送資料**。

1. 產生器會將資料傳送至 Kinesis 資料串流。

   完成下一節時，讓產生器保持執行狀態。

## 測試 Studio 筆記本
<a name="example-notebook-streams-test"></a>

在本節中，您可以使用 Studio 筆記本查詢 Kinesis 資料串流中的資料。

1. 前往 [https://console.aws.amazon.com/managed-flink/home?region=us-east-1\$1/applications/dashboard](https://console.aws.amazon.com/managed-flink/home?region=us-east-1#/applications/dashboard) 開啟 Managed Service in the Apache Flink 主控台。

1. 在 **Managed Service for Apache Flink 應用程式**頁面，選擇 **Studio 筆記本**標籤。選擇 **MyNotebook**。

1. 在 **MyNotebook** 頁面，選擇**在 Apache Zeppelin 中開啟**。

   Apache Zeppelin 介面會在新標籤中開啟。

1. 在**歡迎來到 Zeppelin\$1** 頁面，選擇 **Zeppelin 筆記**。

1. 在 **Zeppelin 筆記**頁面，在新筆記中輸入以下查詢：

   ```
   %flink.ssql(type=update)
   select * from stock
   ```

   選擇執行圖示。

   一小段時間後，筆記會顯示 Kinesis 資料串流中的資料。

若要為應用程式開啟 Apache Flink 儀表板以檢視操作層面，請選擇 **FLINK 作業**。如需 Flink 儀表板的詳細資訊，請參閱 [Managed Service for Apache Flink 開發人員指南](https://docs.aws.amazon.com/)中的 [Apache Flink 儀表板](https://docs.aws.amazon.com/managed-flink/latest/java/how-dashboard.html)。

如需 Flink 串流 SQL 查詢的更多範例，請參閱 [Apache Flink 文件](https://nightlies.apache.org/flink/flink-docs-release-1.15/)中的[查詢](https://nightlies.apache.org/flink/flink-docs-release-1.15/dev/table/sql/queries.html)。