

終止支援通知：在 2025 年 12 月 15 日， AWS 將終止對 的支援 AWS IoT Analytics。2025 年 12 月 15 日之後，您將無法再存取 AWS IoT Analytics 主控台或 AWS IoT Analytics 資源。如需詳細資訊，請參閱[AWS IoT Analytics 終止支援](https://docs.aws.amazon.com/iotanalytics/latest/userguide/iotanalytics-end-of-support.html)。

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

# 步驟 2：匯出先前擷取的資料
<a name="export-previous-data"></a>

 對於先前擷取並存放於 的資料 AWS IoT Analytics，您需要將其匯出至 Amazon S3。若要簡化此程序，您可以使用 CloudFormation 範本來自動化整個資料匯出工作流程。您可以使用指令碼進行部分 （時間範圍型） 資料擷取。

![\[使用 CloudFormation 匯出先前擷取資料的架構\]](http://docs.aws.amazon.com/zh_tw/iotanalytics/latest/userguide/images/ingested-architecture.png)


## CloudFormation 將資料匯出至 Amazon S3 的 範本
<a name="cfn-data-ingestion"></a>

上圖說明使用 CloudFormation 範本在相同資料 AWS IoT Analytics 存放區中建立資料集的程序，以根據時間戳記進行選擇。這可讓使用者在所需的時間範圍內擷取特定資料點。此外，還會建立內容交付規則，將資料匯出至 Amazon S3 儲存貯體。

以下程序說明步驟。

1. 準備 CloudFormation 範本並將其儲存為 YAML 檔案。例如 `migrate-datasource.yaml`。

   ```
   # Cloudformation Template to migrate an AWS IoT Analytics datastore to an external dataset
   AWSTemplateFormatVersion: 2010-09-09
   Description: Migrate an AWS IoT Analytics datastore to an external dataset
   Parameters:
     DatastoreName:
       Type: String
       Description: The name of the datastore to migrate.
       AllowedPattern: ^[a-zA-Z0-9_]+$
     TimeRange:
       Type: String
       Description: |
         This is an optional argument to split the source data into multiple files.
         The value should follow the SQL syntax of WHERE clause.
         E.g. WHERE DATE(Item_TimeStamp) BETWEEN '09/16/2010 05:00:00' and '09/21/2010 09:00:00'.
       Default: ''
     MigrationS3Bucket:
       Type: String
       Description: The S3 Bucket where the datastore will be migrated to.
       AllowedPattern: (?!(^xn--|.+-s3alias$))^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$
     MigrationS3BucketPrefix:
       Type: String
       Description: The prefix of the S3 Bucket where the datastore will be migrated to.
       Default: ''
       AllowedPattern: (^([a-zA-Z0-9.\-_]*\/)*$)|(^$)
   Resources:
     # IAM Role to be assumed by the AWS IoT Analytics service to access the external dataset
     DatastoreMigrationRole:
       Type: AWS::IAM::Role
       Properties:
         AssumeRolePolicyDocument:
           Version: 2012-10-17		 	 	 
           Statement:
             - Effect: Allow
               Principal:
                 Service: iotanalytics.amazonaws.com
               Action: sts:AssumeRole
         Policies:
           - PolicyName: AllowAccessToExternalDataset
             PolicyDocument:
               Version: 2012-10-17		 	 	 
               Statement:
                 - Effect: Allow
                   Action:
                     - s3:GetBucketLocation
                     - s3:GetObject
                     - s3:ListBucket
                     - s3:ListBucketMultipartUploads
                     - s3:ListMultipartUploadParts
                     - s3:AbortMultipartUpload
                     - s3:PutObject
                     - s3:DeleteObject
                   Resource:
                     - !Sub arn:aws:s3:::${MigrationS3Bucket}
                     - !Sub arn:aws:s3:::${MigrationS3Bucket}/${MigrationS3BucketPrefix}*
   
     # This dataset that will be created in the external S3 Export
     MigratedDataset:
       Type: AWS::IoTAnalytics::Dataset
       Properties:
         DatasetName: !Sub ${DatastoreName}_generated
         Actions:
           - ActionName: SqlAction
             QueryAction:
               SqlQuery: !Sub SELECT * FROM ${DatastoreName} ${TimeRange}
         ContentDeliveryRules:
           - Destination:
               S3DestinationConfiguration:
                 Bucket: !Ref MigrationS3Bucket
                 Key: !Sub ${MigrationS3BucketPrefix}${DatastoreName}/!{iotanalytics:scheduleTime}/!{iotanalytics:versionId}.csv
                 RoleArn: !GetAtt DatastoreMigrationRole.Arn
         RetentionPeriod:
           Unlimited: true
         VersioningConfiguration:
           Unlimited: true
   ```

1.  決定需要匯出資料的資料 AWS IoT Analytics 存放區。針對本指南，我們將使用名為 的範例資料存放區`iot_analytics_datastore`。  
![\[識別 AWS IoT Analytics 資料存放區：\]](http://docs.aws.amazon.com/zh_tw/iotanalytics/latest/userguide/images/datastore.png)

1.  建立或識別要匯出資料的 Amazon S3 儲存貯體。針對本指南，我們將使用 儲存`iot-analytics-export`貯體。  
![\[建立或識別 Amazon S3 儲存貯體\]](http://docs.aws.amazon.com/zh_tw/iotanalytics/latest/userguide/images/export-s3-bucket.png)

1.  建立 CloudFormation 堆疊。
   + 導覽至 https：//[https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/)。
   + 按一下**建立堆疊**，然後選取**使用新資源 （標準）**。
   + 上傳 `migrate-datasource.yaml` 檔案。  
![\[上傳至 CFN 主控台\]](http://docs.aws.amazon.com/zh_tw/iotanalytics/latest/userguide/images/cfn-console-link.png)

1.  輸入堆疊名稱並提供下列參數：
   + **DatastoreName**：您要遷移 AWS IoT Analytics 的資料存放區名稱。
   + **MigrationS3Bucket**：存放遷移資料的 Amazon S3 儲存貯體。
   + **MigrationS3BucketPrefix** （選用） ：Amazon S3 儲存貯體的字首。
   + **TimeRange** （選用） ：用於篩選匯出資料的`SQL WHERE`子句，允許根據指定的時間範圍將來源資料分割成多個檔案。  
![\[CFN 主控台堆疊詳細資訊\]](http://docs.aws.amazon.com/zh_tw/iotanalytics/latest/userguide/images/cfn-console-details.png)

1. 在設定堆疊選項畫面上按一下**下一步**。

1.  選取核取方塊以確認建立 IAM 資源，然後按一下**提交**。  
![\[CFN 主控台堆疊詳細資訊\]](http://docs.aws.amazon.com/zh_tw/iotanalytics/latest/userguide/images/cfn-capabilities.png)

1.  檢閱**事件**索引標籤上的堆疊建立是否完成。  
![\[事件索引標籤\]](http://docs.aws.amazon.com/zh_tw/iotanalytics/latest/userguide/images/events-tab.png)

1.  成功完成堆疊後，導覽至 **AWS IoT Analytics → 資料集**以檢視遷移的資料集。  
![\[檢視資料集\]](http://docs.aws.amazon.com/zh_tw/iotanalytics/latest/userguide/images/view-datasets.png)

1.  選取產生的資料集，然後按一下**立即執行**以匯出資料集。  
![\[執行資料集\]](http://docs.aws.amazon.com/zh_tw/iotanalytics/latest/userguide/images/run-datasets.png)

1.  您可以在資料集的內容索引標籤上檢視**內容**。  
![\[內容資料集索引標籤\]](http://docs.aws.amazon.com/zh_tw/iotanalytics/latest/userguide/images/content-datasets.png)

1.  最後，在 Amazon S3 主控台中開啟 **iot-analytics-export** 儲存貯體來檢閱匯出的內容。  
![\[事件索引標籤\]](http://docs.aws.amazon.com/zh_tw/iotanalytics/latest/userguide/images/final-review.png)