步骤 4:将数据从 Amazon S3 加载到 Amazon Redshift - Amazon Redshift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

步骤 4:将数据从 Amazon S3 加载到 Amazon Redshift

使用 Amazon Redshift 查询编辑器是将数据加载到表中的最简单方法。创建集群后,您可以使用 Amazon Redshift 控制台将数据从 Amazon S3 加载到集群。

利用查询编辑器 v2,可在使用加载数据向导时简化数据加载。有关更多信息,请参阅 使用查询编辑器 v2 将您自己的数据从 Amazon S3 加载到 Amazon Redshift。您也可以使用查询编辑器 v2 创建表并加载数据。有关更多信息,请参阅 使用查询编辑器从 Amazon S3 加载示例数据

要将您自己的数据从 Amazon S3 加载到 Amazon Redshift,Amazon Redshift 需要一个 IAM 角色,该角色具有从指定的 Amazon S3 桶加载数据所需的权限。

首先,连接到数据库。接下来,在数据库中创建一些表。然后,将您自己的数据从 Amazon S3 加载到 Amazon Redshift。有关使用查询编辑器 v2 的更多信息,请参阅《Amazon Redshift 管理指南》中的使用查询编辑器 v2

在查询编辑器 v2 加载数据向导中生成和使用的 COPY 命令支持可用于 COPY 命令语法的所有参数,以从 Amazon S3 加载数据。有关 COPY 命令及其用于从 Amazon S3 中复制加载的选项的信息,请参阅《Amazon Redshift 数据库开发人员指南》中的 Amazon Simple Storage Service 中的 COPY 命令

此时,您拥有了一个名为 dev 的数据库,并且已连接。接下来,在该数据库中创建一些表,将数据上传到表并尝试查询。为方便起见,您上载的示例数据在 Amazon S3 桶中可用。

注意

如果您使用的是 SQL 客户端工具,请确保您的 SQL 客户端已连接到集群。

完成此步骤后,您可以执行以下操作:

注意

要尝试在查询编辑器中查询数据而不加载自己的数据,请选择示例数据中的加载示例数据。如果您这样做,Amazon Redshift 会在集群创建过程中自动将其示例数据集加载到您的 Amazon Redshift 集群。

要从 Amazon S3 加载示例数据
  1. 创建表。

    如果您使用 Amazon Redshift 查询编辑器,请分别复制并运行下列 create table 语句,以在 dev 数据库中创建表。有关语法的更多信息,请参阅《Amazon Redshift 数据库开发人员指南》中的 CREATE TABLE

    create table users( userid integer not null distkey sortkey, username char(8), firstname varchar(30), lastname varchar(30), city varchar(30), state char(2), email varchar(100), phone char(14), likesports boolean, liketheatre boolean, likeconcerts boolean, likejazz boolean, likeclassical boolean, likeopera boolean, likerock boolean, likevegas boolean, likebroadway boolean, likemusicals boolean);
    create table venue( venueid smallint not null distkey sortkey, venuename varchar(100), venuecity varchar(30), venuestate char(2), venueseats integer);
    create table category( catid smallint not null distkey sortkey, catgroup varchar(10), catname varchar(10), catdesc varchar(50));
    create table date( dateid smallint not null distkey sortkey, caldate date not null, day character(3) not null, week smallint not null, month character(5) not null, qtr character(5) not null, year smallint not null, holiday boolean default('N'));
    create table event( eventid integer not null distkey, venueid smallint not null, catid smallint not null, dateid smallint not null sortkey, eventname varchar(200), starttime timestamp);
    create table listing( listid integer not null distkey, sellerid integer not null, eventid integer not null, dateid smallint not null sortkey, numtickets smallint not null, priceperticket decimal(8,2), totalprice decimal(8,2), listtime timestamp);
    create table sales( salesid integer not null, listid integer not null distkey, sellerid integer not null, buyerid integer not null, eventid integer not null, dateid smallint not null sortkey, qtysold smallint not null, pricepaid decimal(8,2), commission decimal(8,2), saletime timestamp);
  2. 使用 COPY 命令从 Amazon S3 中加载示例数据。

    注意

    我们建议使用 COPY 命令将大型数据集从 Amazon S3 或 Amazon DynamoDB 加载到 Amazon Redshift 中。有关 COPY 语法的更多信息,请参阅《Amazon Redshift 数据库开发人员指南中的 COPY

    1. 下载文件 tickitdb.zip,其中包含各个示例数据文件。

    2. 将各个文件解压缩并将其加载到 Amazon Web Services 区域 中 Amazon S3 桶的 tickit 文件夹中。

    3. 编辑本教程中的 COPY 命令以指向 Amazon S3 桶中的文件。有关如何使用 Amazon S3 管理文件的信息,请参阅《Amazon Simple Storage Service 用户指南》中的创建和配置 S3 桶

    4. 要加载示例数据,为您的集群提供代表您访问 Amazon S3 的身份验证。您可以通过引用您在前面的步骤中创建并设置为集群默认角色的 IAM 角色来提供身份验证。

      COPY 命令包含用于 IAM 角色、桶名称和 Amazon Web Services 区域 的 Amazon 资源名称 (ARN) 的占位符,如下例所示。

      copy users from 's3://<myBucket>/tickit/allusers_pipe.txt' iam_role default delimiter '|' region '<aws-region>';

      您的 COPY 命令应类似于以下示例。

      copy users from 's3://<myBucket>/tickit/allusers_pipe.txt' iam_role default delimiter '|' region '<aws-region>';

      要加载示例数据,请将以下 COPY 命令中的 <myBucket><aws-region> 替换为您的值。如果您使用的是 Amazon Redshift 查询编辑器,请单独运行以下命令。

      copy users from 's3://<myBucket>/tickit/allusers_pipe.txt' iam_role default delimiter '|' region '<aws-region>';
      copy venue from 's3://<myBucket>/tickit/venue_pipe.txt' iam_role default delimiter '|' region '<aws-region>';
      copy category from 's3://<myBucket>/tickit/category_pipe.txt' iam_role default delimiter '|' region '<aws-region>';
      copy date from 's3://<myBucket>/tickit/date2008_pipe.txt' iam_role default delimiter '|' region '<aws-region>';
      copy event from 's3://<myBucket>/tickit/allevents_pipe.txt' iam_role default delimiter '|' timeformat 'YYYY-MM-DD HH:MI:SS' region '<aws-region>';
      copy listing from 's3://<myBucket>/tickit/listings_pipe.txt' iam_role default delimiter '|' region '<aws-region>';
      copy sales from 's3://<myBucket>/tickit/sales_tab.txt' iam_role default delimiter '\t' timeformat 'MM/DD/YYYY HH:MI:SS' region '<aws-region>';