

终止支持通知： AWS 将于 2025 年 12 月 15 日终止对的支持 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_cn/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_cn/iotanalytics/latest/userguide/images/datastore.png)

1.  创建或确定要将数据导出到的 Amazon S3 存储桶。在本指南中，我们将使用`iot-analytics-export`存储桶。  
![\[创建或识别 Amazon S3 存储桶\]](http://docs.aws.amazon.com/zh_cn/iotanalytics/latest/userguide/images/export-s3-bucket.png)

1.  创建 CloudFormation 堆栈。
   + 导航到 [https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/)。
   + 单击 “**创建堆栈**”，然后选择 “**使用新资源（标准）**”。
   + 上传 `migrate-datasource.yaml`文件。  
![\[上传到 CFN 控制台\]](http://docs.aws.amazon.com/zh_cn/iotanalytics/latest/userguide/images/cfn-console-link.png)

1.  输入堆栈名称并提供以下参数：
   + **DatastoreName**：要迁移 AWS IoT Analytics 的数据存储的名称。
   + m@@ **igrations3Bucket：存储**迁移数据的 Amazon S3 存储桶。
   + M@@ **igrationS3 BucketPrefix**（可选）：Amazon S3 存储桶的前缀。
   + **TimeRange**（可选）：用于筛选正在导出的数据的`SQL WHERE`子句，允许根据指定的时间范围将源数据拆分为多个文件。  
![\[CFN 控制台堆栈详细信息\]](http://docs.aws.amazon.com/zh_cn/iotanalytics/latest/userguide/images/cfn-console-details.png)

1. 在 “配置堆栈选项” 屏幕上单击 “**下一步**”。

1.  选中复选框以确认创建 IAM 资源，然后单击 “**提交**”。  
![\[CFN 控制台堆栈详细信息\]](http://docs.aws.amazon.com/zh_cn/iotanalytics/latest/userguide/images/cfn-capabilities.png)

1.  在 “**事件**” 选项卡上查看堆栈创建完成情况。  
![\[“事件” 选项卡\]](http://docs.aws.amazon.com/zh_cn/iotanalytics/latest/userguide/images/events-tab.png)

1.  成功完成堆栈后，导航至 **AWS IoT Analytics → 数据集**以查看迁移的数据集。  
![\[查看数据集\]](http://docs.aws.amazon.com/zh_cn/iotanalytics/latest/userguide/images/view-datasets.png)

1.  选择生成的数据集，然后单击 “**立即运行**” 导出数据集。  
![\[运行数据集\]](http://docs.aws.amazon.com/zh_cn/iotanalytics/latest/userguide/images/run-datasets.png)

1.  可以在数据集的**内容**选项卡上查看内容。  
![\[“内容数据集” 选项卡\]](http://docs.aws.amazon.com/zh_cn/iotanalytics/latest/userguide/images/content-datasets.png)

1.  最后，通过在 Amazon S3 控制台中打开**iot-analytics-export**存储桶来查看导出的内容。  
![\[“事件” 选项卡\]](http://docs.aws.amazon.com/zh_cn/iotanalytics/latest/userguide/images/final-review.png)