

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# 在 Amazon Redshift 中使用 AWS CloudFormation 共享数据入门
<a name="data-sharing-within-account-CF"></a>

您可以通过使用配置 AWS 资源的 AWS CloudFormation 堆栈，自动执行数据共享设置。CloudFormation 堆栈设置同一个 AWS 账户中两个 Amazon Redshift 集群之间的数据共享。因此，您可以在不运行 SQL 语句来预调配资源的情况下开始数据共享。

堆栈在您指定的集群上创建了数据共享。数据共享包括表格和只读示例数据。这些数据可以被另一个 Amazon Redshift 集群读取。

如果您想通过运行 SQL 语句在 AWS 账户中开始共享数据，以便在不使用 CloudFormation 的情况下设置数据共享并授予权限，请参阅[共享对 AWS 账户内数据的读取访问权限](within-account.md)。

运行数据共享 CloudFormation 堆栈之前，您必须使用有权创建 IAM 角色和 Lambda 函数的用户进行登录。您还需要同一个账户中的两个 Amazon Redshift 集群。一个作为*创建者*，用于共享示例数据，另一个作为*使用者*，用于读取该数据。对于这些集群的主要要求是每个集群均需使用 RA3 节点。有关其他要求，请参阅[Amazon Redshift 中的数据共享注意事项](datashare-considerations.md)。

