使用 Amazon CloudFormation 管理数据共享 - Amazon Redshift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 Amazon CloudFormation 管理数据共享

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

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

如果您想通过运行 SQL 语句在 Amazon 账户中开始共享数据,以便在不使用 CloudFormation 的情况下设置数据共享并授予权限,请参阅共享对 Amazon Web Services 账户内数据的读取访问权限

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

有关开始设置 Amazon Redshift 集群的更多信息,请参阅开始使用 Amazon Redshift。有关使用 CloudFormation 自动进行设置的更多信息,请参阅什么是 Amazon CloudFormation?

重要

在启动 CloudFormation 堆栈之前,请确保在同一账户中有两个 Amazon Redshift 集群,并且这些集群使用 RA3 节点。确保每个集群都有一个数据库和一个超级用户。有关更多信息,请参阅 CREATE DATABASEsuperuser

要启动您的 CloudFormation 堆栈,以进行 Amazon Redshift 数据共享,请执行以下操作:
  1. 单击启动 CFN 堆栈,将您带到 Amazon Web Services Management Console 中的 CloudFormation 服务。

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

    堆栈创建过程启动,引用存储在 Amazon S3 中的 CloudFormation 模板文件。CloudFormation 模板指的是声明组建堆栈的 Amazon 资源的 JSON 格式文本文件。有关 CloudFormation 模板的更多信息,请参阅了解模板基础知识

  2. 选择下一步,输入堆栈详细信息。

  3. 根据参数中的每个集群,输入以下内容:

    • 您的 Amazon Redshift 集群名称,例如 ra3-consumer-cluster

    • 您的数据库名称,例如 dev

    • 数据库用户的名称,例如 consumeruser

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

    选择下一步

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

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

  5. 功能下,选择我确认 Amazon CloudFormation 可能会创建 IAM 资源

  6. 选择创建堆栈

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

如果创建堆栈时发生错误,请执行以下操作:

  • 确保为每个 Redshift 集群输入了正确的集群名称、数据库名称和数据库用户名。

  • 确保您的集群有 RA3 节点。

  • 请确保您以具有创建 IAM 角色和 Lambda 函数的权限的用户身份登录。有关创建 IAM 角色的更多信息,请参阅创建 IAM 角色。有关创建 Λ 函数的策略的更多信息,请参阅函数开发

查询您创建的数据共享

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

要查询您的数据共享:
  1. 使用客户端工具(如 Amazon Redshift 查询编辑器 v2),连接到创建 CloudFormation 堆栈时输入的数据库上的生产者集群。

  2. 查询数据共享。

    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 关联的数据库名称。

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

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

    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_salestickit_sales_view。有关 TICKIT 示例数据库的更多信息,请参阅示例数据库

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

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

    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 | +------------------+--------------------------------------+------------+------------------+-------------+-------------------------------------+-------------+
  5. 您可以通过指定数据共享的数据库和架构来查询数据共享中的表。有关更多信息,请参阅 使用跨数据库查询的示例。以下查询从 TICKIT 示例数据库的 SALES 表中返回销售和卖家数据。有关更多信息,请参阅 SALES 表

    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 | +---------+--------+----------+---------+---------+--------+---------+-----------+------------+---------------------+
    注意

    查询会根据共享架构中的视图运行。您无法直接连接到从数据共享创建的数据库。它们是只读的。

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

    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 数据中返回销售和卖家数据。

    有关数据共享查询的更多示例,请参阅共享对 Amazon Web Services 账户内数据的读取访问权限