有关开始设置 Amazon Redshift 集群的更多信息，请参阅 [Amazon Redshift 预置数据仓库入门](https://docs.aws.amazon.com/redshift/latest/gsg/new-user.html)。有关使用 CloudFormation 自动进行设置的更多信息，请参阅[什么是 AWS CloudFormation？](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)。

**重要**  
在启动 CloudFormation 堆栈之前，请确保在同一账户中有两个 Amazon Redshift 集群，并且这些集群使用 RA3 节点。确保每个集群都有一个数据库和一个超级用户。有关更多信息，请参阅[CREATE DATABASE](r_CREATE_DATABASE.md)和[超级用户](r_superusers.md)。

**要启动您的 CloudFormation 堆栈，以进行 Amazon Redshift 数据共享，请执行以下操作：**

1. 单击[https://console.aws.amazon.com/cloudformation/home?#/stacks/new?stackName=DataShare&templateURL=https://s3.amazonaws.com/redshift-downloads/docs-downloads/DataShare.yml](https://console.aws.amazon.com/cloudformation/home?#/stacks/new?stackName=DataShare&templateURL=https://s3.amazonaws.com/redshift-downloads/docs-downloads/DataShare.yml)，将您带到 AWS 管理控制台 中的 CloudFormation 服务。

   如果出现登录提示，请登录。

   堆栈创建过程启动，引用存储在 Amazon S3 中的 CloudFormation 模板文件。CloudFormation *模板*指的是声明组建堆栈的 AWS 资源的 JSON 格式文本文件。有关 CloudFormation 模板的更多信息，请参阅[了解模板基础知识](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/gettingstarted.templatebasics.html)。

1. 选择**下一步**，输入堆栈详细信息。

1. 根据**参数**中的每个集群，输入以下内容：
   + 您的 Amazon Redshift 集群名称，例如 **ra3-consumer-cluster**
   + 您的数据库名称，例如 **dev**
   + 数据库用户的名称，例如 **consumeruser**

   我们建议使用测试集群，因为堆栈会创建多个数据库对象。

   选择**下一步**。

1. 此时将显示堆栈选项。

   选择**下一步**以接受默认设置。

1. 在**功能**下，选择**我确认 AWS CloudFormation 可能会创建 IAM 资源**。

1. 选择**创建堆栈**。

CloudFormation 使用模板构建 Amazon Redshift 堆栈大约需要 10 分钟时间，会创建一个名为 `myproducer_share` 的数据共享。堆栈在堆栈详细信息中指定的数据库中创建数据共享。只有该数据库中的对象才能共享。

如果创建堆栈时发生错误，请执行以下操作：
+ 确保为每个 Redshift 集群输入了正确的集群名称、数据库名称和数据库用户名。
+ 确保您的集群有 RA3 节点。
+ 请确保您以具有创建 IAM 角色和 Lambda 函数的权限的用户身份登录。有关创建 IAM 角色的更多信息，请参阅[创建 IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html)。有关创建 Λ 函数的策略的更多信息，请参阅[函数开发](https://docs.aws.amazon.com/lambda/latest/dg/access-control-identity-based.html#permissions-user-function)。

## 查询您创建的数据共享
<a name="data-sharing-within-account-CF-querying"></a>

要使用以下过程，请确保您拥有在所述每个集群上运行查询所需的权限。

**要查询您的数据共享：**

1. 使用客户端工具（如 Amazon Redshift 查询编辑器 v2），连接到创建 CloudFormation 堆栈时输入的数据库上的生产者集群。

1. 查询数据共享。

   ```
   SHOW DATASHARES;
                     
   +------------------+-------------+-----------------+-------------------+------------+------------+---------------------+-----------+------------------+--------------------------------------+
   |    share_name    | share_owner | source_database | consumer_database | share_type | createdate | is_publicaccessible | share_acl | producer_account |          producer_namespace          |
   +------------------+-------------+-----------------+-------------------+------------+------------+---------------------+-----------+------------------+--------------------------------------+
   | myproducer_share | 100         | sample_data_dev | myconsumer_db     | INBOUND    | NULL       | true                | NULL      |  producer-acct   |        your-producer-namespace       |
   +------------------+-------------+-----------------+-------------------+------------+------------+---------------------+-----------+------------------+--------------------------------------+
   ```

   前面的命令返回堆栈创建的数据共享的名称，名为 `myproducer_share`。它还会返回与数据共享 `myconsumer_db` 关联的数据库名称。

   复制创建者命名空间标识符，以便在后面的步骤中使用。

1. 描述数据共享中的对象。

   ```
   DESC DATASHARE myproducer_share;
                     
   +------------------+--------------------------------------+------------+------------------+-------------+-------------------------------------+-------------+
   | producer_account |          producer_namespace          | share_type |    share_name    | object_type |             object_name             | include_new |
   +------------------+--------------------------------------+------------+------------------+-------------+-------------------------------------+-------------+
   |   producer-acct  |        your-producer-namespace       | OUTBOUND   | myproducer_share | schema      | myproducer_schema                   | true        |
   |   producer-acct  |        your-producer-namespace       | OUTBOUND   | myproducer_share | table       | myproducer_schema.tickit_sales      | NULL        |
   |   producer-acct  |        your-producer-namespace       | OUTBOUND   | myproducer_share | view        | myproducer_schema.ticket_sales_view | NULL        |
   +------------------+--------------------------------------+------------+------------------+-------------+-------------------------------------+-------------+
   ```

   描述数据共享时，它会返回表和视图属性。堆栈会将包含示例数据的表和视图添加到创建者数据库中，例如 `tickit_sales` 和 `tickit_sales_view`。有关 TICKIT 示例数据库的更多信息，请参阅[示例数据库](c_sampledb.md)。

   您无需委托数据共享权限即可运行查询。堆栈会授予必要权限。

1. 使用客户端工具连接使用者集群。描述数据共享，指定创建者的命名空间。

   ```
   DESC DATASHARE myproducer_share OF NAMESPACE '<namespace id>'; --specify the unique identifier for the producer namespace
                     
   +------------------+--------------------------------------+------------+------------------+-------------+-------------------------------------+-------------+
   | producer_account |          producer_namespace          | share_type |    share_name    | object_type |             object_name             | include_new |
   +------------------+--------------------------------------+------------+------------------+-------------+-------------------------------------+-------------+
   |   producer-acct  |        your-producer-namespace       | INBOUND    | myproducer_share | schema      | myproducer_schema                   | NULL        |
   |   producer-acct  |        your-producer-namespace       | INBOUND    | myproducer_share | table       | myproducer_schema.tickit_sales      | NULL        |
   |   producer-acct  |        your-producer-namespace       | INBOUND    | myproducer_share | view        | myproducer_schema.ticket_sales_view | NULL        |
   +------------------+--------------------------------------+------------+------------------+-------------+-------------------------------------+-------------+
   ```

1. 您可以通过指定数据共享的数据库和架构来查询数据共享中的表。有关更多信息，请参阅 [跨数据库查询示例](cross-database_example.md)。以下查询从 TICKIT 示例数据库的 SALES 表中返回销售和卖家数据。有关更多信息，请参阅 [示例数据库](c_sampledb.md)。

   ```
   SELECT * FROM myconsumer_db.myproducer_schema.tickit_sales_view;
                     
   +---------+--------+----------+---------+---------+--------+---------+-----------+------------+---------------------+
   | salesid | listid | sellerid | buyerid | eventid | dateid | qtysold | pricepaid | commission |      saletime       |
   +---------+--------+----------+---------+---------+--------+---------+-----------+------------+---------------------+
   |       1 |      1 |    36861 |   21191 |    7872 |   1875 |       4 |       728 |      109.2 | 2008-02-18 02:36:48 |
   |       2 |      4 |     8117 |   11498 |    4337 |   1983 |       2 |        76 |       11.4 | 2008-06-06 05:00:16 |
   |       3 |      5 |     1616 |   17433 |    8647 |   1983 |       2 |       350 |       52.5 | 2008-06-06 08:26:17 |
   |       4 |      5 |     1616 |   19715 |    8647 |   1986 |       1 |       175 |      26.25 | 2008-06-09 08:38:52 |
   |       5 |      6 |    47402 |   14115 |    8240 |   2069 |       2 |       154 |       23.1 | 2008-08-31 09:17:02 |
   +---------+--------+----------+---------+---------+--------+---------+-----------+------------+---------------------+
   ```
**注意**  
查询会根据共享架构中的视图运行。您无法直接连接到从数据共享创建的数据库。它们是只读的。

1. 要运行包含聚合的查询，请使用以下示例。

   ```
   SELECT * FROM myconsumer_db.myproducer_schema.tickit_sales ORDER BY 1,2 LIMIT 5;
                     
   +---------+--------+----------+---------+---------+--------+---------+-----------+------------+---------------------+
   | salesid | listid | sellerid | buyerid | eventid | dateid | qtysold | pricepaid | commission |      saletime       |
   +---------+--------+----------+---------+---------+--------+---------+-----------+------------+---------------------+
   |       1 |      1 |    36861 |   21191 |    7872 |   1875 |       4 |       728 |      109.2 | 2008-02-18 02:36:48 |
   |       2 |      4 |     8117 |   11498 |    4337 |   1983 |       2 |        76 |       11.4 | 2008-06-06 05:00:16 |
   |       3 |      5 |     1616 |   17433 |    8647 |   1983 |       2 |       350 |       52.5 | 2008-06-06 08:26:17 |
   |       4 |      5 |     1616 |   19715 |    8647 |   1986 |       1 |       175 |      26.25 | 2008-06-09 08:38:52 |
   |       5 |      6 |    47402 |   14115 |    8240 |   2069 |       2 |       154 |       23.1 | 2008-08-31 09:17:02 |
   +---------+--------+----------+---------+---------+--------+---------+-----------+------------+---------------------+
   ```

   该查询从示例 TICKIT 数据中返回销售和卖家数据。

   有关数据共享查询的更多示例，请参阅[共享对 AWS 账户内数据的读取访问权限](within-account.md)。